vbaoutlooksharepoint-online

Save MailItem in SharePoint Online using Outlook VBA


I have to adapt a macro which is saving selected MailItems in different folders to save on SharePoint Online of my company.

We are well able to do a manual SaveAs, and add the .msg file to a SharePoint folder by entering the URL of SharePoint in the Address bar.

The idea is:

'for each mail selected (but generally there is only one mail selected):
For Each olkMsg In Outlook.ActiveExplorer.Selection
    'Many actions done to collect information from mail
    
    'Save the email in Sharepoint 
    olkMsg.SaveAs "https://MyCompany.sharepoint.com/sites/MyTeam/Shared Documents/TestArchives/TestXXX.msg", olMSG

I tried by encoding the URL. Run time error -2147286788 (800300fc) disappeared but nothing is going to SharePoint:

olkMsg.SaveAs "https%3A%2F%2FMyCompany.sharepoint.com%2Fsites%2FMyTeam%2FShared Documents%2FTestArchives%2FTestXXX.msg", olMSG

I tried the UNC path. It did not work on our SharePoint on Microsoft Cloud:

olkMsg.SaveAs "\\MyCompany.sharepoint.com\sites\MyTeam\Shared Documents\TestArchives\TestXXX.msg", olMSG

I don't mind an intermediate step such as saving the file in a Temp folder first and then transferring it to SharePoint. I tried without success, and deleted the code.


Solution

  • I didn't manage to save directly to sharepoint with MailItem.SaveAs. And don't know why it work when we do a manual SaveAs and paste the URL... Unfortunately MailItem.SaveAs is totally differents of WorkBook.SaveAs (which works fine with HTTP URL).

    As workaround I created a shortcut of folder in Sharepoint to my OneDrive: enter image description here

    and then put the path of this shortcut in mailItem.SaveAs "C:\Users\USERID\OneDrive - MyCompany\FolderNameInSharepoint"

    enter image description here