In this article I will explain with an example, how to convert Office Word document (Doc & Docx) to HTML using C# and VB.Net.
The Office Word document (Doc & Docx) will be converted to HTML using Microsoft Office Interop Library in C# and VB.Net.
 
 
Adding reference of Microsoft Office Interop Library
In order to add reference of the Microsoft Office Interop Library, right click on the Project in Solution Explorer and click Add Reference.
Then from the following Window, please select Microsoft Office Interop.Word assembly.
Convert Word (Doc & Docx) to HTML using C# and VB.Net
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control, a Button and an HTML DIV.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="submit" OnClick="Upload" />
<hr />
<div id="dvWord" runat="server"></div>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions;
 
VB.Net
Imports System.IO;
Imports Microsoft.Office.Interop.Word;
Imports System.Text.RegularExpressions;
 
 
Convert Office Word Document to HTML and display it in browser in ASP.Net
The following event handler gets called when the Submit button is clicked.
The uploaded Word Document file is first saved into a Folder named Temp within the Project Folder and then the Word Document file is converted into HTML string using Microsoft Office Interop Library.
Finally the HTML string will be assigned to the InnerHtml property of the HTML DIV.
C#
protected void Upload(object sender, EventArgs e)
{
    object documentFormat = 8;
    string randomName = DateTime.Now.Ticks.ToString();
    object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
    string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";
    object fileSavePath = Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
 
    //If Directory not present, create it.
    if (!Directory.Exists(Server.MapPath("~/Temp/")))
    {
        Directory.CreateDirectory(Server.MapPath("~/Temp/"));
    }
 
    //Upload the word document and save to Temp folder.
    FileUpload1.PostedFile.SaveAs(fileSavePath.ToString());
 
    //Open the word document in background.
    _Application applicationclass = new Application();
    applicationclass.Documents.Open(ref fileSavePath);
    applicationclass.Visible = false;
    Document document = applicationclass.ActiveDocument;
 
    //Save the word document as HTML file.
    document.SaveAs(ref htmlFilePath, ref documentFormat);
 
    //Close the word document.
    document.Close();
 
    //Read the saved Html File.
    string wordHTML = System.IO.File.ReadAllText(htmlFilePath.ToString());
 
    //Loop and replace the Image Path.
    foreach (Match match in Regex.Matches(wordHTML, "<v:imagedata.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase))
    {
        wordHTML = Regex.Replace(wordHTML, match.Groups[1].Value, "Temp/" + match.Groups[1].Value);
    }
 
    //Delete the Uploaded Word File.
    System.IO.File.Delete(fileSavePath.ToString());
 
    dvWord.InnerHtml = wordHTML;
}
 
VB.Net
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
    Dim documentFormat As Object = 8
    Dim randomName As String = DateTime.Now.Ticks.ToString
    Dim htmlFilePath As Object = Server.MapPath("~/Temp/") & randomName + ".htm"
    Dim directoryPath As String = Server.MapPath("~/Temp/") & randomName + "_files"
    Dim fileSavePath As Object = Server.MapPath("~/Temp/") & Path.GetFileName(FileUpload1.PostedFile.FileName)
 
    'If Directory not present, create it.
    If Not Directory.Exists(Server.MapPath("~/Temp/")) Then
        Directory.CreateDirectory(Server.MapPath("~/Temp/"))
    End If
 
    'Upload the word document and save to Temp folder.
    FileUpload1.PostedFile.SaveAs(fileSavePath.ToString)
 
    'Open the word document in background.
    Dim applicationclass As _Application = New Application
    applicationclass.Documents.Open(fileSavePath)
    applicationclass.Visible = False
    Dim document As Document = applicationclass.ActiveDocument
 
    'Save the word document as HTML file.
    document.SaveAs(htmlFilePath, documentFormat)
 
    'Close the word document.
    document.Close()
 
    'Read the saved Html File.
    Dim wordHTML As String = System.IO.File.ReadAllText(htmlFilePath.ToString)
 
    'Loop and replace the Image Path.
    For Each match As Match In Regex.Matches(wordHTML, "<v:imagedata.+?src=[""'](.+?)[""'].*?>", RegexOptions.IgnoreCase)
        wordHTML = Regex.Replace(wordHTML, match.Groups(1).Value, ("Temp/" + match.Groups(1).Value))
    Next
 
    'Delete the Uploaded Word File.
    System.IO.File.Delete(fileSavePath.ToString)
 
    dvWord.InnerHtml = wordHTML
End Sub
 
 
Screenshot
Convert Word (Doc & Docx) to HTML using C# and VB.Net
 
 
Downloads