Discussion:
Copy message body with formatting from Outlook to Word.
(too old to reply)
w***@gmail.com
2009-03-20 21:30:08 UTC
Permalink
I am new to Outlook VBA (experienced with Excel) and am trying to
develop a custom print style for Outlook 2003 as layed out in MS Press
"Outlook 2003 Inside Out" Chapter 26.

I've made a word document template with several text fields that I am
populating with information from VBA. It works pretty slick except
for the fact that the message body is being copied from Outlook to
Word as simple text using the .Body property. This looks pretty bad
when there is formatting (tables, etc) in the email.

I am struggling to find a method to copy the message body from an
outlook message item into a word field including the formatting. I am
fumbling with the .WordEditor property but getting nowhere. Can
someone please shed some light on how I can accomplish the copying
with formatting task?

Thank You
Ken Slovak - [MVP - Outlook]
2009-03-23 13:08:47 UTC
Permalink
WordEditor is a Word.Document object. It only applies if WordMail is being
used, otherwise it's null.

The HTMLBody property of the item will have all the formatting, but it's
HTML that includes formatting tags in addition to the actual text. You can
grab HTMLBody, but that also includes all sorts of header information and
other HTML that you may or may not want to capture. If you don't want that
stuff you need to get HTMLBody as a string value and parse it using string
functions to only use the parts between the <body> </body> tags.

One method that can be used is to take the item and save it using the SaveAs
method as an HTML file. You can then open that file using Word, as a
separate Document object, and manipulate things that way.

One other thing to bear in mind is the capacity of whatever field you are
using to place the data from the item into Word. A large amount of text may
be more than the capacity of whatever you are using as the marker for that
data.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by w***@gmail.com
I am new to Outlook VBA (experienced with Excel) and am trying to
develop a custom print style for Outlook 2003 as layed out in MS Press
"Outlook 2003 Inside Out" Chapter 26.
I've made a word document template with several text fields that I am
populating with information from VBA. It works pretty slick except
for the fact that the message body is being copied from Outlook to
Word as simple text using the .Body property. This looks pretty bad
when there is formatting (tables, etc) in the email.
I am struggling to find a method to copy the message body from an
outlook message item into a word field including the formatting. I am
fumbling with the .WordEditor property but getting nowhere. Can
someone please shed some light on how I can accomplish the copying
with formatting task?
Thank You
w***@gmail.com
2009-03-23 14:45:38 UTC
Permalink
On Mar 23, 7:08 am, "Ken Slovak - [MVP - Outlook]"
Post by Ken Slovak - [MVP - Outlook]
WordEditor is a Word.Document object. It only applies if WordMail is being
used, otherwise it's null.
The HTMLBody property of the item will have all the formatting, but it's
HTML that includes formatting tags in addition to the actual text. You can
grab HTMLBody, but that also includes all sorts of header information and
other HTML that you may or may not want to capture. If you don't want that
stuff you need to get HTMLBody as a string value and parse it using string
functions to only use the parts between the <body> </body> tags.
One method that can be used is to take the item and save it using the SaveAs
method as an HTML file. You can then open that file using Word, as a
separate Document object, and manipulate things that way.
One other thing to bear in mind is the capacity of whatever field you are
using to place the data from the item into Word. A large amount of text may
be more than the capacity of whatever you are using as the marker for that
data.
--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.http://www.slovaktech.com/products.htm
Post by w***@gmail.com
I am new to Outlook VBA (experienced with Excel) and am trying to
develop a custom print style for Outlook 2003 as layed out in MS Press
"Outlook 2003 Inside Out" Chapter 26.
I've made a word document template with several text fields that I am
populating with information from VBA.  It works pretty slick except
for the fact that the message body is being copied from Outlook to
Word as simple text using the .Body property.  This looks pretty bad
when there is formatting (tables, etc) in the email.
I am struggling to find a method to copy the message body from an
outlook message item into a word field including the formatting.  I am
fumbling with the .WordEditor property but getting nowhere.  Can
someone please shed some light on how I can accomplish the copying
with formatting task?
Thank You
Well, I got something working over the weekend for RTF format email.
This is not an elegant or polished solution but it works well for me.

