如何将文件名中的主题另存为?
How to SaveAs with Subject in file name?
我正在尝试将 Outlook 文件夹中的所有邮件保存到硬盘。
'==========================================
' 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
我收到以下错误:
Erreur d'exécution '-2147286788 (800300fc)': Echec de l'opération.
您需要确保结果文件名 (strFileName
) 有效且仅包含允许的符号。尝试使用有问题的名称保存任何文件,您将收到不允许使用某些符号的通知。但在我们的例子中,Outlook 对象模型不会给你有意义的答案,它只是通知你一个问题。作为开发人员,您应该分析此类情况并解决问题。例如,如果您尝试搜索错误代码,您可能会找到类似的线程 - Outlook Email Archiving Macro Doesnt work if subject has asterisk。例如,您可能会找到一个示例函数,它替换了一些在 SaveAs
调用中使用的字符:
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
我正在尝试将 Outlook 文件夹中的所有邮件保存到硬盘。
'==========================================
' 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
我收到以下错误:
Erreur d'exécution '-2147286788 (800300fc)': Echec de l'opération.
您需要确保结果文件名 (strFileName
) 有效且仅包含允许的符号。尝试使用有问题的名称保存任何文件,您将收到不允许使用某些符号的通知。但在我们的例子中,Outlook 对象模型不会给你有意义的答案,它只是通知你一个问题。作为开发人员,您应该分析此类情况并解决问题。例如,如果您尝试搜索错误代码,您可能会找到类似的线程 - Outlook Email Archiving Macro Doesnt work if subject has asterisk。例如,您可能会找到一个示例函数,它替换了一些在 SaveAs
调用中使用的字符:
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