You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过VBA在Outlook中添加PPT附件?现有代码附件功能失效

解决VBA生成Outlook邮件时附件添加失效的问题

你代码里添加附件的写法是错误的——Outlook的MailItem对象不能直接给.Attachment赋值,必须使用Attachments.Add()方法来添加附件,这就是附件功能失效的核心原因。

修正后的代码片段

把你代码中添加附件的部分替换成以下内容:

'Inserisco allegato nella mail
'先检查文件是否存在,避免因路径错误/文件缺失报错
If Dir(FileSettimanale) <> "" Then
    .Attachments.Add(FileSettimanale)
Else
    MsgBox "指定的附件文件不存在:" & FileSettimanale, vbExclamation
End If

关键说明

  • Attachments.Add是Outlook对象模型中添加附件的标准方法,直接传入文件的完整路径即可完成添加
  • 增加Dir()函数的检查是为了避免因FileSettimanale变量存储无效路径、文件被删除或移动导致代码报错
  • 确保FileSettimanale变量存储的是完整的绝对文件路径(例如C:\Docs\monitoraggio_settimanale.pptx),相对路径可能会导致Outlook无法定位文件

完整的修正后代码示例:

'First, select the body format as olFormatHtml.
.BodyFormat = olFormatHTML

'Now display the email.
.Display

'write the email in the body of the email
.HTMLBody = "Buongiorno Giacomo, " & "<br>" & "<br>" & "Invio in allegato il monitoraggio settimanale di Neafidi al " & DataCons & "<br>" & "<br>" & "Grazie e buon proseguimento," & .HTMLBody

'Invio la mail al seguente indirizzo
.To = MailInvio
.CC = MailInvioCC

'Inserisco oggetto della mail
.Subject = SoggettoMail

'Inserisco allegato nella mail
If Dir(FileSettimanale) <> "" Then
    .Attachments.Add(FileSettimanale)
Else
    MsgBox "指定的附件文件不存在:" & FileSettimanale, vbExclamation
End If

End With

内容的提问来源于stack exchange,提问作者Luca Guglielmini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 02:05:18