【问题标题】:How to send a Word document as body of an email with VBA如何使用 VBA 将 Word 文档作为电子邮件正文发送
【发布时间】:2016-06-07 03:41:19
【问题描述】:

我创建了一个适用于 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

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

【问题讨论】:

  • 一方面,您将 Word 文档的内容作为字符串(在变量 MsgTxt 中)获取,但根据定义不包含任何图像。

标签: vba excel ms-word outlook


【解决方案1】:

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

类似这样的:

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

【讨论】:

  • 你就是那个男人。我想知道为什么您需要 .Display 才能真正正常运行。如果没有 .Display,它将发送电子邮件但没有正文。谢谢帕特里克
  • 非常奇怪 - 此解决方案.Send 一起使用,因此对于自动发送电子邮件来说不是可行的解决方案
  • @urdearboy 是的,我刚想到这个,可能目前唯一的快速解决方案是将.Display.Send 放在一起来完成这项工作。
【解决方案2】:

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

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

【讨论】:

  • 我发现这对msdn.microsoft.com/en-us/library/… 有点帮助。使用 Inspector.WordEditor 您可以“创建带有嵌入图像的格式化电子邮件”。但它并没有告诉你如何,我已经尝试在谷歌上寻找 WordEditor 功能,但我找不到它
  • Inspector.WordEditor 属性返回 Word.Document 对象 - msdn.microsoft.com/en-us/library/bb211897(v=office.12).aspx - 但我认为它不会让您导入新的 DOC 文件来代替现有文档。
  • 我没有导入新的 DOC 文件。我正在打开一个现有文件
  • 如果您有一个与特定 Outlook 邮件的邮件正文相对应的 Word.Document 对象,则需要将您的 DOC 文件导入到该文档中。
猜你喜欢
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-14
  • 2015-10-02
  • 2020-08-15
  • 2019-08-29
相关资源
最近更新 更多