Unable to call outlook mail item "SaveAs" method

I’m trying to save the outlook email item to physical drive through InvokeCode activity but getting below error all the time:

Outlook version:2013

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ----> System.Runtime.InteropServices.COMException: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
at Microsoft.Office.Interop.Outlook._MailItem.SaveAs(String Path, Object Type)
at UiPathCodeRunner_cf009ab11a6e4202bc242f7f11192581.Run(String

Hi @ronlobo

Could you give these topics a read?
https://forum.uipath.com/search?q=0x80004004%20status%3Asolved

They all contain a post marked as a solution and they all share your error code.

Hi,

I think I’ve created the same issue but in my case Save method works SaveAs method does not work.

And it used to work before, stopped workin in 18.4.5 version onwards

Hi @ronlobo,
Have you had any workaround or solution?

@2aozturk
I found a workaround for the requirement I was working upon. But the issue is still there with Outlook Interop.

I had no option to loop through all unread mail and save the valid ones and mark them unread through UiPath activities.

Have you checked Event Viewer logs? Did you see anything interesting? I have two candidates for this error at the moment;
1-x86 to x64 compatibility which is a weaker candidate,
2-Antivirus/Security/Policy restrictions

I will inform you if I find a solution, please text me back if you find anything interesting :slight_smile:

Thanks…

1 Like

@2aozturk Thanks
I guess the issue is with outlook security. I don’t have any error logs in viewer but will try one more time and see if I can trace back further.

Thanks

I have a security log in event viewer like Security Center failed to validate caller with error DC040780 it seems like an antivirus issue may be happening.

Have you tried Save or Send methods at all? Both working in my case, only SaveAs is failing…

@2aozturk

Yes both Save and Send work for me.

I tried using SaveAs again and can see below error logged in the event viewer

System.IO.IOException: The write operation failed, see inner exception. —> System.ServiceModel.CommunicationObjectAbortedException: The operation cannot be completed because the pipe was closed.

I think we both struggle from the same reason then :slight_smile:

I’ve tried same functionality with vba codes and saw the error mentioned in my previous post.

Below is the code, you can run it within excel vba designer.
Note: Outlook must be left opened during the runtime of this code

Public Sub create_then_save_email()
On Error GoTo lbl_error

Dim OutApp As Outlook.Application
Set OutApp = GetObject(, "Outlook.Application")
Dim email As Outlook.MailItem
Set email = OutApp.CreateItem(0)
email.To = "anemail@address.com"
email.Subject = "Test Subject"
email.HTMLBody = "Test <b>HTML</b> Body"
'Application.Wait DateAdd("s", 3, Now)

Dim targetFolder As String, targetFile As String
targetFolder = "C:\NewFolder"
targetFile = targetFolder & "\" & "SavedEmail.msg"
create_folder targetFolder

MsgBox "E-mail will be saved saved at when you confirm this popup => " & targetFile, vbExclamation

email.SaveAs targetFile

MsgBox "E-mail saved at [" & targetFile & "] please check the location...", vbInformation

Exit Sub

lbl_error:
If Err <> 0 Then
    MsgBox Err.Description, vbCritical
End If
End Sub

one more :slight_smile: you should also past this code into vba designer, sorry :slight_smile:

Sub create_folder(s_directory As String)
Set OFSO = CreateObject("Scripting.FileSystemObject")
dirExists = OFSO.FolderExists(s_directory)

'Set targetFolder = "C:\NewFolder"

If Not dirExists Then
    MkDir s_directory
End If

Thanks

I have my code written in VB.Net. I tried running the same code on my laptop and it is working fine without any issue. Looks like an outlook security issue on client domain.

1 Like

Hi, problem was related with a computer policy, we’ve set the following property by adding robot process as trusted process or you can simple set this policy to Automatically approve with your own risk :slight_smile:

Thanks… :slight_smile:

Links is below:
<link removed>

2 Likes

And also here are protected objects and methods of Outlook

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.