Insert html text with VBA

P

Paul Shapiro

I have an application that inserts formatted text to Word from plain text
that includes custom format tags. The application is being updated to take
html-formatted text. I was hoping that inserting html text to a Word
document would be more straightforward than trapping all the tags and
replacing them with Word styles. I could not find anything to insert html
into a Word document and have the html tags interpreted as formatting. When
I insert the text, the html tags are included as literal text. I know you
can copy html-formatted text from IE and paste it into Word with the
formatting intact. Is there a clean way to do this in Word VBA? I would hate
to have to create an html document from the text, open it in IE, select it
all, copy it and then paste to Word. But that's all I can find so far. Any
suggestions?
 
F

fumei via OfficeKB.com

Text is text. Word does not know that tags are anything but...well, text.

Yes, you can copy content from IE and paste it into Word, but to say the
format is intact is not quite accurate. Word will interpret it as best as it
can.

Is there a clean way with VBA? No. Text is text. <whatever>blah</whatever>
is no more meaningful to Word than <bold>blah</bold>. They are both text.

There many many threads in many forums on this subject. How to "convert"
HTML tags (as text) to true Word style formatting. None, as far as I have
seen, have suggestion to make this "clean". Some suggestions work reasonably
well, but nothing that is rock solid.

Even copying from IE is, in true Word concepts, very very ugly. It creates
multitudes of Normal + Char format type styles. Bleeech.
 
P

Pede

I have had a similar challenge (in Word 2003). My html is in a file, and I
import it into the Word-document with the .InsertFile method, which converts
the html formatting into equivalent formatting in Word, at least the tags
which were relevant to me: <P><br><ul><style><b>. The VBA code is only:

Set m_docRange = doc.Bookmarks("bodyText").Range
Call m_docRange.InsertFile(m_htmlFile, , False)

"bodyText" is a bookmark, I have defined in the .dot file
 

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