Reputation: 11
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. :)
Upvotes: 0
Views: 901
Reputation: 11
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
Upvotes: 0
Reputation: 7850
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
Upvotes: 1