用文本和图像填充邮件项目

我在Excel VBA中有以下代码:

Sub CreateEmailAndSend() Dim outApp As Object Dim OutMail As Object Set outApp = CreateObject("Outlook.Application") Set oMail = outApp.CreateItem(0) Dim Doc As Object oMail.Display Set Doc = outApp.ActiveInspector.WordEditor oMail.To = "" oMail.Subject = "test" ' first sentence Dim msg As String msg = "Plain Sentence" Doc.Range(0, 0) = msg ' second sentence comes after msg = "Bold and Highlight Yellow Sentence" Doc.Range(Len(Doc.Range), Len(Doc.Range)) = msg Doc.Range.Font.Bold = True Doc.Range.HighlightColorIndex = wdYellow ' paste image below it Dim imagerng As Range Set imagerng = Range(Cells(1, 1), Cells(5, 5)) imagerng.CopyPicture Appearance:=xlScreen, Format:=xlBitmap Doc.Range(Len(Doc.Range), Len(Doc.Range)).Paste End Sub 

基本上我想要做的是创build并显示一个如下所示的电子邮件:

简单的句子
粗体和突出显示黄色句子(该句粗体突出显示)
(位图图像​​)
{我的签名}

但是,我从我的代码得到的输出是

简单句子(粗体)
(位图图像​​)和第二个句子全部{我的签名}

我应该如何解决我的代码?

我认为你的问题是你试图访问Word对象模型中的范围的方式。 一些谷歌search后,我已经取代了你的Doc.Range(Len(Doc.Range), Len(Doc.Range)). 段落引用部分。 见下文:

 Sub CreateEmailAndSend() Dim outApp As Object Dim OutMail As Object Set outApp = CreateObject("Outlook.Application") Set oMail = outApp.CreateItem(0) Dim Doc As Object oMail.Display Set Doc = outApp.ActiveInspector.WordEditor oMail.To = "" oMail.Subject = "test" ' first sentence Dim msg As String msg = "Plain Sentence" Doc.Range(0, 0) = msg ' second sentence comes after msg = "Bold and Highlight Yellow Sentence" Doc.Paragraphs(1).Range.InsertParagraphAfter Doc.Paragraphs(2).Range = msg Doc.Paragraphs(2).Range.Font.Bold = True Doc.Paragraphs(2).Range.HighlightColorIndex = wdYellow ' paste image below it Dim imagerng As Range Set imagerng = Range(Cells(1, 1), Cells(5, 5)) imagerng.CopyPicture Appearance:=xlScreen, Format:=xlBitmap Doc.Paragraphs(2).Range.InsertParagraphAfter Doc.Paragraphs(3).Range.InsertParagraphAfter Doc.Paragraphs(3).Range.Paste End Sub 

这现在正在为我工​​作。