How to insert HTML formatted strings into a Microsoft Word document using Visual Basic while maintaining formatting?

I am using Visual Basic and the automation interface to retrieve strings from an external application. These lines contain simple html formatting codes (<b>, <i>, etc.). Is there any simple function in Visual Basic for Word to insert these lines into a Word document and convert html formatting codes to word formatting?

+7
html vba ms-word word-vba
source share
4 answers

Here is the link for adding HTML to the clipboard using VB:

http://support.microsoft.com/kb/274326

Once you have the HTML in the clipboard, paste it into your doc word using something like this:

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

This is pretty much equivalent to cutting and pasting it manually.

+13
source share

Use InsertFile

 Set objdoc = objInsp.WordEditor Set objword = objdoc.Application Set objsel = objword.Selection objsel.WholeStory vs_html = "<html><body>" + vs_body + "</body></html>" vs_file = "C:\temp\1.html" Call DumptoFile(vs_file, "", vs_html, False) RetVal = objsel.InsertFile(vs_file, , , False, False) 
+1
source share

AFAIK there is no built-in function to do this in VBA. You will have to write it yourself, which would not be too difficult if you calm him down, for example, to analyze <b>, <i>, <a> and <p>, All other tags should be ignored.

0
source share

I am using 2016. The only thing that worked was Range.InsertFile (path). Special insert did not work.

0
source share

All Articles