Avatar of 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:= _

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

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

.NET ProgrammingVisual Basic.NETMicrosoft Word

Avatar of undefined
Last Comment
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?

Avatar of chrisfixit


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.

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)
    End Sub
End Class

Open in new window

Avatar of chrisfixit


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)
    End Sub
End Class

Open in new window

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

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of chrisfixit


Thanks for all your help. I was getting errors because I had added the COM references as well as the NET Interop refs.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo