none
Continuing problems with WebService and Marshal.GetActiveObject RRS feed

  • Question

  • Hi

    I thought I had the solution when I closed the previous thread regarding this, but it seems I was wrong.

    I got it to work when using the default debug (http://localhost:1319) configuration, but as soon as I move it to an ordinary IIS it fails.

    I know this is not a "recommended" use of Office in an unattended server solution, but this has worked like a charm for over a year, creating +10 000 documents.

    This is the scenario:

    A WebService is using Word 2007 to create documents by demand from a local company website. The documents are created in batches depending on which template the document is based on. Some of the templates is used for several document but up to now the WebService has managed to work as planned. The WebService has it's own Application Pool on IIS running with a specially created user account. The web.config for the service also impersonates the same user.

    In order to increase performance I'm trying to reuse the Word process, instead of restart it for every WebService call. This should decrease the time it takes for a batch to run through.
    It's here the call to Marshal.GetActiveObject is used.

    The error I get is: command is anavailable HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))

    From the WebService method I use this to get hold of a Word session:

    Dim wrdApp as Word.Application
    
    wrdApp = GetRunningWord()
    If wrdApp = Nothing Then
       'Error in call
       Return Nothing
       Exit Function
    Else
      'Call OK. Execute Word tuff
    End If
    

    The function GetRunningWord looks like this:

        <SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.UnmanagedCode)> _
        Private Function GetRunningWord() As Word.Application
    
            Dim LobjWordApplication As Word.Application = Nothing
    
            If Process.GetProcessesByName("WINWORD").Length <> 0 Then
                ' Get a pointer to the Word application
                Try
                    LobjWordApplication = Marshal.GetActiveObject("Word.Application")
                Catch ex As Exception
    		LobjWordApplication = Nothing
    		WriteWSLogg("Error getting Word-session.", ex.Message, logFile)
                End Try
            Else
                ' Create a new instance of the word application
                LobjWordApplication = New Word.Application
                LobjWordApplication.Visible = True
                LobjWordApplication.Visible = False
            End If
    
            GetRunningWord = LobjWordApplication
    
        End Function
    
    

    The Visible=True and Visible = False lines for the Word application is there in order for the Word application to show up in the GOT-list of processes.

    The solution compiles and there are no errors or warnings displayed.


    The solution is written in Visual Studio 2008, and Office version is 2007 with SP2.

    Any help is appreciated

    Thanks in advance

     


    Best Regards Peter Karlström Midrange AB, Sweden
    Tuesday, November 8, 2011 3:44 PM

Answers

  • Hi Cindy

    Now 14 days have past and still no reply.

    It's a pity since this is supposed to be a paid service for which I pay a yearly fee as part of my MSDN subscription.
    At least a reply if using Marshal.GetActiveObject in the WebService solution is even possible to accomplish would have been nice.

    Thanks anyway for the support you have delivered.

     


    Best Regards Peter Karlström Midrange AB, Sweden
    Friday, November 25, 2011 9:10 AM

All replies

  • Hi Peter

    The VSTO forum is certainly the wrong venue for this question as VSTO targets extending the Office UI and is never used in asp.net, IIS or anything of that nature. So I'm going to move this to Word for Developers where it's at least more "on-target" than in the VSTO forum.

    You say the program has worked for over a year, but you are trying to change the way it works and its quite possible that the configuration you're trying to run Word in may simply not support GetActiveObject. It might be worthwhile to ask in forums that support the configuration you're using whether or not GetActiveObject can be used successfully to retrieve an application instance from the ROT for COM interop.

    In any case, the better - and much more efficient approach - would be to build these documents as Open XML. That's what the file formats were designed for. Word wouldn't need to be installed or present on your server, so you'd have none of these problems.


    Cindy Meister, VSTO/Word MVP
    Tuesday, November 8, 2011 6:35 PM
    Moderator
  • Hi Peter,

    I assume Word was started by the same user account which runs the code fragment above, correct?

    Have you read http://social.msdn.microsoft.com/Forums/en-AU/vbinterop/thread/c596b669-347e-4f57-9963-65a95d6c062b?


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Wednesday, November 9, 2011 7:22 AM
  • Hi Andrei

    Thanks for your response.

    Yes, the w3wp.exe process is run by the same user as the the running Winword.exe I want to reattach to.

    I followed your link and read it through. The link in it, to www.codecomments.com, is not working, but I tested to change the services as suggested so that the LocalSystem "user" could interact with the desktop, but that change made no difference. same error. 


    Best Regards Peter Karlström Midrange AB, Sweden
    Wednesday, November 9, 2011 9:07 AM
  • Hi Cindy

    I did, sadly, not get any more help on this.

    I would have preciated a GO or a NONO for this approach in my solution, and I'm not sure if your moving this issue away from a forum I know is a "MSDN Managed Discussion Group" is the cause or not.

    I know about Open XML and that Microsoft doesn't recommend using Office for automation om servers, but our problem is more urgent than the time we have to spend on rewriting the webservice for Open XML.

    Aren't there any Microsoft technichian involved in this forum that can give me the GO or the NONO?

    Thanks in advance


    Best Regards Peter Karlström Midrange AB, Sweden
    Friday, November 11, 2011 10:06 AM
  • Hi Peter

    This forum is monitored by the same Microsoft Contingent Staff as the VSTO forum. The amount of time it takes to get a response from them is the same. If we "leave it alone" for a couple of days, one of them will pick it up.

    FWIW, though, the answers everyone who asks this kind of thing (server-side - and there have been a slew of them the last couple of weeks) gets from staff has been pretty much the same as the answer you got from Calvin Gao originally: what you want to do is not supported and to look at using OpenXML.

    I recommend you post an answer to this reply saying you're looking for more help specifically from staff. (For this reason, I'd not have answered except that you asked about whether staff is present here.)


    Cindy Meister, VSTO/Word MVP
    Friday, November 11, 2011 10:25 AM
    Moderator
  • Hi Cindy

    Thanks for your reply and your tips.

    We will rewrite this webservice in the "near" future, but as for now, we need "a fix".

    So, this is to staff: I'm looking for more information on this from staff!

     


    Best Regards Peter Karlström Midrange AB, Sweden
    Friday, November 11, 2011 11:27 AM
  • Hi Cindy

    Now 14 days have past and still no reply.

    It's a pity since this is supposed to be a paid service for which I pay a yearly fee as part of my MSDN subscription.
    At least a reply if using Marshal.GetActiveObject in the WebService solution is even possible to accomplish would have been nice.

    Thanks anyway for the support you have delivered.

     


    Best Regards Peter Karlström Midrange AB, Sweden
    Friday, November 25, 2011 9:10 AM