none
ActiveDocument RRS feed

  • Question

  • I have developed a MS Word 2010 addin using Visual Studio 2010. I am using DocumentChange event so that I can figure out when a new or existing document is loaded. I am using code for that.

    using MSWord = Microsoft.Office.Interop.Word;
    --------------------------------------------
    --------------------------------------------
    --------------------------------------------
    private MSWord.Application oWord;
    private MSWord.Application oDocument;
    --------------------------------------------
    --------------------------------------------
    --------------------------------------------
    public Connect()
    {
        oWord = new MSWord.Application();
        oWord.Visible = true;
    
        oWord.DocumentChange += new MSWord.ApplicationEvents4_DocumentChangeEventHandler(oWord_DocumentChange);
    }
    Now can someone suggest me how can I use Active Document in this context? As I have to extract the name, extension and path of the currently loaded document.

    As this thing doesn't seem to work.

    void oWord_DocumentChange()
    {
         string strDoc = oWord.ActiveDocument.Name;
         MessageBox.Show("Document Change Event" + strDoc);
    }


    Adeel

    Tuesday, July 24, 2012 7:39 AM

Answers

  • You're absolutely right that you need to cast the generic application object to a Word Application variable (obviously a different type if you're not designing this as a Word AddIn).

    Is your AddIn loaded when Word starts up? (i.e. load behaviour 3  or somesuch)

    I think the issue may be that this code:

        //oWord = new MSWord.Application();
         oWord
    = (MSWord.Application)applicationObject; // Application Object instead of new Instance
         oWord
    .Visible = true;
         oWord
    .DocumentChange += new MSWord.ApplicationEvents4_DocumentChangeEventHandler(oWord_DocumentChange);

    Is being run too early. Try moving it to your OnStartupComplete method.

    Also, if you have got the arguments to the IDTExensibility2 methods wrong, it may not load. A good way to test that is to comment out (//) all your code except the method declarations themselves, and check that it works.

    • Marked as answer by maverick786us Tuesday, July 24, 2012 9:12 AM
    • Edited by JosephFox Tuesday, July 24, 2012 9:12 AM
    Tuesday, July 24, 2012 9:01 AM

All replies

  • Your handling of the DocumentChange event looks okay to me.

    This is a problem:

    oWord = new MSWord.Application();

    Because you're starting a new instance of Word with that line. You need to get a variable representing the current instance of Word.

    As per this article, you ought to allow your Connect method to take the application argument. Then you can store that at the class level.

    Tuesday, July 24, 2012 8:02 AM
  • Your handling of the DocumentChange event looks okay to me.

    This is a problem:

    oWord = new MSWord.Application();

    Because you're starting a new instance of Word with that line. You need to get a variable representing the current instance of Word.

    As per this article, you ought to allow your Connect method to take the application argument. Then you can store that at the class level.

    Thank you Joseph,

    How can I link it with current instance and usuage of applciation argument instead of new one?


    Adeel


    Tuesday, July 24, 2012 8:26 AM
  • OK I made the change this time I assigned application object instead of new instance. But this time the addin isn't getting loaded :(

    public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
    {
    	applicationObject = application;
    	addInInstance = addInInst;
    }
    
    public Connect()
    {
         //oWord = new MSWord.Application();
         oWord = (MSWord.Application)applicationObject; // Application Object instead of new Instance
         oWord.Visible = true;
         oWord.DocumentChange += new MSWord.ApplicationEvents4_DocumentChangeEventHandler(oWord_DocumentChange);
    }
    
    void oWord_DocumentChange()
    {
         MessageBox.Show("Document Change Event" + oWord.ActiveDocument.Name.ToString());
    }


    Adeel

    Tuesday, July 24, 2012 8:48 AM
  • You're absolutely right that you need to cast the generic application object to a Word Application variable (obviously a different type if you're not designing this as a Word AddIn).

    Is your AddIn loaded when Word starts up? (i.e. load behaviour 3  or somesuch)

    I think the issue may be that this code:

        //oWord = new MSWord.Application();
         oWord
    = (MSWord.Application)applicationObject; // Application Object instead of new Instance
         oWord
    .Visible = true;
         oWord
    .DocumentChange += new MSWord.ApplicationEvents4_DocumentChangeEventHandler(oWord_DocumentChange);

    Is being run too early. Try moving it to your OnStartupComplete method.

    Also, if you have got the arguments to the IDTExensibility2 methods wrong, it may not load. A good way to test that is to comment out (//) all your code except the method declarations themselves, and check that it works.

    • Marked as answer by maverick786us Tuesday, July 24, 2012 9:12 AM
    • Edited by JosephFox Tuesday, July 24, 2012 9:12 AM
    Tuesday, July 24, 2012 9:01 AM
  • You're absolutely right that you need to cast the generic application object to a Word Application variable (obviously a different type if you're not designing this as a Word AddIn).

    Is your AddIn loaded when Word starts up? (i.e. load behaviour 3  or somesuch)

    I think the issue may be that this code:

        //oWord = new MSWord.Application();
         oWord
    = (MSWord.Application)applicationObject; // Application Object instead of new Instance
         oWord
    .Visible = true;
         oWord
    .DocumentChange += new MSWord.ApplicationEvents4_DocumentChangeEventHandler(oWord_DocumentChange);

    Is being run too early. Try moving it to your OnStartupComplete method.

    Also, if you have got the arguments to the IDTExensibility2 methods wrong, it may not load. A good way to test that is to comment out (//) all your code except the method declarations themselves, and check that it works.


    Thanks Joseph when i moved the whole thing from Connect to OnStartupComplete it worked.

    Adeel

    Tuesday, July 24, 2012 9:13 AM