Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

MS Word Automation from ASP.NET and Publishing

0.00/5 (No votes)
13 Aug 2007 1  
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