DataObject.SetText and HTML

S

smay

I'm trying to pull strings of HTML from a database table and put them into
Bookmarks in my Word document while retaining the formatting (bold, italics,
bulleted lists, etc) from the HTML tags. The DataObject.SetText does not
recognize the string as HTML, and therefore when I use
ActiveDocument.Range.PasteSpecial DataType:=wdPasteHTML I get the error "The
specified data type is unavailable". I've tried wrapping the string in
<html> and <body> tags, but that doesn't help. I do not want to see the
tags, just the formatted text. Suggestions please!

Call the method below as:
UpdateBookmark 1, "<b>Some text</b>" OR
UpdateBookmark 1, "<html><body><b>Some text</b></body></html>"

Dim MyData As DataObject
Sub UpdateBookmark(BookmarkToUpdate As Integer, TextToUse As String)
Set MyData = New DataObject
MyData.SetText TextToUse
MyData.PutInClipboard

Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks("Goal" & BookmarkToUpdate).Range
BMRange.PasteSpecial DataType:=wdPasteHTML
End Sub
 
D

Doug Robbins - Word MVP

It would not be difficult to create a macro that iterated through a list of
tags and their associated formatting and then for each of the above
combinations, used the .Find command to locate each occurence, apply the
desired format and delete the tags

You will find some information on finding the tags at
http://word.mvps.org/FAQs/General/DeleteTags.htm

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 
P

Paul Shapiro

Instead of using DataObject, I've put the html onto the clipboard using
Windows API's, specifying that it's in html format, and then paste
it into the document. The API's to put html onto the clipboard are ugly, but
once done they seem to work well.

I've also used code that writes the html into a temporary file and then uses
the Range.InsertFile command to insert the html. That's pretty easy, but
doesn't get the formatting as reliably as the clipboard API.

I can send you a copy of the code if you email me.
 
S

smay

I got it to work using the code from:
http://support.microsoft.com/default.aspx?scid=kb;en-us;274326

Then I changed by code as below. The PutHTMLClipboard appends the header
variables required for the clipboard to recognize the string as HTML, which
in turn allowed me to use PasteSpecial with the wdPasteHTML DataType.

Thanks for the help.


Sub UpdateBookmark(BookmarkToUpdate As Integer, TextToUse As String, Paste
As Boolean)
If Paste And Len(Trim(TextToUse)) > 0 Then
PutHTMLClipboard TextToUse, "<HTML><BODY>", "</BODY></HTML>"
End If

Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks("Goal" & BookmarkToUpdate).Range

If Paste And Len(Trim(TextToUse)) > 0 Then

BMRange.PasteSpecial DataType:=wdPasteHTML
Else
BMRange.Text = TextToUse
End If

ActiveDocument.Bookmarks.Add "Goal" & BookmarkToUpdate, BMRange
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top