【问题标题】:Save attachments using mail subject in file name使用文件名中的邮件主题保存附件
【发布时间】:2022-08-13 21:23:04
【问题描述】:

我想保存 Outlook 365 收件箱中的所有附件。

复制this tutorial 我写道:

Sub Download_Attachments()

    Dim ns As NameSpace
    Dim olFolder_Inbox As Folder
    Dim olMail As MailItem
    Dim olAttachment As Attachment

    Dim fso As Object
    Dim File_Saved_Folder As String

    File_Saved_Folder_Path = \"C:\\GIS\\temp\\mails\"

    Set ns = GetNamespace(\"MAPI\")
    
    Set olFolder_Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set fso = CreateObject(\"Scripting.FileSystemObject\")
    
    For Each olMail In olFolder_Inbox.Items
    
        If TypeName(olMail) = \"MailItem\" And olMail.Attachments.Count > 0 Then
        
            fso.CreateFolder (fso.BuildPath(File_Saved_Folder_Path, Trim(olMail.Subject)))
            
            For Each olAttachment In olMail.Attachments
            
                olAttachment.SaveAsFile fso.BuildPath(File_Saved_Folder_Path, Trim(olMail.Subject)) & \"\\\" & olAttachment.FileName
                                    
            Next olAttachment
        
        End If

    Next olMail
    
    Set olFolder_Inbox = Nothing
    Set ns = Nothing
    
    Set fso = Nothing
    
End Sub

当我执行宏时,我大致得到(翻译自瑞典语):

错误 76,找不到路径

  • 我假设您在 fso.CreateFolder (fso.BuildPath(File_Saved_Folder_Path, Trim(olMail.Subject))) 行收到错误?如果是这样,请确保您的主题不包含special characters(因为文件不能包含它们)。还要限制你的主题以防万一,这样你就不会在命名时创建太大的路径

标签: vba outlook office365 filepath email-attachments


【解决方案1】:

MailItem 类的Subject 属性和Attachment 类的FileName 属性可能包含不能用于文件名的禁止符号。所以。在调用Attachment 类的SaveAsFile 方法之前,您需要检查文件路径是否存在此类文件夹并且路径不包含禁止符号。请参阅What characters are forbidden in Windows and Linux directory names? 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    相关资源
    最近更新 更多