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).
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).
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc";
oWord = new Word.Application();
object objDocTemplate = Server.MapPath("") + "\\MyTemplate\\MyTemplate.dot";
oDoc = oWord.Documents.Add(ref objDocTemplate, ref oMissing, ref oMissing,
oDoc.ActiveWindow.View.TableGridlines = false;
oDoc.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;
oDoc.ShowSpellingErrors = false;
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";
string MyImage = "img/MyImage.jpg";
ref linktofile as object, ref savewithfile, ref range);
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);
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.
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.
To view the created document file from the web site (browser), beneath all the Automation code, just redirect to the created Word document using
C# Corner - Word automation using C#