Sub PrintCustomMessageFormat()
'Modification to Outlook 2003 Inside Out Code sample

'Set up objects
Dim strTemplate As String
Dim objWord As Word.Application
Dim objDocs As Word.Documents
Dim objWordDocEditor As Word.Document
Dim mybklist As Word.Bookmarks

Dim objApp As Application
Dim objItem As MailItem
Dim objFolder As MAPIFolder 'WP
Dim objNS As NameSpace


Dim folderName As String
Dim strAttachments As String
'Create a Word document and current message item object
Set objApp = CreateObject("Outlook.Application")

Set objNS = objApp.GetNamespace("MAPI")
'Check to ensure Outlook item is selected

If TypeName(objApp.ActiveInspector) = "Nothing" Then
MsgBox "Message not open. Exiting", vbOKOnly + vbInformation,
"Outlook Inside Out"
Exit Sub
End If


Set objItem = objApp.ActiveInspector.CurrentItem
'WP: Add to get current folder
Set objFolder = objApp.ActiveExplorer.CurrentFolder


folderName = objFolder.FolderPath
If objItem.Attachments.Count <> 0 Then
For Each Attachment In objItem.Attachments
strAttachments = strAttachments + Attachment.FileName
+ vbCrLf

Next
End If

Set objWord = CreateObject("Word.Application")
strTemplate = "r:\EmailCustomPrintFormat2.dot"
Set objDocs = objWord.Documents
objDocs.Add strTemplate
Set mybklist = objWord.ActiveDocument.Bookmarks
'Fill in the form
objWord.ActiveDocument.Bookmarks("Subject").Select
objWord.Selection.TypeText CStr(objItem.Subject)

objWord.ActiveDocument.Bookmarks("From").Select
objWord.Selection.TypeText CStr(objItem.SenderName)
objWord.ActiveDocument.Bookmarks("DateSent").Select
objWord.Selection.TypeText CStr(objItem.SentOn)
objWord.ActiveDocument.Bookmarks("Received").Select
objWord.Selection.TypeText CStr(objItem.ReceivedTime)
objWord.ActiveDocument.Bookmarks("To").Select
objWord.Selection.TypeText CStr(objItem.To)

objWord.ActiveDocument.Bookmarks("folderName").Select
objWord.Selection.TypeText CStr(folderName)

objWord.ActiveDocument.Bookmarks("Attachments").Select
objWord.Selection.TypeText strAttachments

objWord.Visible = True

If objItem.GetInspector.EditorType = olEditorWord Then
'Word format email so try to copy with formatting
'http://www.outlookcode.com/article.aspx?id=31
'http://support.microsoft.com/kb/212682
Set objWordDocEditor = objItem.GetInspector.WordEditor
objWordDocEditor.Range.Copy
objWord.ActiveDocument.Bookmarks("Body").Select
'objWord.ActiveDocument.Bookmarks("Body").Range.InsertAfter
(vbCrLf)
' overwrites whole doc objWord.ActiveDocument.Range.Paste
objWord.Selection.Paste
'objWord.Selection.FormattedText = objWordDocEditor.Range
Else
objWord.ActiveDocument.Bookmarks("Body").Select
objWord.Selection.TypeText objItem.Body

End If


If MsgBox("Continue?", vbYesNo, "Continue") = vbYes Then

'Print and exit
objWord.PrintOut Background:=True
'Process other system events until printing is finished
While objWord.BackgroundPrintingStatus
DoEvents
Wend
End If

objWord.Quit SaveChanges:=wdvbaDoNotSaveChanges

'WP additions
Set objWord = Nothing
Set objApp = Nothing

End Sub

Loading...