Inserting HTML into word 2003 using VB.NET

S

Sylvain

Hi,

I'm trying to insert simple html code into a word document
programmatically, the only way I could think of doing this was by
adding the html code to the clipboard, and then pasting it in word
using an html format. selection.insertXML exists, but only inserts
wordML, and I couldn't find any insertHTML other than with the
pasteSpecial.

However if I have anything above 16 characters (depending on the html
tags), I get a (0x800A1066): command failed error. Here is a link
http://www.dotnet247.com/247reference/msgs/50/254902.aspx that I based
my code on, and his example works fine, but try adding over 20
characters instead of "test" <h1>test<h1> and this error will pop up.

Here is what I've done:

'Dim encoder As UTF8Encoding = New UTF8Encoding
'Dim tempStr As String = "123456789012345678901234567890" <- this won't
work
'--------------------------------------------------------

Dim tempStr As String = "test 1<br>test 2"

Dim htmlText As String = "Version:0.9" & vbCrLf & "StartHTML:-1" &
vbCrLf & "EndHTML:-1" & vbCrLf & "StartFragment:000076" & vbCrLf &
"EndFragment:000128" & vbCrLf & "<!DOCTYPE>" & vbCrLf &
"<HTML><BODY><p>" & tempStr & "</p></BODY></HTML>"

Dim clipDO As New System.Windows.Forms.DataObject

clipDO.SetData(System.Windows.Forms.DataFormats.Html, htmlText)

System.Windows.Forms.Clipboard.SetDataObject(clipDO, True)

oApplication.Selection.PasteSpecial(, , , ,
word.WdPasteDataType.wdPasteHTML)

'--------------------------------------------------------

If anyone knows how to successfully insert html code in word, please
let me know, I would greatly appreciate it.

Thank you,

sylvain.
 
D

Don

Hi,

I'm trying to insert simple html code into a word document
programmatically, the only way I could think of doing this was by
adding the html code to the clipboard, and then pasting it in word
using an html format. selection.insertXML exists, but only inserts
wordML, and I couldn't find any insertHTML other than with the
pasteSpecial.

However if I have anything above 16 characters (depending on the html
tags), I get a (0x800A1066): command failed error. Here is a link
http://www.dotnet247.com/247reference/msgs/50/254902.aspx that I based
my code on, and his example works fine, but try adding over 20
characters instead of "test" <h1>test<h1> and this error will pop up.

Here is what I've done:

'Dim encoder As UTF8Encoding = New UTF8Encoding
'Dim tempStr As String = "123456789012345678901234567890" <- this won't
work
'--------------------------------------------------------

Dim tempStr As String = "test 1<br>test 2"

Dim htmlText As String = "Version:0.9" & vbCrLf & "StartHTML:-1" &
vbCrLf & "EndHTML:-1" & vbCrLf & "StartFragment:000076" & vbCrLf &
"EndFragment:000128" & vbCrLf & "<!DOCTYPE>" & vbCrLf &
"<HTML><BODY><p>" & tempStr & "</p></BODY></HTML>"

Dim clipDO As New System.Windows.Forms.DataObject

clipDO.SetData(System.Windows.Forms.DataFormats.Html, htmlText)

System.Windows.Forms.Clipboard.SetDataObject(clipDO, True)

oApplication.Selection.PasteSpecial(, , , ,
word.WdPasteDataType.wdPasteHTML)

'--------------------------------------------------------

If anyone knows how to successfully insert html code in word, please
let me know, I would greatly appreciate it.

Thank you,

sylvain.

There's an option (almost like a "remark") for displaying html in a web
page (most do it for instructional purposes).
I'm not sure if it would function the same in Word or not?
I seem to recall the Word and/or MS reaplces the tags with {; or
something similar.

I have a notation of the method saved someplace, however since I've never
had a need to use it? I was unable to locate the "hack".
Tried a couple of google's and was apprently not using the correct terms
and didn't feel like wading through hundreds of pages.
 
S

Sylvain

What I ended up doing is simply create an html file (u have to add
<html><body>...</body></html> or else it won't work even if your htm
file appears fine in IE), and inserted it into word using the
insertFile() method. That seems to work for me.

Thanks.
 
Top