C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Creating a Word document using ASP.NET and C#


Posted Date: 30 Jun 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Sumit ThomasMember Level: Silver    
Rating: 1 out of 5Points: 7



In one of my projects recently, I had to create word documents dynamically using ASP.NET. I turned to MSDN for help and was surprised to find how easy it was to do so.

I am using Visual Studio 2002 and .NET Framework 1.0. Therefore if you are using the latest versions, unlike me :-( then do check out for the updates in the classes and methods that I am going to use in this article.

1) OK, lets start. Open up Visual Studio and create an ASP.NET Web application using C#.

2) Now we have to add a COM reference to our project. In the references dialog box, under COM you will find Microsoft Word 9.0 Object Library. Select it and add it to the project. This will add few COM dll's in the bin directory.

3) Now the code…



object fileName="D:\\MyWord.doc";
object novalue=System.Reflection.Missing.Value;

Word.ApplicationClass objWord = new Word.ApplicationClass();
Word.Document objWordDoc=objWord.Documents.Add(ref novalue, ref novalue, ref novalue, ref novalue);

objWordDoc.Activate();

objWord.Selection.TypeText(".NET Rocks!!!");
objWord.Selection.TypeParagraph();
objWord.Selection.TypeText("Sumit loves .NET");

objWordDoc.SaveAs(ref fileName, ref novalue, ref novalue, ref novalue,
ref novalue, ref novalue, ref novalue, ref novalue, ref novalue,
ref novalue, ref novalue);

objWord.Quit(ref novalue, ref novalue, ref novalue);


Now check the MyWord.doc file in the D: drive.

The code is pretty simple and intuitive, hence a quick explanation.

* We are first creating a object of Word.ApplicationClass
* Next we add a Word document to this object. Incase you don't want to pass a particular parameter, use the 'novalue' object that we have created in the code.
* The following steps are pretty self explanatory.

The Visual Studios' Intellisense feature will let us know of the expected parameters of each method. In the latest versions of .NET framework the number of parameters required is more.

Do check out http://msdn.microsoft.com to learn about automation in .NET

Cheers






Responses

Author: Dick Bevernage    05 Jul 2004Member Level: Bronze   Points : 0
Very easy indeed. But do keep in mind this requires MS WORD to be installed next to your web server.

Check out http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757 to know why this isn't a good idea.

There are 3th party server components that solve this problem (e.g. SoftArtisans OfficeWriter).
But I haven't come across any freeware yet, anybody else did? Feel free to let us know!



Author: AbiBaby (Abishek Bellamkonda)    05 Jul 2004Member Level: Gold   Points : 0
Word on webserver is Major security issue


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Passing Multiple Parameters in DataGrid’s Asp:Hyperlink Column
Previous Resource: How to disable the shutdown option in the computer
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use