pdf - 只想保存 .pdf 附件
问题描述
我只是想将所选电子邮件附件中的 PDF 保存到我计算机上的文件夹中。现在使用下面的代码保存所有附件,例如 JPG 和 htm 项目。我是否在不正确的位置选择了 PDF?无论我将用于选择 PDF 的代码放在哪里,似乎在玩耍之后它实际上并没有挑选出 PDF
Sub SavePDFAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim l As Long
Dim lngCount As Long
Dim tlngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim finalpath As String
On Error Resume Next
' Instantiate an Outlook Application object.
Set objOL = Application
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' Set the Attachment folder.
strFolderpath = "T:"
' Check each selected item for attachments.
For Each objMsg In objSelection
Set objAttachments = objMsg.Attachments
' Pull PDFs only
For Each objAttachment In objMsg.Attachments
If Right(objAttachment.FileName, 3) = "pdf" Then
objAttachment.SaveAsFile strFolderpath & strFile
End If
Next objAttachment
lngCount = objAttachments.count
If lngCount > 0 Then
For i = lngCount To 1 Step -1
' Get the file name.
strFile = objAttachments.item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
objAttachments.item(i).SaveAsFile strFile
Next i
End If
Next
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
解决方案
请参考以下代码:
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
' Get the path to your My Documents folder
On Error Resume Next
' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' The attachment folder needs to exist
' You can change this to another folder name of your choice
' Set the Attachment folder.
strFolderpath = "\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\TEST\"
' Check each selected item for attachments.
For Each objMsg In objSelection
For each objAttachment in objMsg.Attachments
if Right(objAttachment.FileName, 3) = "pdf" then
' Append the file name to the folder.
strFile = strFolderpath & objAttachment.FileName
' Save it
objAttachments.Item(i).SaveAsFile strFile
end if
Next objAttachment
Next objMsg
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
保存 PDF 代码:
if Right(objAttachment.FileName, 3) = "pdf" then
有关更多信息,请参阅链接:VBA 将带有 pdf 扩展名的电子邮件附件保存到文件夹
推荐阅读
- javascript - 为每个项目添加类
- go - 如何处理“服务器推送”http连接的关闭事件
- mysql - 使用 MySQL Workbench 添加外键时出现 MySQL 错误 1064
- json - 在 Sequelize 结果中包含资源链接
- javascript - 如何使用 Django 中用户过滤的数据库中的数据生成气泡图(Chart.js)?
- java - java.lang.VerifyError:无法从具有 Firebase 管理员的最终类继承
- python-3.x - 在 matplotlib 3-D 图上设置刻度
- javascript - 如何使用 jQuery 通过键删除 JSON 中的 JSON?
- ios - 如何将 JSON 值从一个 ViewController 传递到另一个
- r - r- 'regr.xgboost' 不支持因子输入