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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35609112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:15:04  来源:igfitidea点击:

How to send a Word document as body of an email with VBA

excelvbaexcel-vbams-wordoutlook

提问by Justin Case

I've created a macro that works with outlook and excel that will use a list of email addresses (in excel) and send all those addresses an email (in outlook). I want to take a word document (from microsoft word) and use it as the body of the email. The problem is, I will have images in the word document and I need the word document to keep it's formatting. Right now, my VBA takes the content of my word document but the formatting is gone and images aren't included. This is my code:

我创建了一个适用于 Outlook 和 excel 的宏,它将使用电子邮件地址列表(在 excel 中)并将所有这些地址发送一封电子邮件(在 Outlook 中)。我想获取一个 word 文档(来自 microsoft 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

I've searched all over google for a solution but I haven't found one. How can I send a word document as the body of the email with it's formatting in tact and the images included?

我已经在谷歌上搜索了一个解决方案,但我还没有找到。如何发送一个 Word 文档作为电子邮件的正文,格式正确并包含图像?

回答by Patrick Wynne

You are getting the contents of your template document as a string, which by definition will not contain any formatting or images. You should instead copy the contents to the clipboard and then paste them into the new email.

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

Something like this:

像这样的东西:

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

回答by Dmitry Streblechenko

If not for the images, you could save the document as an HTML file, read its contents, then set the MailItem.HTMLBody property.

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

Images are more involved. I don't see a straightforward way to bring them into a message body.

图像涉及更多。我没有看到将它们带入消息正文的直接方法。