我在 Excel 2016 中有一个 VBA 脚本,可将工作表导出为 PDF,然后在 Outlook 2016 中创建电子邮件:
Tabelle8.ExportAsFixedFormat Type:=xlTypePDF, Filename:=
ThisWorkbook.Path & "\" & ExportFilename, Quality:=xlQualityStandard
, IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish _
:=False
strPDF = ThisWorkbook.Path & "\" & ExportFilename & ".pdf"
Set OutlookApp = CreateObject("Outlook.Application")
Set strEmail = OutlookApp.CreateItem(0)
With strEmail
.To = recipient
.CC = ""
.Subject = subject
.HTMLBody = text
.Attachments.Add strPDF
.Display
EndWith
只要工作簿位于本地驱动器上,它就可以正常工作,但一旦位于 OneDrive 中就会失败。在 OneDrive 上,
MsgBox strPDF
返回
不知何故,这会导致错误“下载失败”:
为什么会这样以及如何解决?
最佳答案
使用临时文件夹或应用程序路径导出 .PDF
:
如果目标只是附上.PDF
文件到 Outlook 邮件项目,然后而不是导出 .PDF
至:
ThisWorkbook.Path
...(返回当前工作簿的保存路径),您可以将其导出到:
Application.Path
...返回 Excel 安装路径 ;就我而言,它是:
C:\Program Files (x86)\Microsoft Office\root\Office16
所以你会改变这一行如下:
strPDF = Application.Path & "\" & ExportFilename & ".`.PDF`"
...或者,将其导出到 Windows 临时文件夹 :
strPDF = Environ("temp") & "\" & ExportFilename & ".pdf"
特别是如果
.PDF
的唯一目的是附加到电子邮件中。就我而言,Windows Temp 文件夹是:C:\Users\[WindowsLoginName]\AppData\Local\Temp
无论哪种方式,您仍然可以(至少是临时的)访问您选择的任何目的地的文件。
保留副本:
如果您还需要在 OneDrive 上保留文件的副本,那么您有几个选择。
如果
.PDF
之前已正确保存到 OneDrive,但 Excel 无法将其附加到 Outlook 邮件项目,然后您可以导出文件,如下所示:'export PDF to workbook path
strPDF_save = ThisWorkbook.Path & "\" & ExportFilename & ".pdf"
Tabelle8.ExportAsFixedFormat xlTypePDF, strPDF
'export PDF to temp folder
strPDF_temp = Environ("temp") & "\" & ExportFilename & ".pdf"
Tabelle8.ExportAsFixedFormat xlTypePDF, strPDF
'create Outlook object and send email as attachment
Set OutlookApp = CreateObject("Outlook.Application")
With OutlookApp.CreateItem(0)
.To = recipient
.Subject = Subject
.HTMLBody = Text
.Attachments.Add strPDF
.Display 'display the email before sending
End With
(我还从录制宏中删除了一些多余的代码。)
将驱动器号映射到 OneDrive:
如果您打算使用 OneDrive 定期保存/检索文件,我建议 将驱动器号映射到 OneDrive 文件夹 .
CID
地址栏中的数字:Computer
,然后单击“Map Network Drive
”。 4.在映射网络驱动器对话框中,选择一个用于引用 OneDrive 的驱动器号(可能是
O:
)。在 Folder
文本框,输入:
https://d.docs.live.net/
Your CID Number
点击
Reconnect at Logon
,然后单击完成。驱动器将被映射!此时,您可以在桌面等上创建驱动器号的快捷方式,并且可以将驱动器号用作本地驱动器来保存/打开文件等。
最后的想法:
我必须指出, 有点违背了云存储的目的。将文件保存到 OneDrive 然后 以附件形式通过电子邮件发送 .
最佳实践现在告诉我们将文件保存在共享或可共享位置,然后通过电子邮件发送指向它的链接——所有这些都可以通过 Office 365 的强大功能完成。
这样做可以降低以下风险:
Raising spam flags
Some email clients will mark emails containing large files as spam and drop the incoming message into a junk mail folder.
Delivery failure
Even in the cloud age, some email clients have strict file size limits. Sending a link instead of a bulky file ensures a smooth delivery to the intended recipient.
Consuming space
Managing your organization’s data and storage keeps your IT staff hopping. Sending and receiving large files – especially when there’s a lighter alternative – makes their lives more difficult. (Source)
更多信息:
关于excel - OneDrive 中的文件未附加到 Outlook 消息 : Download error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48842149/