如何使用VBA将Word文档作为电子邮件的主体发送

我创build了一个与Outlook和Excel一起使用的macros,它将使用电子邮件地址列表(在Excel中),并通过电子邮件(在Outlook中)发送所有这些地址。 我想从word文档中提取word文档,并将其用作电子邮件的正文。 问题是,我将在word文档中有图像,我需要word文档来保持它的格式。 现在,我的VBA采取我的Word文档的内容,但格式化已经消失,图像不包括在内。 这是我的代码:

Sub spamEmail() 'Setting up the Excel variables. Dim olApp As Object Dim oMail As Outlook.MailItem Dim iCounter As Integer Dim Dest As Variant Dim SDest As String Dim Excel As Object Dim Name As String Dim Word As Object Dim oAccount As Outlook.Account Dim doc As Word.Document Dim itm As Object Dim MsgTxt As String 'Set the outlook account to send. Set oAccount = Application.Session.Accounts.Item(2) 'Create excel object. Set Excel = CreateObject("excel.application") Excel.Visible = True Excel.Workbooks.Open ("C:\Users\Deryl Lam\Documents\Book1.xlsx") Excel.Workbooks("Book1.xlsx").Activate 'Create a word object. Set Word = CreateObject("word.application") Set doc = Word.Documents.Open _ (FileName:="C:\Users\Deryl Lam\Documents\emailBody.docx", ReadOnly:=True) 'Pulls text from file for message body MsgTxt = doc.Range(Start:=doc.Paragraphs(1).Range.Start, _ End:=doc.Paragraphs(doc.Paragraphs.Count).Range.End) 'Loop through the excel worksheet. For iCounter = 1 To WorksheetFunction.CountA(Workbooks("Book1.xlsx").Sheets(1).Columns(1)) 'Create an email for each entry in the worksheet. Set oMail = Application.CreateItem(olMailItem) With oMail SDest = Cells(iCounter, 1).Value If SDest = "" Then 'Dont do anything if the entry is blank. Else 'Do additional formatting on the BCC and Subject lines, add the body text from the spreadsheet, and send. Name = Cells(iCounter, 2).Value .BCC = SDest .Subject = "FYI" .Body = "Dear " & Name & "," & vbCrLf & MsgTxt 'SendUsingAccount is new in Office 2007 'Change Item(1)to the account number that you want to use .SendUsingAccount = oAccount .Send End If End With Next iCounter 'Clean up the Outlook application. Set olMailItm = Nothing Set olApp = Nothing End Sub 

我search了谷歌的解决scheme,但我还没有find一个。 我怎样才能发送一个word文档作为电子邮件的正文格式和图像包括在内?

您将以string的forms获取模板文档的内容,根据定义,它将不包含任何格式或图像。 您应该将内容复制到剪贴板,然后将其粘贴到新电子邮件中。

像这样的东西:

 Sub emailFromDoc() Dim wd As Object, editor As Object Dim doc As Object Dim oMail As MailItem Set wd = CreateObject("Word.Application") Set doc = wd.documents.Open(...path to your doc...) doc.Content.Copy doc.Close set wd = Nothing Set oMail = Application.CreateItem(olMailItem) With oMail .BodyFormat = olFormatRichText Set editor = .GetInspector.WordEditor editor.Content.Paste .Display End With End Sub 

如果不是图像,您可以将文档保存为HTML文件,读取其内容,然后设置MailItem.HTMLBody属性。

图像更涉及。 我没有看到一个简单的方法将他们带入消息体。