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

Office2016下Excel VBA导入Word类型OLEObject至Outlook邮件正文的代码语法

Solution to Insert Word OLEObject Content (Including Images) into Outlook Email Body via VBA (Office 2016)

I’ve worked through similar issues before—getting embedded Word content (especially with images) into Outlook emails can be tricky if you don’t leverage the Word object model properly. The key is to use the WordEditor of the Outlook mail item, which lets you paste content exactly as it appears in the source Word document, preserving images and formatting.

Early Binding Version (Requires Library References)

This version is faster and has intellisense support. First, go to the VBA Editor > Tools > References, then check:

  • Microsoft Word 16.0 Object Library
  • Microsoft Outlook 16.0 Object Library
Sub InsertOLEWordContentIntoOutlook()
    Dim oleObj As OLEObject
    Dim wordDoc As Word.Document
    Dim outlookApp As Outlook.Application
    Dim mailItem As Outlook.MailItem
    Dim wordEditor As Word.Document
    
    ' Set your target OLEObject (update sheet name and object name)
    Set oleObj = ThisWorkbook.Sheets("Sheet1").OLEObjects("EmbeddedWordDoc")
    
    ' Verify the OLEObject is a Word document
    If Not oleObj.ProgID Like "*Word.Document*" Then
        MsgBox "Selected OLEObject is not a Word document!", vbExclamation
        Exit Sub
    End If
    
    ' Get the embedded Word document
    Set wordDoc = oleObj.Object
    
    ' Initialize Outlook
    Set outlookApp = New Outlook.Application
    Set mailItem = outlookApp.CreateItem(olMailItem)
    
    ' Set email details
    mailItem.Subject = "Embedded Word Document Content"
    mailItem.To = "recipient@example.com"
    
    ' Access the email's Word editor to preserve formatting/images
    Set wordEditor = mailItem.GetInspector.WordEditor
    
    ' Copy all content from the embedded Word doc
    wordDoc.Content.Copy
    
    ' Paste content into the email body with original formatting
    wordEditor.Content.PasteAndFormat wdFormatOriginalFormatting
    
    ' Clear clipboard
    Application.CutCopyMode = False
    
    ' Clean up (critical to avoid hanging Word/Outlook processes)
    wordDoc.Close SaveChanges:=wdDoNotSaveChanges
    Set wordDoc = Nothing
    Set oleObj = Nothing
    Set wordEditor = Nothing
    Set mailItem = Nothing
    Set outlookApp = Nothing
    
    ' Optional: Display the email
    ' mailItem.Display
End Sub

Late Binding Version (No Library References Needed)

Use this if you want the code to work across different Office versions without setting references:

Sub InsertOLEWordContentIntoOutlook_LateBinding()
    Dim oleObj As OLEObject
    Dim wordDoc As Object
    Dim outlookApp As Object
    Dim mailItem As Object
    Dim wordEditor As Object
    
    ' Set your target OLEObject (update sheet name and object name)
    Set oleObj = ThisWorkbook.Sheets("Sheet1").OLEObjects("EmbeddedWordDoc")
    
    ' Verify the OLEObject is a Word document
    If Not oleObj.ProgID Like "*Word.Document*" Then
        MsgBox "Selected OLEObject is not a Word document!", vbExclamation
        Exit Sub
    End If
    
    ' Get the embedded Word document
    Set wordDoc = oleObj.Object
    
    ' Initialize Outlook
    Set outlookApp = CreateObject("Outlook.Application")
    Set mailItem = outlookApp.CreateItem(0) ' 0 = olMailItem
    
    ' Set email details
    mailItem.Subject = "Embedded Word Document Content"
    mailItem.To = "recipient@example.com"
    
    ' Access the email's Word editor
    Set wordEditor = mailItem.GetInspector.WordEditor
    
    ' Copy all content from the embedded Word doc
    wordDoc.Content.Copy
    
    ' Paste content (wdFormatOriginalFormatting = 16)
    wordEditor.Content.PasteAndFormat 16
    
    ' Clear clipboard
    Application.CutCopyMode = False
    
    ' Clean up
    wordDoc.Close SaveChanges:=0 ' 0 = wdDoNotSaveChanges
    Set wordDoc = Nothing
    Set oleObj = Nothing
    Set wordEditor = Nothing
    Set mailItem = Nothing
    Set outlookApp = Nothing
    
    ' Optional: Display the email
    ' mailItem.Display
End Sub

Common Pitfalls to Avoid

  • Not cleaning up objects: Failing to set objects to Nothing can leave hidden Word/Outlook processes running in the background. Always include the cleanup steps.
  • Trying to use HTMLBody directly: The HTMLBody property won’t handle embedded images from Word correctly—using the WordEditor is the only reliable way to preserve images and formatting.
  • Incorrect OLEObject reference: Double-check the sheet name and OLEObject name (you can find the OLEObject name in the Excel Developer tab > Design Mode > select the object > look at the Name box in the top-left).

内容的提问来源于stack exchange,提问作者Riccardo La Marca

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:27:50