Click here to Skip to main content
Click here to Skip to main content
Go to top

MS Word Automation from ASP.NET and Publishing

, 13 Aug 2007
Rate this:
Please Sign up or sign in to vote.
MS Word Automation from ASP.NET

Introduction

This article provides details on how to perform MS Word Automation from ASP.NET. It also includes details on how to make the Automation feature work after publishing the files to the Web Server (Windows 2000 Server / IIS 5).

Background

There are many sites that provide details on how to perform Automation from ASP.NET. But very little information is available on making the web site work, once its published to the Web server. Access privilege has to be assigned to carry out Automation from web site.

Using the Code

The following code snippet provides basic information on how to create an MS Word DOC from ASP.NET / C#. What we do here is open a pre-defined document template (DOT file) and during execution, insert data to the template wherever required and then save the DOT file as a new DOC file. The sample code shows how to insert a data/image into specific cells in the DOT file. It is assumed that the template conatains tables into which we write the data/image during execution. Remember to Add Reference to the COM Object Library for MS Word (e.g. Microsoft Word 9.0 Object Library for MS Word 2000).

// Handle parameters you don't care about in .NET        
object oMissing = System.Reflection.Missing.Value;

// Predefined bookmark
object oEndOfDoc = "\\endofdoc";  

// Launch MS Word
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();

// Get the document template (.DOT) file
object objDocTemplate = Server.MapPath("") + "\\MyTemplate\\MyTemplate.dot";
oDoc = oWord.Documents.Add(ref objDocTemplate, ref oMissing, ref oMissing, 
    ref oMissing);

// Forcefully hide the table GridLines
oDoc.ActiveWindow.View.TableGridlines = false;
// Forcefully set the Zoom percentage to 100% when opening the document
oDoc.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;
// Forcefully hide the Spelling Errors
oDoc.ShowSpellingErrors = false;

// Add text data to the cells in the Table (This can be static data or 
// data retrieved from database)
oDoc.Tables.Item(1).Cell(1, 1).Range.Text = "My Document's Title";
oDoc.Tables.Item(1).Cell(2, 1).Range.Text = "My Document Description";
oDoc.Tables.Item(1).Cell(3, 1).Range.Text = "My Image Name";
// Add image to the cell in the Table
string MyImage = "img/MyImage.jpg";
oDoc.Tables.Item(1).Cell(4, 1).Range.InlineShapes.AddPicture(myImage, 
    ref linktofile as object, ref savewithfile, ref range);

// Save opened template as another .doc file
Object oSaveAsFile = Server.MapPath("") + "\\MyTemplate\\MyDocument.doc";
oDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                   ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
                   ref oMissing, ref oMissing, ref oMissing);

// Close the document, destroy the object and Quit Word
object SaveChanges = true;
oDoc.Close(ref SaveChanges, ref oMissing, ref oMissing);
oDoc = null;
oWord.Quit(ref SaveChanges, ref oMissing, ref oMissing);

Once the required coding has been done, when running the web app from DEBUG mode (that is while executing from .NET IDE), the web app would run and on the event specified, the Automation task would be performed and the document file (MyDocument.doc) would be created.

But if this code is published and hosted in IIS, while trying to perform the Automation, the following error would be displayed, if no permission has been granted.

Screenshot - com_error.gif

To do away with this bugging issue, carry out the following tasks:

  • Create a new Windows user (e.g. MSWordUser). Note that this user should be part of the group "Administrators".
  • From the Start menu, run dcomcnfg utility (Start > Run > dcomcnfg).
  • Select "Microsoft Word Document" from the "Applications" tab.
  • Click the "Default Security" tab.
  • Click "Edit Default" button under "Default Access Permissions". Include MSWordUser to have "Allow Access" permission.
  • Click "Edit Default" button under "Default Launch Permissions". Include MSWordUser to have "Allow Launch" permission.

Try running the application now and if the error persists even after carrying out the above mentioned tasks, include the following in the Web.Config file and thats it! The DOC file would be created and saved without any issues.

Screenshot - web_config.gif

To view the created document file from the web site (browser), beneath all the Automation code, just redirect to the created Word document using Response.Redirect().

Reference Sites

C# Corner - Word automation using C#

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Hari Narayanan
Web Developer
India India
No Biography provided

Comments and Discussions

 
QuestionDon't use word automation with ASP.net PinmemberMember 29981283-Apr-14 4:09 
QuestionMS Word free alternative PinmemberJohnny Glenn25-Mar-12 22:11 
GeneralMy vote of 1 Pingrouphector garduno20-Sep-10 19:25 
GeneralError 'Microsoft.Office.Interop.Word.Tables' does not contain a definition for 'Items' [modified] Pinmembersandhyaonly12-Aug-10 3:11 
AnswerRe: Error 'Microsoft.Office.Interop.Word.Tables' does not contain a definition for 'Items' PinmemberKurtz NatureBoy29-Nov-10 5:25 
GeneralRedirects to GenericErrorPage.htm PinmemberSudhakaraTP28-Jan-09 7:35 
GeneralCant find "Microsoft Word Document" from the "Applications" in windows XP [modified] Pinmembergrasshopper414-Jul-08 13:28 
Generalthanks, great article everything works! Pinmembercybermox10-Mar-08 14:34 
GeneralObject Reference is not set Pinmemberbharatsinh Mori26-Aug-07 23:08 
GeneralRe: Object Reference is not set Pinmembersilvermonopoly13-Sep-07 5:10 
GeneralRe: Object Reference is not set Pinmembertalk2astro20-Sep-07 1:39 
QuestionError: Object reference not set to an instance of an object. PinmemberPriti Doshi22-Aug-07 9:02 
AnswerRe: Error: Object reference not set to an instance of an object. Pinmembersilvermonopoly13-Sep-07 5:10 
GeneralRe: Error: Object reference not set to an instance of an object. - SOLVED Pinmembersharkiz15-Jul-08 20:24 
QuestionRe: Error: Object reference not set to an instance of an object. Pinmembertalk2astro20-Sep-07 1:38 
QuestionRe: Error: Object reference not set to an instance of an object. Pinmembergabru12330-May-08 9:05 
AnswerRe: Error: Object reference not set to an instance of an object. Pinmemberchillibug22-Jun-08 22:51 
AnswerRe: Error: Object reference not set to an instance of an object. Pinmemberhorsechickencool6-May-14 23:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 13 Aug 2007
Article Copyright 2007 by Hari Narayanan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid