Error on converting HTML to Word

D

dtamajon

I'm working with VS.NET and Office 2003. Now I'm trying to convert some
HTML files to Word format.

Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim missingValue As Object = System.Reflection.Missing.Value


wDoc = wApp.Documents.Open(htmlFile.html)
wDoc.SaveAs("wordFile.doc", Word.WdSaveFormat.wdFormatDocument)

wDoc.Close()
wApp.Quit(missingValue, missingValue, missingValue)


That code work fine for Office 2000, but not for 2003, because Word is
asking for losing style sheets on transformation (you can try doing the
same from the Word application). So, I need to avoid the message box.

I tried with "wApp.DisplayAlerts = WdAlertLevel.wdAlertsNone", with no
results.

Any idea?

Thanks,
Dani
 
C

Cindy M -WordMVP-

Hi Dani

I don't think there's any way to suppress this message. What you need to
do is determine if the document links to a styles sheet and remove that
information from the HTML before saving. You may know of a better tool
for doing this than using Word's object model, if so you should use that
before opening the HTML file in Word.

Otherwise, it can be done in Word something like this (Word VBA)
Sub GetHMTLSource()
Dim rng As Word.Range
Dim sHTML As String
Dim doc As Word.Document
Dim lStartPos As Long
Dim lEndPos As Long
Dim sNextChar As String
Dim sStyleLink as String

Set doc = ActiveDocument
sHTML = doc.HTMLProject.HTMLProjectItems(1).Text
sStyleLink = "<link rel=Stylesheet"
If InStr(sHTML, sStyleLink) <> 0 Then
lStartPos = InStr(doc.HTMLProject.HTMLProjectItems(1).Text,
sStyleLink)
lEndPos = lStartPos
Do
lEndPos = lEndPos + 1
sNextChar = Mid(sHTML, lEndPos, 1)
Loop Until sNextChar = ">"
End If
'MsgBox Mid(sHTML, lStartPos, lEndPos - lStartPos + 1)
sHTML = Left(sHTML, lStartPos - 2) & Mid(sHTML, lEndPos + 1)
doc.HTMLProject.HTMLProjectItems(1).Text = sHTML
doc.HTMLProject.RefreshDocument
End Sub
I'm working with VS.NET and Office 2003. Now I'm trying to convert some
HTML files to Word format.

Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim missingValue As Object = System.Reflection.Missing.Value


wDoc = wApp.Documents.Open(htmlFile.html)
wDoc.SaveAs("wordFile.doc", Word.WdSaveFormat.wdFormatDocument)

wDoc.Close()
wApp.Quit(missingValue, missingValue, missingValue)


That code work fine for Office 2000, but not for 2003, because Word is
asking for losing style sheets on transformation (you can try doing the
same from the Word application). So, I need to avoid the message box.

I tried with "wApp.DisplayAlerts = WdAlertLevel.wdAlertsNone", with no
results.

Any idea?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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