Close





 
Thursday, 14 July 2011 06:14

How to Use C#/VB.NET to Convert Word to HTML

Why We Convert Word to HTML?

Microsoft Word should be one of the best programs for content design and edit. If you want to create PDF, you can design in Microsoft Word and if you want to create HTML, you still can design in Microsoft Word. And then, convert the Word files to PDF or HTML.

 

How to Convert Word to HTML with Spire.Doc?

Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. And it’s also a component for developers/programmers to use C#/VB.ET convert Word to HTML, PDF, Text, RTF, XML, etc. This article will show you how to use C#/VB.NET convert Word to HTML with Spire.Doc.

 

Download Spire.Doc (or Spire.Office) with .NET Framework 2.0 (or above) together and follow the simple steps below to convert Word to HTML.

 

Step 1

Create a project in Visual Studio. Add Spire.Doc DLL as reference.

Step 2

Use the code below to load the local Doc file which you want to convert to HTML.

	    Document document = new Document();
	    document.LoadFromFile(@"D:\Sample.doc");

Step 3

The code below can help us save the Word doc file as HTML format

	    document.SaveToFile("Sample.html", FileFormat.Html);

Step 4

Now, copy the below full code into your project for C# or VB.NET. And then, press F5 to start the program. A HTML file will be automatically generated.

Convert Word to HTML with Full C# Code:

[C#]
	private void button1_Click(object sender, EventArgs e)
	{
	    //Create word document
	    Document document = new Document();
	    document.LoadFromFile(@"D:\Sample.doc");
	 
	    //Save doc file.
	    document.SaveToFile("Sample.html", FileFormat.Html);
	 
	    //Launching the MS Word file.
	    WordDocViewer("Sample.html");
	}
	 
	private void WordDocViewer(string fileName)
	{
	    try
	    {
	        System.Diagnostics.Process.Start(fileName);
	    }
	    catch { }
	}
     

Convert Word to HTML with Full VB.NET Code:

[Visual Basic]
	Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
	    'Create word document
	    Dim document_Renamed As New Document()
	    document_Renamed.LoadFromFile("D:\Sample.doc")
	 
	    'Save doc file.
	    document_Renamed.SaveToFile("Sample.html", FileFormat.Html)
	 
	    'Launching the MS Word file.
	    WordDocViewer("Sample.html")
	End Sub
	 
	Private Sub WordDocViewer(ByVal fileName As String)
	    Try
	        Process.Start(fileName)
	    Catch
	    End Try
	End Sub
     

 

Effective Screenshot


More about Spire.Doc

Download Spire.Doc

Purchase Spire.Doc