Reputation: 67
I'm trying to save to hard drive all the mails in my Outlook folders.
'==========================================
' Save Inbox Mail Items To The File System
'==========================================
Sub saveInboxMailItemsToFileSystem()
' Set variables
Dim ns As Outlook.NameSpace
Dim inbox As Outlook.MAPIFolder
Dim item As Outlook.MailItem
Dim strFileName As String
' Set namespace
Set ns = Application.GetNamespace("MAPI")
' Get inbox folder
Set inbox = ns.GetDefaultFolder(olFolderInbox)
' Loop through all mail items
For Each item In inbox.Items
' Set filename
strFileName = "C:\Users\<moi>\SvgMail\" & VBA.Format(item.ReceivedTime, "yyyymmdd", vbUseSystemDayOfWeek) & " - " & item.Subject & ".msg"
' Save mail item to file system
item.SaveAs strFileName, Outlook.OlSaveAsType.olMSG
Next item
End Sub
I get the following error:
Erreur d'exécution '-2147286788 (800300fc)': Echec de l'opération.
Upvotes: 0
Views: 392
Reputation: 49405
You need to make sure that the result file name (strFileName
) is valid and contains only allowed symbols. Try to save any file using the problematic name and you will be notified that some symbols are not allowed. But in our case the Outlook object model doesn't give you meaningful answers, it just notifies you about a problem. You, as a developer, should analyze such cases and fix the problem. For example, if you try to search for the error code you may find a similar thread - Outlook Email Archiving Macro Doesnt work if subject has asterisk. For example, you may find a sample function which replaces some characters for using in the SaveAs
call:
Function FixFileName(FileName As String) As String
Dim fname As String
fname = Trim(FileName)
fname = Replace(fname, " ", "_")
fname = Replace(fname, ",", "")
fname = Replace(fname, "'", "")
fname = Replace(fname, "(", "")
fname = Replace(fname, ")", "")
fname = Replace(fname, "~", "")
fname = Replace(fname, "*", "")
fname = Replace(fname, "?", "")
fname = Replace(fname, "/", "")
fname = Replace(fname, "\", "")
fname = Replace(fname, """", "")
FixFileName = fname
End Function
Upvotes: 1