none
Problem using Marshal.GetActiveObject("Word.Application") from WebService RRS feed

  • Question

  • Hi

    I've created a WebService in VB.NET Visual Studio 2008 with .NET 3.5.

    The Webservice has been working fine for over a year, but now we are having some performance issues.

    The webservice creates Office document, spreadsheets and presentations based on a passed on template and som metadata.

    Each call to the service returns Null (on errors) or a document/spreadsheet/presentation object on success.

    The problem is that the service is called from a Web application (Apache-based) and that application uses my webservice in batches.

    These batches are getting bigger, and I'm trying to slim down the time it takes for the webservice to produce a result.

    The only thing left is to try and reuse the Word application, so that each call doesn't have to restart a new Word session.

    With the codeline Process.GetProcessByName("WINWORD") I manage to see wether a previous instance exists.

    If it does, I try accessing it with Marshal.GetActiveObject("Word.Application"), but this throws an exception. Message is Object is unavailable and the error code is 800401E3.

    I've looked around everywere for a solution, but have failed.

    This is the code I use for my function:

        <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 = GetActiveObject("Word.Application")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            Else
                'Create a new instance of the word application
                LobjWordApplication = New Word.Application
            End If
    
            GetRunningWord = LobjWordApplication
    
        End Function
    


    The error is shown during debugging and also when compiled.

    Hope anybody can help.

    Also, this is in Microsoft Office 2007, 32-bit. 


    Best Regards Peter Karlström Midrange AB, Sweden
    Friday, November 4, 2011 3:50 PM

Answers

  • I solved it myself.

    Problem was impersonation in the Web.config file.

    It was another user starting Word than it was running the WebService.

    Hope this helps someone else getting this error.

     


    Best Regards Peter Karlström Midrange AB, Sweden
    Monday, November 7, 2011 10:46 AM

All replies

  • To use Marshal have you included a reference to the runtime services?

    Imports System.Runtime.InteropServices
    


     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Saturday, November 5, 2011 11:30 AM
  • Hi Rich

    Yes. As I said, the project compiles.

    I use the function as this in the WebMethod of the Webservice:

    Dim wrdApp as Word.Application
    
    wrdApp = GetRunningWord
    If wrdApp <> Nothing
       'Creates classes and executes stuff
    
    Else
       'Generate error
    
    End if
    


    Best Regards Peter Karlström Midrange AB, Sweden
    Saturday, November 5, 2011 11:52 AM
  • Hi Peter,

    You should be aware of that manipulate Office  object model in server-side is not recommended by Microsoft:

    Considerations for server-side Automation of Office

    The recommended approach to create and edit Office document in server-side is using Open XML SDK. Here is a sample for your reference:

    http://msdn.microsoft.com/en-us/library/bb735940(v=office.12).aspx

    http://msdn.microsoft.com/en-us/library/bb735939(v=office.12).aspx

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, November 7, 2011 8:08 AM
    Moderator
  • Hi Calvin

     

    Sorry, but this is no help.

    Even though this is not recommended by Microsoft, this solution has worked like a charm for over a year, created + 10.000 documents, spreadsheets and presentations.

    My question is if it's possible to get "re-attached" to a previously started Word application using Marshal.GetActiveObject, or if there is another approach to this.

    The handle to the Word-application created by the first call to the webservice, is lost when the call to the webservice method is finished.

     

    Thanks in advance


    Best Regards Peter Karlström Midrange AB, Sweden
    Monday, November 7, 2011 8:37 AM
  • I solved it myself.

    Problem was impersonation in the Web.config file.

    It was another user starting Word than it was running the WebService.

    Hope this helps someone else getting this error.

     


    Best Regards Peter Karlström Midrange AB, Sweden
    Monday, November 7, 2011 10:46 AM