Share via


Is there a way to paste HTML into Word and have it rendered?

Question

Wednesday, November 8, 2017 4:27 AM

I'm working on a script that reads in an XML Document and pastes the contents into Word in a nicely formatted, readable, presentable format. Or, that's the idea anyway. Most of it works. One issue I'm having is with tables.

The tables in the XML source are basically just HTML tables. If I dump the HTML code into an HTML file, then load it in a web browser, it all looks fine. For a simple table, with no merged cells, that's easy enough to populate into Word as well.

The issue I'm having is with documents that have tables with merged cells. So it's not a simple 5x5 table or 3x5 table, etc. The only way I can think of to accommodate this is to parse the HTML code to build the table row by row, and merge cells as I do. That is not a prospect I find appealing.

Is there a way, through PowerShell, to programatically paste the HTML source into the Word document so it recognizes and renders the HTML code? Or is there some other easier way to do this so I don't have to tear apart each <tr> and <td> element to build the table piece by piece?

All replies (6)

Thursday, November 9, 2017 4:14 AM ✅Answered | 1 vote

What have you tried?  Word can receive pasted HTML but it has to be pasted as HTML and not text.

Look up how to use the object model to paste into Word.

Did you try this one?

https://stackoverflow.com/questions/161519/how-do-i-insert-html-formatted-strings-into-a-microsoft-word-document-using-visu

or this one:

https://www.experts-exchange.com/questions/26240969/Copy-HTML-Code-To-Word-using-VBA.html

or this one:

https://stackoverflow.com/questions/27854534/how-do-i-insert-html-to-word-using-vba

\(ツ)_/


Wednesday, November 8, 2017 4:36 AM

Search for "powershell word automation" to find many examples.

Look in the Gallery for complete scripts.

\(ツ)_/


Wednesday, November 8, 2017 8:56 AM

Yes, I have, thank you. I can't find examples for what I'm asking.


Wednesday, November 8, 2017 12:36 PM

A simple search finds hundreds of examples:

https://www.google.com/search?newwindow=1&source=hp&ei=B_oCWr_7AcTDmwGpgpqwCQ&q=powershell+insert+html+into+word+document&oq=powershell+insert+html+into+word+&gs_l=psy-ab.1.0.33i22i29i30k1.3654.17235.0.21202.36.35.0.0.0.0.182.4422.3j31.34.0....0...1.1.64.psy-ab..2.34.4417.0..0j0i131k1j0i10k1j0i22i30k1.0.U3KhEJDAfLg

\(ツ)_/


Thursday, November 9, 2017 4:04 AM

None of which address the specifics of my question.


Thursday, November 9, 2017 6:00 AM

That first link got me on the right track.

That gave me the key, and I had to look up a couple more bits to get it working properly. I ended up with these three lines of code:

Set-Clipboard $tagtext -AsHtml # $tagtext is the HTML code for the table

$selection = $wordfile.selection # I'm building the word file as I go, so I want to paste
                                 #    at the current position.

$selection.PasteSpecial() ,,,,10 # 10 is the numeric value for wdPasteHTML

Thank you very much for your help.