vba - 使用 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
解决方案
根据要求,这是我的评论作为答案:
你确定附件(1)是PDF文件吗?签名和图像可以记录为附件。您应该扫描附件集合并检查扩展名,直到找到 PDF 文件。
推荐阅读
- cpu-architecture - 如何判断真值表是否有错?
- java - Android Studio 使用的 JAVA_HOME 的实际值是多少?
- c# - 实体框架:批量批量UpdateFromQuery
- python - 使用 Python 从 HTML 中的脚本标签中提取数据
- python - 如何在 discord.py 中记录特定用户的信息?
- julia - Julia 中是否存在具有可变参数的分布?
- postgresql - Scaffold-DbContext:在 .NetCore 中从 postgres 数据库生成上下文时消除下划线
- batch-file - 无密码修改计划任务
- javascript - 在 Javascript 中检查单选按钮的值
- nexus - 将 Nexus 3.15.2(包含 nexus2 代理存储库)升级到 3.26.1