How to show Microsoft Word Document content in browser using ASP.NET?


Recently I have seen so many members posting the questions in forum “How to show Word Document in browser without showing Open/Save dialog box. Today we will see how we can show the word content in browser without opening the whole word document separately in browser.

Today we are going to learn how to open the word document content in browser using ASP.NET. This is one of the common requirements if you are building job portal or any portal which takes the attachments in Microsoft Word document.

Implementating opening of word document on web browser



This approach to show the word content in browser doesn't require much coding. It requires only the Microsoft.Office.Interop assembly reference and few lines of code.

First we will prepare our sample word document.

How to show Microsoft Word Document content in browser using ASP.NET

Save the above document in a folder, In this case I have saved the same in D drive of my computer.

Now we will move to the coding section

Open Visual Studio and then open a new project, give a proper name to your solution and also for the webform.

As we are using MS Word, of course we have to add reference to the Microsoft.Office.Interop assembly. Right click on Reference from your solution explorer and select .NET tab and scroll down to select the Microsoft.Office.Interop. Make sure to select the correct version dll. If you are using MS Office 2007 then select 12.0 if you are using MS Office 2010 then select 14.0

Add reference to Microsoft.Office.Interop assembly

We will come to the real coding now,

Add a button in your web form as "Show Word Document". Double click on it to open the code behind page.
First we will import the required namespace as


using Microsoft.Office.Interop;


Below is the code you have to use under button click event.


protected void btnShowDoc_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application objWordApp = new Microsoft.Office.Interop.Word.Application();
object objWordFile = "D:\\dotnetspider.docx";
object objNull = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document WordDoc = objWordApp.Documents.Open(
ref objWordFile, ref objNull, ref objNull,
ref objNull, ref objNull, ref objNull,
ref objNull, ref objNull, ref objNull,
ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);
WordDoc.ActiveWindow.Selection.WholeStory();
WordDoc.ActiveWindow.Selection.Copy();
string strWordText = WordDoc.Content.Text;
WordDoc.Close(ref objNull, ref objNull, ref objNull);
objWordApp.Quit(ref objNull, ref objNull, ref objNull);
Response.Write(strWordText);
}


In the above code you can see that I am reading the MS Word file dotnetspider.docx saved in D drive. I believe most of the code here is self explanatory.

Show Microsoft Word Document content

How to show Microsoft Word Document content in browser

If you have any doubt in this code please post your comment below.


Article by Asheej T K
Thanks and Regards Asheej T K Dotnet Galaxy

Follow Asheej T K or read 33 articles authored by Asheej T K

Comments

Author: saravanakumar07 Dec 2012 Member Level: Gold   Points : 3

hi sir,

if the word document have any alignment, styles, animations, images means how to display the word document into the web page. in this resource just like notepad file read and write content into the webpage. put title as "How to show Microsoft Word Document text content in browser using ASP.NET?"

Guest Author: Anitha14 Mar 2013

How to design a web form that accepts data and store it in notepad file?

Guest Author: Senthilnathan19 Mar 2013

HI sir,

By this coding we just read the content of the word file as notepad. I need to implement that the whole word application should open under the Browser window.This is First phase and then we have to Edit the content and have to save the changes .These all done under the browser window itself..Please help me

Guest Author: Jain Nirav04 Jun 2013

How to show same word format in web browser.Please Help me.

Guest Author: Jain Nirav04 Jun 2013

Hi Sir

How to show same word file and same formet in web browser.

Guest Author: sandeep26 Dec 2014

hi sir

how to view the ms word in browser for editable, not for read-only.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: