Question : Problem automating word on IIS

Hi experts,

Scenario / Goal

I am writing an Intranet system that uses data on an sql server. The web application needs to be able to 'mail merge' MS Word templates with some basic client details.

I am using Visual Studio 2008 and within that environment the application works fine; It launches Word and can do the merge. When I try to deploy to my Internal webserver the app crashes with the following output:
*****

Server Error in '/Beta' Application.
--------------------------------------------------------------------------------

Word could not fire the event.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Word could not fire the event.

Source Error:


Line 21:         oWord = CreateObject("Word.Application")
Line 22:         oWord.Visible = True
Line 23:         oDoc = oWord.Documents.Add("c:\temp\standard letter.dot")
Line 24:        
Line 25:        
 

Source File: C:\Inetpub\wwwroot\Beta\Word Automation.aspx    Line: 23

Stack Trace:


[COMException (0x800a1772): Word could not fire the event.]
   Microsoft.Office.Interop.Word.Documents.Add(Object& Template, Object& NewTemplate, Object& DocumentType, Object& Visible) +0
   ASP.word_automation_aspx.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\Beta\Word Automation.aspx:23
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

 --------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
*****

Environment / History

I am using an XP Pro (SP2) machine as the webserver
Running IIS 5.1
I have word 2003 installed on the server
I have installed the MS Office 2003 interop on the server
My test aspx page is attached. It displays the current user and that user is displaying as ASPNET when I run it on the server.

On the server I have done the following to try to resolve the situation -

DCOMCNFG.exe / Found the component (Microsoft Word Document)
Properties / security tab / added ASPNET user to customise settings in launch/access/config
Identity tab is set to 'The launching user'
This changed an initial error message of 'Exception Details: System.Exception: Cannot create ActiveX component.' to the following error where it cannot fire the event.

Any thoughts?


Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
<%@ Page Language="VB" %>
 
<%@ Import Namespace = "System.Runtime.InteropServices"%>
<%@ Import Namespace = "Microsoft.Office.Interop.Word"%>
 

 

 


    Automation for the nation!


    
    

<%=Environment.UserName%>

Answer : Problem automating word on IIS

>PaulHews - you're saying the bottom-line / party-line is that we just don't do this?

MS recommends that you don't.

>one that can maybe do it in RTF format?

Given that RTF is essentially a text format, you could setup unique template markers in your RTF file using Word, and do the replacement using string functions.  (Simple example below)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As New System.Text.StringBuilder(My.Computer.FileSystem.ReadAllText("C:\temp\template.rtf"))
 
        s.Replace("%%firstname%%", "Joe")
        s.Replace("%%lastname%%", "Smith")
        s.Replace("%%address%%", "123 Woodland Dr")
 
        My.Computer.FileSystem.WriteAllText("C:\temp\test.rtf", s.ToString, False, System.Text.Encoding.ASCII)
        Process.Start("C:\temp\test.rtf") 'Just to display new RTF file
    End Sub
 
 
End Class
 
Rename as template.rtf
 
Random Solutions  
 
programming4us programming4us