Link to home
Start Free TrialLog in
Avatar of chrisfixit
chrisfixit

asked on

Saving A Word Document as HTML in VB NET

I want to save a word document as HTML.. I have the following code..

adoc.SaveAs(FileName:="c:\test.htm", FileFormat:=8, _
        LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
        :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
        SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
        False)  

but I don't understand why I can't simply use

aDoc.SaveAs(FileName:="c:\test.htm", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument) ???


Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

You can, except that you need to use the correct save format....

    wrdDoc.SaveAs("C:\Path\Test.htm", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML)

Are you getting errors when you try the above code? What is the error?

Wayne
Avatar of chrisfixit
chrisfixit

ASKER

yes I get 'Interop' is not a member of Microsoft error.

As is probably obvious ;-) I'm new to VB Net and I took a piece of code which opens a Word document when a Form button is clicked.

I modified the code so that it opens an Excel worksheet, extracts an email address from each row and creates an Outlook Mailitem for each address and sends the Word Document as the body of the email..

(it does work !!)

I used created the Excel and Outlook Apllication instances using

Dim oExcel As New Microsoft.Office.Interop.Excel.Application
Dim oOutlook As New Microsoft.Office.Interop.Outlook.Application..

but the Word Application from the 'borrowed' code uses..

Private WordApp As New Word.ApplicationClass()

I'm totally confused as to how the two different ways of achieving apparently similar outcomes relate to each other .. though intuitively Iguess this is why I'm getting the error.

The Project has references to Microsoft Excel 11.0 Object Library, Oulook 11.0 Object Library and
Word 11.0 object Library.. the former two have 'Copy Local' aas False, whilst the Word library is True

I haven't the faintest idea what this means ;-(

I'm using Visual Studio 2008, XP Pro SP3
'Copy Local' simply means it'll copy the required references when you build the project. This is important when the end user may not already have the references assemblies installed.

As for the rest, take a look at the below code (A reference was added to the Word 11.0 object Library).

I always import namespaces like that for Office applications, mainly because it makes it so much easier to use. It simply means that instead of this....

    Dim oWord As New Microsoft.Office.Interop.Word.Application

....you can shorten it down to this....

    Dim oWord As New Word.Application

If you have any further queries, please be specific, and post any relevant code.

Wayne
Imports Word = Microsoft.Office.Interop.Word
 
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim wrdApp As New Word.Application
        Dim wrdDoc As Word.Document = wrdApp.Documents.Open("C:\Test.doc")
 
        wrdDoc.SaveAs("C:\Test.htm", Word.WdSaveFormat.wdFormatHTML)
 
        wrdDoc.Close()
        wrdApp.Quit()
 
    End Sub
 
End Class

Open in new window

Thanks very much for explaining it so clearly - I decided to start from scratch using your model..

I had to change the Imports Word = ..  to Imports Microsoft.Office.Interop.Word as it gave me the following

error 'Imports alias 'Word' conflicts with 'Namespace Word' declared in the root namespace.'

I then added the Excel and Outlook COM references but got the following error

'Namespace or type specified in the Imports 'Microsoft.Office.Interop.Outlook' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.'

I've attached my code so far..


Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop.Outlook
 
 
 
Public Class Form1
    Inherits System.Windows.Forms.Form
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Aword As New Word.Application
        Dim Adoc As Word.Document = Aword.Documents.Open("C:\kanales.doc")
        Adoc.SaveAs("C:\kanaleshtml", Word.WdSaveFormat.wdFormatHTML)
        Adoc.Close()
        Aword.Quit()
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all your help. I was getting errors because I had added the COM references as well as the NET Interop refs.