VBA in Word 2010 to copy formatted text to email message

lorraine
NewLounger
Posts: 9
Joined: 09 Feb 2011, 22:51

VBA in Word 2010 to copy formatted text to email message

Post by lorraine »

Hi everyone

I have the following vba code which worked in all previous versions of Word to copy text to an email message and keep formatting:

Code: Select all

strBody = ActiveDocument.HTMLProject.HTMLProjectItems(1).text
Apparently the HTMLProject is no longer supported. I can't use the feature of sending this document to an email message as only part of the text of a document is grabbed, then placed into a new doc which is saved as html.

I have found suggestions to use DataObject or convert to XML then back to html, but unsure how to do this. Can anyone please provide code which will do the same as the above?

I posted this on msdn yesterday but have received no replies as yet.

Many thanks

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA in Word 2010 to copy formatted text to email message

Post by HansV »

The HTMLProject object was deprecated in Word 2007.
You might use something like

ActiveDocument.SaveAs2 FileName:="MyDoc.htm", FileFormat:=wdFormatFilteredHTML

to save the document as a HTML file, then get the text from the HTML file.
Best wishes,
Hans

lorraine
NewLounger
Posts: 9
Joined: 09 Feb 2011, 22:51

Re: VBA in Word 2010 to copy formatted text to email message

Post by lorraine »

Thanks Hans, I have code similar to yours to save the html - I need the code to grab the formatted text.

This is my code:

Code: Select all

ActiveDocument.SaveAs FileName:="HTMLDoc.html", FileFormat:=wdFormatHTML
strBody = ActiveDocument.HTMLProject.HTMLProjectItems(1).text
ActiveDocument.Saved = True
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
It is the second line which no longer works in Word 2010:

Code: Select all

strBody=ActiveDocument.HTMLProject.HTMLProjectItems(1).text
Cheers
Lorraine

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA in Word 2010 to copy formatted text to email message

Post by HansV »

I'd choose a different approach: in Outlook 2007 and 2010, you can always use Word to edit the content of an email message.

For example:

Code: Select all

    Dim objOutlook As Outlook.Application
    Dim objMailItem As Outlook.MailItem
    Dim objInspector As Outlook.Inspector
    Dim docSource As Document
    Dim docTarget As Document
    Set objOutlook = GetObject(, "Outlook.Application")
    Set objMailItem = objOutlook.CreateItem(olMailItem)
    Set objInspector = objMailItem.GetInspector
    Set docSource = ActiveDocument
    Set docTarget = objInspector.WordEditor
    docSource.Content.Copy
    docTarget.Content.Paste
    objMailItem.Display
This will copy the formatted text of the active Word document into the body of a new email message in Outlook. There is no need for an intermediary XML or HTML file. Hopefully you can adapt this to your needs. (This example assumes that Outlook is already running, but you can easily change it to start Outlook if necessary)
Best wishes,
Hans

lorraine
NewLounger
Posts: 9
Joined: 09 Feb 2011, 22:51

Re: VBA in Word 2010 to copy formatted text to email message

Post by lorraine »

Thanks so much Hans. I can't actually use that approach as my email message is composed with other information collected from our database. Here's the code I've now found successful, albeit clunky compared to the previous one liner! Could be helpful if you need to copy only part of a part of a document to email message. Now I have my "strBody" to incorporate with other info from the database:

Code: Select all

'copy part of document to email message and retain formatting
Selection.GoTo What:=wdGoToBookmark, Name:="obmkEmailStartText"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

Selection.Extend
Selection.GoTo What:=wdGoToBookmark, Name:="obmkEmailEndText"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.Copy
Word.Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:="HTMLDoc.html", FileFormat:=wdFormatHTML
ActiveDocument.BuiltInDocumentProperties.item("Title").Value = ""
'strBody = ActiveDocument.HTMLProject.HTMLProjectItems(1).text
'22 Sep 2011 HTMLProject deprecated in Office 2010 so the following replaces above line
ActiveDocument.Saved = True
ActiveDocument.Close SaveChanges:=wdSaveChanges
Open "HTMLDoc.html" For Input As #1
Dim htmlString As String
htmlString = ""

Do While Not EOF(1)
    htmlString = htmlString & Input(1, #1)
Loop

strBody = htmlString
Hans, I really appreciate your help. You are invaluable!

Cheers
Lorraine

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA in Word 2010 to copy formatted text to email message

Post by HansV »

I don't know how you manipulate the message body, but using the WordEditor object, you can edit the message body the same way you can edit a Word document: find, replace, insert text, etc.

But I'm glad you've found a workaround.
Best wishes,
Hans