Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
682 views
in Technique[技术] by (71.8m points)

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

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:

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:UsersDeryl LamDocumentsBook1.xlsx")
   Excel.Workbooks("Book1.xlsx").Activate

   'Create a word object.
   Set Word = CreateObject("word.application")
   Set doc = Word.Documents.Open _
   (FileName:="C:UsersDeryl LamDocumentsemailBody.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...