none
DocumentOpen changes in Office 2010 RRS feed

  • Question

  • Hi

    I have a couple of Office 2007 VSTO Addins developed in VS2008 which is now lifted to Office 2010 using VS2010.

    I have just noticed that Microsoft has changed the way built in events is handled in Word 2010.
    Our Addin processes documents from a web based Document Managing system, and in Word 2010, if Word is not started, the DocumentOpen event (and WindowsActivate) is not processed in the same way it was in Word 2007.
    Also if a user double-clicks a document in Windows explorer, the same problems occurs if Word is not already started.

    Even if I'm very curious about WHY, my questions is;

    1. How to handle this in my VB.NET solutions?
    2. Is it the same changes made for Excel and Powerpoint?

    I have seen some short examples of how to do this for Word in C# for the Startup event, but I would appreciate any help in VB.

    Thanks in advance


    Best Regards Peter Karlström Midrange AB, Sweden

    Wednesday, August 15, 2012 6:10 AM

Answers

  • I see. Good explanation.

    I think you've already found the solution. Navneet is saying you need to move the place where you subscribe to the DocumentOpen event. Here's my translation:


    1. Declare a private variable called 'initialized', type Boolean, in class ThisAddIn

    Public Class ThisAddIn
    
        Dim initialized As Boolean = False
    
        'Other code
    
    End Class


    2. Create a new function in class ThisAddIn called InitializeCustom, and inside it subscribe to DocumentOpen event

    Public Class ThisAddIn
        'Other code
        Private Sub InitializeCustom()
            initialized = True
            AddHandler Application.DocumentOpen, AddressOf appWord_DocumentOpen
        End Sub
    End Class


    3. Call InitializeCustom from Initialize method (which you will find in the codesheet ThisAddIn.Desiger.vb. The easiest way to find this is to press Ctrl-F and enter 'Protected Overrides Sub Initialize')

    Protected Overrides Sub Initialize()
        'Other code. InitializeCustom must be called at the END of this sub
        Me.InitializeCustom()
    End Sub

    4. As a test, display a message if ThisAddIn_Startup fires before events are subscribed

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        If Not initialized Then
            MsgBox("Error: AddIn startup fired before DocumentOpen event subscribed")
        End If
    End Sub
    • Edited by JosephFox Wednesday, August 15, 2012 10:26 PM Improved code for step 2
    • Marked as answer by Peter Karlström Friday, August 17, 2012 8:22 AM
    Wednesday, August 15, 2012 10:01 PM

All replies

  • Unfortunately, Microsoft don't recommend using Interop between computers. This explains why, and suggests alternatives.

    Wednesday, August 15, 2012 5:50 PM
  • This is not interop. As I wrote; The same problem occurs if you double-click a file in Windows explorer.


    Best Regards Peter Karlström Midrange AB, Sweden

    Wednesday, August 15, 2012 5:53 PM
  • Could you describe the problem a bit more? What is the specific unwanted behavior?

    Edit: The "DocumentOpen" event is part of the Interop namespace. If you're literally talking about opening a file, then it's an end user problem.

    Edit2: Oh, I see. You thought I meant 'interop' as in 'between two computers'....there are a lot of overloaded phrases in computing!
    • Edited by JosephFox Wednesday, August 15, 2012 6:17 PM
    Wednesday, August 15, 2012 6:13 PM
  • Hi Joseph

    OK, here's a bit more details.

    These Add-ins are ordinary end-user VSTO Add-ins to Word, Excel and Powerpoint.
    They are loaded from the Addin Startup event, where all initiations are made.

    The Web-based document managing system has a web-browser interface which the end-users access through Internet Explorer.

    When a document is "checked out" from the document managing system, the web-server writes the requested document to the end users local disk, and the document is opened using the files extensions associated application, in this case Word.
    This is actually the same behaviour as if the user double-clicks a Word document file in Windows explorer.

    In Word 2007 and in versions before, the DocumentOpen event is triggered (by Word) after the inititiation events (Addin Startup). With this behavior I can be sure that the Addins code in the DocumentOpen event is executed for the opened document.

    In Word 2010, Microsoft has changed the loading behaviour, so that the DocumentOpen event (and apperantly others too) is triggered before the Addin Startup event, so my code is not executed for the opened document.

    The errors occur ONLY if Word is not previously started. If Word is running when the file is opened, DocumentOpen is executed as expected.

    Hope this is enough for you to understand the problem.

    Also, I have found a forum thread explaning this, but the workaround is in C# (which I don't understand well enough) and its only an overall explanation.
    Here is the link:
    MSDN Forum thread


    Best Regards Peter Karlström Midrange AB, Sweden

    Wednesday, August 15, 2012 8:41 PM
  • I see. Good explanation.

    I think you've already found the solution. Navneet is saying you need to move the place where you subscribe to the DocumentOpen event. Here's my translation:


    1. Declare a private variable called 'initialized', type Boolean, in class ThisAddIn

    Public Class ThisAddIn
    
        Dim initialized As Boolean = False
    
        'Other code
    
    End Class


    2. Create a new function in class ThisAddIn called InitializeCustom, and inside it subscribe to DocumentOpen event

    Public Class ThisAddIn
        'Other code
        Private Sub InitializeCustom()
            initialized = True
            AddHandler Application.DocumentOpen, AddressOf appWord_DocumentOpen
        End Sub
    End Class


    3. Call InitializeCustom from Initialize method (which you will find in the codesheet ThisAddIn.Desiger.vb. The easiest way to find this is to press Ctrl-F and enter 'Protected Overrides Sub Initialize')

    Protected Overrides Sub Initialize()
        'Other code. InitializeCustom must be called at the END of this sub
        Me.InitializeCustom()
    End Sub

    4. As a test, display a message if ThisAddIn_Startup fires before events are subscribed

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        If Not initialized Then
            MsgBox("Error: AddIn startup fired before DocumentOpen event subscribed")
        End If
    End Sub
    • Edited by JosephFox Wednesday, August 15, 2012 10:26 PM Improved code for step 2
    • Marked as answer by Peter Karlström Friday, August 17, 2012 8:22 AM
    Wednesday, August 15, 2012 10:01 PM
  • Hi Joseph

    Thanks for your code and your help.

    With a little fixing to get it right in my solution, it finely looks like it's working.
    Thanks.

    After som checking in my solutions, these changes in startup behavior is NOT made in Excel and Powerpoint?


    Best Regards Peter Karlström Midrange AB, Sweden


    Friday, August 17, 2012 7:54 AM
  • I'm afraid I don't know. As far as I know Microsoft have not officially noted these changes...that thread is pretty much the only source of information.

    If you start a thread in the General Office Development forum, eventually Microsoft staff will respond.

    Friday, August 17, 2012 8:29 AM