Tom Collins
Tom Collins

Reputation: 4069

Pasting HTML into Word using VBA

I need to paste 6000 HTML strings into Word as formatted using VBA. I can do this if I insert an HTML file, but would rather do this with passing a string.
This is the sample HTML I'm using:

<html><body>normal <b>bold</b><br></body></html>

I can get this to work:

Selection.InsertFile FileName:="html test file.htm"

Result: normal bold (this is what I want).

But not this (with the above HTML in the clipboard):

ActiveDocument.Range.PasteSpecial ,,,,WdPasteDataType.wdPasteHTML

With the later, I get error 5342 "the specified data type is unavailable".

Upvotes: 1

Views: 1884

Answers (1)

Tim Williams
Tim Williams

Reputation: 166126

The code posted by GMCB (accepted answer) here will do what you want, based on some short testing:

Injecting RTF code in the Clipboard to paste into MS Word as RTF text via a VBA macro

The main focus of that post is putting RTF-format text into the clipboard, but it also covers populating HTML-format.

Upvotes: 2

Related Questions