OneDrive 中的文件未附加到 Outlook 消息:下载错误

File from OneDrive is not attached to Outlook message: Download error

我在 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

returns

不知何故,这会导致错误 "Download failed":

这是为什么以及如何解决?

使用临时文件夹或应用程序路径导出 .PDF:

如果目标只是将 .PDF 文件附加到 Outlook 邮件项目,那么 而不是 .PDF 导出到:

ThisWorkbook.Path

...(returns 当前工作簿的保存路径),您可以改为将其导出到:

Application.Path

...其中 returns Excel 安装路径 ;在我的例子中是:

C:\Program Files (x86)\Microsoft Office\root\Office16

因此您可以按如下方式更改此行:

strPDF = Application.Path & "\" & ExportFilename & ".`.PDF`"

...或者,将其导出到 Windows 临时文件夹:

 strPDF = Environ("temp") & "\" & ExportFilename & ".pdf"

特别是如果 .PDF 唯一目的 是附加到电子邮件。在我的例子中,Windows 临时文件夹是:

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 来 save/retrieve 文件,我建议 将驱动器号映射到 OneDrive 文件夹

  1. 转到https://onedrive.live.com

  2. 在地址栏中写下或复制CID号码:

  1. 点击Windows键,然后点击Right-clickComputer,点击“Map Network Drive” .

4. 在 映射网络驱动器 对话框中,选择您将用于引用 OneDrive 的驱动器盘符(可能 O:)。在 Folder 文本框中,输入:

https://d.docs.live.net/ Your CID Number

单击 Reconnect at Logon,然后单击 完成

  1. 系统将提示您输入 Microsoft 帐户用户 ID 和密码

驱动器将被映射!此时可以在桌面等创建该盘符的快捷方式,就可以像本地盘一样使用该盘符Saving/Opening个文件等


最终想法:

我必须指出,通过 将文件保存在 OneDrive [=125],您有点 破坏了 Cloud Storage 的目的=]然后 将其作为附件通过电子邮件发送

现在的最佳实践告诉我们将文件保存在共享或可共享的位置,然后通过电子邮件 link 发送给它——所有这些都可以借助 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)


更多信息: