Read a .msg file and save attachement - You dont have permission to open it

Hello everyone!

I had a similar issue with the one described here Read a .msg file and save attachement - Help - UiPath Community Forum.
I need to save the attachments of a .msg file, but using the VB code proposed as solution, leads me to this error.
image

VB code

Could you please help me with any idea?
Thanks!

Hi @Irina_Butu ,
You can try by activity
1.Read the mail usng get mail activity as per your requirement
->output will be of type mail message

2.Use for each loop of the mail message, check with if condition if there are any attachments,

3.Then, if you have any attachments then save them usng save attachments, the output of save attachment will have IEnumberable attachments, so this will have the path along with name of the file and type of the file, again use if statement to check if the IEnumerable attachments ends with .msg format or not,

Regards,

Hello @Irina_Butu

Sequence Name: SaveAttachmentsFromMsg

  1. Assign Activity

    • To: msgFilePath
    • Value: “C:\Path\To\Your\File.msg”
  2. Assign Activity

    • To: outputFolderPath
    • Value: "C:\Path\To\Your\Output\Folder"
  3. Try Catch Activity

    • Try:
      a. Invoke Code

      • Arguments:
        • msgFilePath
        • outputFolderPath

      b. Log Message

      • Message: “Attachments saved successfully.”
    • Catch: Exception
      a. Log Message

      • Message: "Error: " + ex.Message
  4. Invoke Code Activity

    • Arguments:

      • msgFilePath (In/Out)
      • outputFolderPath (In/Out)
    • Code:
      vb
      VB Code to save attachments from .msg file
      outlookApp As New Microsoft.Office.Interop.Outlook.Application
      mailItem As Microsoft.Office.Interop.Outlook.MailItem = outlookApp.Session.OpenSharedItem(msgFilePath)

      For Each attachment In mailItem.Attachments
      attachment.SaveAsFile(Path.Combine(outputFolderPath, attachment.FileName))
      Next

  5. Finally Activity

    • Log Message
      • Message: “Process completed.”

Thanks & Cheers!!!