MS Word from ASP.NET

Feb 28 2008 7:22 AM
Hi I am trying to print .doc file stored on the server by clicking the button on the page.
Here is the code:

protected void test_Click(object sender, EventArgs e)
{
ApplicationClass word_app = new ApplicationClass();

object fileName = "C:\\Doc\\test.doc";

word_app.Visible = true;

word_app.Activate();


word_app.Visible = false;
object falseValue = false;
object trueValue = true;
object missing = Type.Missing;
object readOnly = false;
object isVisible = true;

object format = WdOpenFormat.wdOpenFormatAuto;
object direction = WdDocumentViewDirection.wdDocumentViewLtr;

Document word_doc = word_app.Documents.Open(ref fileName, ref falseValue, ref trueValue, ref falseValue, ref missing, ref missing, ref falseValue, ref missing, ref missing, ref missing, ref missing, ref isVisible);

word_doc.Activate();

word_doc.PrintOut(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, ref missing, ref missing, ref missing);

Thread.Sleep(2000);
word_app.Quit(ref falseValue, ref missing, ref missing);
}

I am refferencing to Interop.Word ; Interop.Office;

Here is the problem:
I am using VS2005 and if I am running this code under ASP.NET Development Server Port – 2006 its working but if I am running it under IIS I am getting :

Stack Trace:
[COMException (0x800a11f9): Cannot activate application] Word.ApplicationClass.Activate() +0 PrintDoc._Default.test_Click(Object sender, EventArgs e) in C:\slavik\PrintDoc\Default.aspx.cs:31 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105 System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +107 System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102


I think that the problem is that in the first case process is accessed by local user and in case of IIS it’s accessed by ASP.NET user but actually I did turn in Component Services \ DCOM Config \ Microsoft Office Word 97-2003 – properties – security tab ASP.NET Machine Account and still I am getting this exception L

IF anybody know what to do in this case I’ll greatly appreciate it.

Thank you in advance.
Dilipv
SharePoint Consulting

Answers (1)