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

Excel VBA邮件宏:已使用图片粘贴时如何添加邮件正文

解决Outlook邮件正文不显示的问题

问题根源

你代码里的核心矛盾点:

  • 同时使用了.Body(纯文本格式)和WordEditor(Outlook富文本编辑接口),一旦调用Email.GetInspector.WordEditor,Outlook会自动切换邮件格式为富文本/HTML,此时.Body的纯文本内容会被直接忽略。
  • wdChartPicture是Word专属常量,如果未引用Word对象库,运行时会触发未定义错误。

修复后的完整代码

Sub Mail_Button()

    Dim rng As Range
    Dim olApp As Object
    Dim Email As Object
    Dim Sht As Excel.Worksheet
    Dim wdDoc As Object ' 用Object类型,无需强制引用Word库
    Dim wd As Date
    wd = WorksheetFunction.WorkDay(Date, -1)
    
    Set Sht = ActiveWorkbook.Worksheets("Report & Email")
    Set rng = Sht.Range("B1:F9")
    rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
        
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    Set olApp = CreateObject("Outlook.Application")
    Set Email = olApp.CreateItem(0)
    Set wdDoc = Email.GetInspector.WordEditor

    With Email
        .To = "person@gmail.com"
        .Subject = "Report - For " & Format(wd, "mmmm dd, yyyy") & " Session"
        
        ' 通过WordEditor直接写入正文文本
        wdDoc.Range.Text = "Please see attached the report for yesterday." & vbCrLf & vbCrLf
        ' 将光标移至文本末尾,再粘贴图片
        wdDoc.Range(wdDoc.Range.End - 1).Select
        wdDoc.Application.Selection.PasteAndFormat Type:=12 ' 12对应wdChartPicture的常量值
        
        .Display ' 可选:自动打开邮件窗口,方便查看效果
    End With
    
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
    Set Email = Nothing
    Set olApp = Nothing
    Set wdDoc = Nothing
    
    MsgBox ("Done!")
End Sub

关键修改说明

  1. 统一用WordEditor操作内容:放弃.Body,直接通过wdDoc.Range.Text设置正文,适配富文本模式的规则,确保内容正常显示。
  2. 调整图片粘贴位置:添加完文本后将光标移至末尾,避免图片覆盖正文内容。
  3. 替换常量为数值:用12替代wdChartPicture,无需额外引用Word对象库即可正常运行;若想保留常量,可在VBA编辑器的「工具→引用」中勾选「Microsoft Word xx.x Object Library」。
  4. 新增.Display方法:自动打开邮件窗口,方便直接验证效果,不用手动查找草稿。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 18:35:12