Click here to Skip to main content
15,883,531 members
Articles / Web Development / ASP.NET
Article

MS Word Automation from ASP.NET and Publishing

Rate me:
Please Sign up or sign in to vote.
3.50/5 (7 votes)
13 Aug 20072 min read 86.7K   32   18
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).

C#
// 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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDon't use word automation with ASP.net Pin
Member 29981283-Apr-14 4:09
Member 29981283-Apr-14 4:09 
QuestionMS Word free alternative Pin
Johnny Glenn25-Mar-12 22:11
Johnny Glenn25-Mar-12 22:11 
GeneralMy vote of 1 Pin
hector garduno20-Sep-10 19:25
hector garduno20-Sep-10 19:25 
GeneralError 'Microsoft.Office.Interop.Word.Tables' does not contain a definition for 'Items' [modified] Pin
sandhyaonly12-Aug-10 3:11
sandhyaonly12-Aug-10 3:11 
AnswerRe: Error 'Microsoft.Office.Interop.Word.Tables' does not contain a definition for 'Items' Pin
Kurtz NatureBoy29-Nov-10 5:25
Kurtz NatureBoy29-Nov-10 5:25 
GeneralRedirects to GenericErrorPage.htm Pin
SudhakaraTP28-Jan-09 7:35
SudhakaraTP28-Jan-09 7:35 
GeneralCant find "Microsoft Word Document" from the "Applications" in windows XP [modified] Pin
grasshopper414-Jul-08 13:28
grasshopper414-Jul-08 13:28 
Generalthanks, great article everything works! Pin
cybermox10-Mar-08 14:34
cybermox10-Mar-08 14:34 
GeneralObject Reference is not set Pin
Bharat_Mori26-Aug-07 23:08
Bharat_Mori26-Aug-07 23:08 
GeneralRe: Object Reference is not set Pin
silvermonopoly13-Sep-07 5:10
silvermonopoly13-Sep-07 5:10 
GeneralRe: Object Reference is not set Pin
talk2astro20-Sep-07 1:39
talk2astro20-Sep-07 1:39 
QuestionError: Object reference not set to an instance of an object. Pin
Priti Doshi22-Aug-07 9:02
Priti Doshi22-Aug-07 9:02 
AnswerRe: Error: Object reference not set to an instance of an object. Pin
silvermonopoly13-Sep-07 5:10
silvermonopoly13-Sep-07 5:10 
GeneralRe: Error: Object reference not set to an instance of an object. - SOLVED Pin
sharkiz15-Jul-08 20:24
sharkiz15-Jul-08 20:24 
QuestionRe: Error: Object reference not set to an instance of an object. Pin
talk2astro20-Sep-07 1:38
talk2astro20-Sep-07 1:38 
QuestionRe: Error: Object reference not set to an instance of an object. Pin
gabru12330-May-08 9:05
gabru12330-May-08 9:05 
AnswerRe: Error: Object reference not set to an instance of an object. Pin
chillibug22-Jun-08 22:51
chillibug22-Jun-08 22:51 
AnswerRe: Error: Object reference not set to an instance of an object. Pin
horsechickencool6-May-14 23:00
horsechickencool6-May-14 23:00 

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

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