Problem pasting html into Word 2007

R

Randy

Hi. I need help. I am having problems pasting an html string into
Word 2007. The code (below) for Word Versions prior to 2007 (12)
works fine, but Word 2007 pastes the string as text with the
associated html tags displaying on the document. Any ideas on this?

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim htmlValue as String

htmlValue = "<html><body>Test</body></html>"

'Open MS-Word via OLE.
Set oWord = CreateObject("word.application")
Set oDoc = oWord.Documents.Add()

'get version of word
If oWord.Version = 12 Then
'Open a new document with the default template.
Dim clipDO As DataObject
clipDO.SetData htmlValue

Clipboard.SetText htmlValue


'Add text to the document.
'Displays with html tags on page.
oWord.Selection.Paste

Else
'This works
oWord.ActiveDocument.HTMLProject.HTMLProjectItems(1).Text =
htmlValue
oWord.ActiveDocument.HTMLProject.RefreshDocument
End If

'Display MS-Word.
oWord.Visible = True
oWord.Activate
 
P

Paul Shapiro

I am not familiar with either of those 2 approaches. I did see some samples
recently suggesting the DataObject.SetData method might have an optional
argument to specify the format you want to use?

Wish I had looked a little harder since I've spent MANY hours writing a VBA
class to put html onto the clipboard! It is finally working correctly today.
The big problems were a) realizing that a unicode string was being converted
to ansi on the clipboard, thus losing any non-ansi characters and b)
figuring out how to convert the unicode string to a utf-8 byte array and put
that onto the clipboard with the required html-format header.

If you want to email me (make the obvious antispam correction to my email
address) I can send you a copy of the clipboard code.
 
R

Randy

Hi.  I need help.  I am having problems pasting an html string into
Word 2007.  The code (below) for Word Versions prior to 2007 (12)
works fine, but Word 2007 pastes the string as text with the
associated html tags displaying on the document.  Any ideas on this?

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim htmlValue as String

htmlValue = "<html><body>Test</body></html>"

'Open MS-Word via OLE.
Set oWord = CreateObject("word.application")
Set oDoc = oWord.Documents.Add()

'get version of word
If oWord.Version = 12 Then
    'Open a new document with the default template.
    Dim clipDO As DataObject
    clipDO.SetData htmlValue

    Clipboard.SetText htmlValue

    'Add text to the document.
    'Displays with html tags on page.
    oWord.Selection.Paste

Else
    'This works
    oWord.ActiveDocument.HTMLProject.HTMLProjectItems(1).Text =
htmlValue
    oWord.ActiveDocument.HTMLProject.RefreshDocument
End If

'Display MS-Word.
oWord.Visible = True
oWord.Activate

Hi Paul. thanks for offering to share your solution. You can e-mail
me directly at (e-mail address removed)

Thanks! Randy
 

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