如何通过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
相关产品推荐
相关产品推荐

