System Requirements
Create a web application. Right click on reference and click on add reference. Select Com tab. Double click on Microsoft Word 11.0 Object Library.
Add following code to create a word file at a folder in root directory at server it is Word folder in my case.
Object missing = System.Reflection.Missing.Value;
string file = "myfile";
Object filename = Server.MapPath("Word/") + file;
object readOnly = false;
object isVisible = true;
Microsoft.Office.Interop.Word.Application oWordApp;
try
{
// initializing Word Application
oWordApp= new Microsoft.Office.Interop.Word.Application();
// initializing Word Document
Microsoft.Office.Interop.Word.Document oWordDoc = oWordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
try
{
oWordDoc.Activate();
oWordApp.Selection.TypeText("This is the text");
oWordApp.Selection.TypeParagraph();
// Saving Word file
oWordDoc.SaveAs(ref filename, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
oWordApp.Application.Quit(ref missing, ref missing, ref missing);
// Oppening Word file
Response.Redirect("~/Word/"+file,true);
}
}
catch (Exception Wex)
{
Response.Write(Wex.Message);
}
Now set the Security Permission to the Folder and Com Server
Security Account | Permissions |
ASP.NET Machine Account | Modify, Read, Write |
IIS_WPG | Modify, Read, Write |
Internet Guest Account (IUSR_ |
Read, Write |
DCOM Config changes
* Please make sure that Internet Guest Account is the same security account that is used for Basic Authentication for anonymous access for this application. If not then give same permission to that user as IUSR_
Note: If any of the above mentioned user is not present in user list then please add that user as mentioned in step10 above.
Go to the Identity tab. Click on This User and assign a user having administrative privileges on the machine.
Note: The Administrative user should have invoked MS Word at least once by logging on to the machine or a complete installation of MS Word 2003 should have been performed. No part of the installation should be remaining.
原文来自:http://hi.baidu.com/upjoyce/blog/item/9c7233385290c82db9998f40.html