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.
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
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.
If you have any doubt in this code please post your comment below.
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?"