Link to home
Start Free TrialLog in
Avatar of Real_coffee
Real_coffee

asked on

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?



<%@ Page Language="VB" %>
 
<%@ Import Namespace = "System.Runtime.InteropServices"%>
<%@ Import Namespace = "Microsoft.Office.Interop.Word"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim oWord As Microsoft.Office.Interop.Word.Application 'Word.Application
        Dim oDoc As Microsoft.Office.Interop.Word.Document 'Word.Document
       
        'Launch Word
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        
        'Start a new doc based upon a template
        oDoc = oWord.Documents.Add("c:\temp\standard letter.dot")
        
        
        'Lets insert some values in the documents bookmarks
        oDoc.Bookmarks.Item("contact").Range.Text = "Mr A S Pnet"
        oDoc.Bookmarks.Item("addr1").Range.Text = "Cobweb Cottage"
        oDoc.Bookmarks.Item("addr2").Range.Text = "1 High Street"
        oDoc.Bookmarks.Item("addr3").Range.Text = "Bunbury"
        oDoc.Bookmarks.Item("addr4").Range.Text = "Cheshire"
        oDoc.Bookmarks.Item("postcode").Range.Text = "CH 1AB"
        oDoc.Bookmarks.Item("greeting").Range.Text = "Mr Pnet"
                
        'Close everything down
        oWord = Nothing
        oDoc = Nothing
               
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Automation for the nation!</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <br />
    <%=Environment.UserName%>
    </div>
    </form>
</body>
</html>

Open in new window

Avatar of PaulHews
PaulHews
Flag of Canada image

Office applications are expected to run interactively, so there are a number of problems automating Office from IIS or from other non-interactive environments.  See this kb article for details and workarounds.

Considerations for server-side Automation of Office
http://support.microsoft.com/?kbid=257757
Avatar of Praveen DM
*. Can u double check if the site is having Network Services permission propagated from the root and also check if the IUSR of the site has permission over the temp folder... of the server...
Avatar of Real_coffee
Real_coffee

ASKER

PaulHews - you're saying the bottom-line / party-line is that we just don't do this? Ok, I take that onboard. Are there any 3rd party tools you would recommend that can get me round this 'mail merge' problem? one that can maybe do it in RTF format?

servoadmin - is that the c:\windows\temp folder?

Has anybody heard of these guys - http://www.mylittletools.net/en/mylittlewriter/

Think this would do what Im after?
Yeah absltly... and for the tool u asked...there has been alot of discussions in experts exchange... select the best rated and appropriate..

https://www.experts-exchange.com/simpleSearch.jsp?q=mylittlewriter&searchSubmit=1&cid=315&redirectURL=%2F&gSearch=0
I have contacted mylittletools - their app will not open existing rtf files.

Just creates them on the fly which is good but not what I need.
servoadmin - permissions are good on c:\windows for IUSR. Word automation still fails
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Paul,

That's really elegant. Its creates a new RTF file, replacing the markers with data. Effectively it creates a new 'mail merged' letter (pity about the sweepstakes!).

line 11. process.start doesnt work for me - what are you trying to do there ? VS tells me that process is not declared?

Ive spent all day working at this from another/wrong angle; using streamwriter to create a csv that a word template is merged with. My idea was just to display a link to the .dot file that the user could click to 'launch' Word themselves. What do you think?

>line 11. process.start doesnt work for me - what are you trying to do there ? VS tells me that process is not declared?<

That just launches the default RTF editor to show the newly created file... It's not desirable in an ASP.NET application.  (Process is in the System.Diagnostics namespace.)

>using streamwriter to create a csv that a word template is merged with. My idea was just to display a link to the .dot file that the user could click to 'launch' Word themselves. What do you think?

I could see some problems with the merge data source not being available on the client PC, and possibly the Word template opening in read-only mode.  
Completely agree with you regarding streamwriter and the merged word doc.

Your solution with the RTF file was exactly what I needed.
In my opinion this was the absolute correct approach to Word automation in ASP.NET (Dont - use RTF files instead).