0
votes

I have created a mail merge with attachment in word vba. Everything is working fine just formatting is lost when macro is run and mail is sent to outlook; I am getting plain simple text. Code below for reference.

For j = 1 To Source.Sections.Count - 1
    Set oItem = oOutlookApp.CreateItem(olMailItem)
    With oItem
         ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True
        .Subject = mysubject & mysubsuffix
        .Body = Source.Sections(j).Range.Text

        Set Datarange = Maillist.Tables(1).Cell(j, 1).Range
        Datarange.End = Datarange.End - 1
        .To = Datarange

       Set Datarange = Maillist.Tables(1).Cell(j, 2).Range
        Datarange.End = Datarange.End - 1
        .CC = Datarange.Text


        For i = 3 To Maillist.Tables(1).Columns.Count
            Set Datarange = Maillist.Tables(1).Cell(j, i).Range
            Datarange.End = Datarange.End - 1
            .Attachments.Add Trim(Datarange.Text), olByValue, 1
        Next i
        .Send
    End With
    Set oItem = Nothing
Next j
Maillist.Close wdDoNotSaveChanges

Thanks in advance. :)

2
You are using Body which is the text body. You need to use HtmlBody if you want formatting. Also, you are not copying any formatting to the email body.Tony Dallimore
I suggest you look at Ron de Bruin's RangeToHtml. This function converts an Excel range to an Html document. This function relies on Microsoft's PublishObjects so the quality of the CSS is poor but Outlook will cope.Tony Dallimore

2 Answers

1
votes

The properties Body and HTMLBody are both string values so contain no formatting. You can only get HTMLBody to provide formatted output if you include the HTML tags in the string that you assign to it.

See this question

0
votes

Thanks for responding all.

I was able to achieve through .Body it itself. As I was pulling body content from word document and it will be totally dynamic plus loop for mail merge, not sure it HTML would be helpful in this case.

My updated code:

 ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True
 Set editor = .GetInspector.WordEditor
.BodyFormat = olFormatRichText
.Body = Source.Sections(j).Range.Text
Source.Content.Sections(j).Range.Copy
editor.Content.Paste
editor.Close