如何在Outlook邮件文本中间插入Excel图片?VBA代码问题求助
Fix: Insert Excel Range Image Between HTML Body Text and Signature in Outlook VBA
I see exactly what's happening here—your current code collapses the Word document range to the very end (after the signature) before pasting the image, which is why it lands there instead of between your custom message and the signature. Let's fix this by adding a clear placeholder in your HTML body, then targeting that position to insert the Excel range exactly where you want it.
Here's the modified VBA code with step-by-step explanations:
Sub CopyRangeToOutlook_Client() Dim oLookApp As Outlook.Application Dim oLookItm As Outlook.MailItem Dim oLookIns As Outlook.Inspector Dim signature As String Dim oWrdDoc As Word.Document Dim oWrdRng As Word.Range Dim ExcRng As Range Dim strbody As String Dim placeholderText As String ' Define a unique placeholder to mark our image insertion spot placeholderText = "[INSERT_EXCEL_RATE_TABLE]" ' Get or initialize Outlook application On Error Resume Next Set oLookApp = GetObject(, "Outlook.Application") If Err.Number = 429 Then Err.Clear Set oLookApp = New Outlook.Application End If On Error GoTo 0 ' Reset error handling to catch future issues ' Temporary mail to capture default signature (required to load it properly) Set oLookItm = oLookApp.CreateItem(olMailItem) oLookItm.Display signature = oLookItm.HTMLBody oLookItm.Close olDiscard ' Close temp mail to avoid clutter ' Build HTML body with our placeholder where the image should go strbody = "<BODY style='font-size:14pt; color:rgb(96,97,96);'>" & _ "Dear Client,<p>I trust you are well.<p>" & _ "Please see below our weekly reference rates.<p>" & _ placeholderText & "<p>" & _ ' Image will replace this placeholder "Kind Regards" & _ "</BODY>" ' Create final mail item Set oLookItm = oLookApp.CreateItem(olMailItem) With oLookItm .To = "xxxxxxxxxxxxx.com" .CC = "xxxxxxxxxxxxx.com" .Subject = "xxxxxxxxxxxxxxxx // xxxxxxxxxxxxxxxx (Pty) Ltd - Weekly Reference Rate" & " - " & Format(Date, "(dd-mm-yyyy)") .HTMLBody = strbody & signature ' Merge custom text with signature .Display ' Show mail to access the Word editor ' Access the mail's underlying Word document Set oLookIns = .GetInspector Set oWrdDoc = oLookIns.WordEditor ' Locate our placeholder in the document Set oWrdRng = oWrdDoc.Content With oWrdRng.Find .Text = placeholderText .Forward = True .Wrap = wdFindStop .Execute End With ' Insert image if placeholder is found If oWrdRng.Find.Found Then oWrdRng.Collapse Direction:=wdCollapseStart ' Move cursor to start of placeholder ' Copy the Excel range Set ExcRng = Sheet3.Range("A1:E26") ExcRng.Copy ' Paste as metafile picture at the placeholder position oWrdRng.PasteSpecial DataType:=wdPasteMetafilePicture ' Clean up: delete the placeholder text oWrdRng.MoveRight Unit:=wdCharacter, Count:=Len(placeholderText) oWrdRng.Delete End If End With ' Clean up object references Set oWrdRng = Nothing Set oWrdDoc = Nothing Set oLookIns = Nothing Set oLookItm = Nothing Set oLookApp = Nothing Set ExcRng = Nothing End Sub
Key Improvements Explained:
- Placeholder-Based Insertion: The
[INSERT_EXCEL_RATE_TABLE]marker lets us precisely target where the image should go, so you won't have to guess about text positions later. - Reliable Signature Capture: We first create a temporary mail to load the default signature, then reuse it in the final mail—this avoids common issues with signatures not loading when building HTML bodies directly.
- Targeted Image Placement: Instead of pasting at the end of the entire document, we locate the placeholder, paste the image there, then remove the marker. This guarantees the image sits between your custom message and signature.
- Better Error Handling: Resetting error handling after initializing Outlook ensures we catch any unexpected issues later in the code.
内容的提问来源于stack exchange,提问作者Ryno Nel
相关产品推荐
相关产品推荐

