excel - OneDrive 中的文件未附加到 Outlook 消息 : Download error

标签 excel vba outlook onedrive

我在 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 文件夹 .
  • 转至 https://onedrive.live.com .
  • 写下或复制 CID地址栏中的数字:

  • map1

  • Windows Key Windows 键,然后右键单击 Computer ,然后单击“Map Network Drive”。

  • map2

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

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



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

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

  • Map4

    驱动器将被映射!此时,您可以在桌面等上创建驱动器号的快捷方式,并且可以将驱动器号用作本地驱动器来保存/打开文件等。

    Links, Not Attachments!

    最后的想法:

    我必须指出, 有点违背了云存储的目的。将文件保存到 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)



    更多信息:
  • Office 365 Tips Worth Sharing: Email a Link, Not an Attachment
  • MSDN : Workbook.ExportAsFixedFormat Method
  • Links vs Attachments - What method should you use?
  • MSDN : Application.Path Property
  • Office.com : Share OneDrive files and folders
  • MSDN : Environ Function
  • Office.com:Change Permissions or Stop sharing OneDrive files or folders
  • Using the REST API以编程方式访问用户的 Microsoft 帐户(例如 OneDrive)
  • 关于excel - OneDrive 中的文件未附加到 Outlook 消息 : Download error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48842149/

    相关文章:

    c++ - ATL COM 向 excel 添加功能

    excel - VBA Excel : How to edit links using user cell inputs?

    VBA-替换字符串中的字符

    vba - 使用条件计算可见行数

    excel - MAX 值使用在 Excel 中的一列中查找逗号分隔列表

    vba - Microsoft Access,在数据表子表单中自动生成列

    excel - VBA 中的字符串分隔问题

    javascript - 无法从 Outlook 功能区打开链接是 javascript API 而不是弹出窗口

    html - 如何从 HTML 电子邮件的 outlook 电子邮件 View 周围删除默认填充/边距

    c# - native 窗口 : Release Handle On Close