首页 > 解决方案 > 使用 SaveAsFile 时 Outlook 损坏 PDF

问题描述

当 PDF 文件到达我的收件箱时,我正在使用以下代码在 Outlook 中自动导出它们。但是,它保存的文件已损坏。SaveAsFile 方法只接受一个参数 - 要保存到的文件路径 - 在文档中没有说我可以传递文件类型。如何在不损坏文件的情况下保存这些 PDF 附件?

    Private WithEvents Items As Outlook.Items

    Private Sub Application_Startup()

        'Declaring Variables [BD]
        Dim oOutlook As Outlook.Application
        Dim oNameSpace As Outlook.NameSpace
        Dim oFolder As Outlook.MAPIFolder

        'Intializing Variables [BD]
        Set oOutlook = Outlook.Application
        Set oNameSpace = Application.GetNamespace("MAPI")

        Set oFolder = oNameSpace.GetDefaultFolder(olFolderInbox).Parent
        Set oFolder = oFolder.Folders("Produce Availability").Folders("Earls Organic")
        Set Items = oFolder.Items

    End Sub

    Private Sub Items_ItemAdd(ByVal Item As Object)

    'Declaring Variables [BD]
    Dim sOutputFileName As String

    Dim oMessage As Outlook.MailItem
    Dim oAttachment As Outlook.Attachments

    'Initializing Variables [BD]
    sDateTime = Format(Now(), "yyyymmddhhnnss")
    sOutputFolderPath = "C:\Earls Organic\"

        On Error GoTo ErrorHandler

        If TypeName(Item) = "MailItem" Then

            Set oMessage = Item
            Set oAttachment = oMessage.Attachments

            sOutputFileName = oMessage.Subject & " " & sDateTime
            sOutputFolderPathAndName = sOutputFolderPath & sOutputFileName & ".pdf"
            oAttachment.Item(1).SaveAsFile sOutputFolderPathAndName

            Set oAttachment = Nothing
            Set oItem = Nothing

        End If

    ProgramExit:
        Exit Sub

    ErrorHandler:
            MsgBox Err.Number & " - " & Err.Description
            Resume ProgramExit

    End Sub

标签: vbapdfoutlook

解决方案


根据要求,这是我的评论作为答案:

你确定附件(1)是PDF文件吗?签名和图像可以记录为附件。您应该扫描附件集合并检查扩展名,直到找到 PDF 文件。


推荐阅读