none
Word 2007 Add In - OnClick event in a WinForm, get active document and call a function RRS feed

  • Question

  • Hello!

    I have three projects (currnetly) within my solution:

    1. Word 2007 Document-project

    - Consist of Template.docx and ThisDocument.cs...

    2. Word 2007 AddIn-project

      - Contains a ribbon (menu) plugin for Word 2007,m which is loaded upon starting word 2007.

    3. Windows Class Library

           - Contains WinForms that shall be reused within the solution (and maybe other solutions later)

    -Contains also classes, database model, linq to sql, etc...

    Scenario I want

    A button in the custom-ribbon-menu is clicked from AddIn-project. A reference to Library-project has been created, so I can create any WinForm within the Library-project and display them by calling Show().

    public void OnRibbonClick()
    {
      Library.Form1 f = new Library.Form1();
      f.Show();
    }

    So far, so good.

    The WinForm has a button, when this button is clicked I want to call one of the functions:

    A. Function located within Word 2007 Document-project, within ThisDocument.CS file.

    OR

    B. Get the Current Active Document from Word 2007 (a handler) and cast it to a Template, so I can call functions within this template. 

    BtnOK is clicked within the WinForm:

                
       private void btnOK_Click(object sender, EventArgs e)
            {
            //The WinForm is within the library-project
            }

    If the WinForm was located within "AddIn" I can recieve active document by:

    Globals.ThisAddIn.Application.ActiveDocument

    But since the WinForm is within "Library" project I cannot reach Globals, nor the active document.

    I want to reuse this WinForm in other word-templates (documents), hence I want to put it in the library project instead...


    Suggestions? 

    How can I recieve the Active Document from within a Window-class library or the "ThisAddin" handler?






    • Edited by colaohye Friday, September 21, 2012 11:46 AM
    Friday, September 21, 2012 10:55 AM

Answers

  • Hi cola

    <<

    How can I recieve the Active Document from within a Window-class library or the "ThisAddin" handler?>>

    Best would probably be to pass the Word.Document object to the WinForm via its constructor.

    Instead of this:

    Library.Form1 f = new Library.Form1();

    Do this:

    Library.Form1 f = new Library.Form1(Globals.ThisAddIn.Application.ActiveDocument);

    And in Library.Form1 you have instead of public void new() {}

    public void new(Word.Document doc){ }

    Use that to populate a class level object if you need to work with the object outside the constructor.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 21, 2012 3:25 PM
    Moderator
  • Hello colaohya,

    I think a good solution for this would be to add an event to the Library.Form1 class that gets raised when the 'OK' button is hit.  So in the OnRibbonClick function, you would subscribe to that event with something like f.EventOK += new EventHandler(_RibbonClicked);

    You would then have subscribed to it from the Addin level (the ribbon) so you have access to all your documents -- including the active document.

    I would be worried about passing around the "Active Document" at the time of construction just because the Active Document can change behind the scenes, and it might not be the activedocument by the time the "OK" button is clicked.

    Good luck!
    ~Joe


    teamsystemsolutions.com | Enriching TFS through Office integration

    Friday, September 21, 2012 6:42 PM

All replies

  • Hi cola

    <<

    How can I recieve the Active Document from within a Window-class library or the "ThisAddin" handler?>>

    Best would probably be to pass the Word.Document object to the WinForm via its constructor.

    Instead of this:

    Library.Form1 f = new Library.Form1();

    Do this:

    Library.Form1 f = new Library.Form1(Globals.ThisAddIn.Application.ActiveDocument);

    And in Library.Form1 you have instead of public void new() {}

    public void new(Word.Document doc){ }

    Use that to populate a class level object if you need to work with the object outside the constructor.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 21, 2012 3:25 PM
    Moderator
  • Hello colaohya,

    I think a good solution for this would be to add an event to the Library.Form1 class that gets raised when the 'OK' button is hit.  So in the OnRibbonClick function, you would subscribe to that event with something like f.EventOK += new EventHandler(_RibbonClicked);

    You would then have subscribed to it from the Addin level (the ribbon) so you have access to all your documents -- including the active document.

    I would be worried about passing around the "Active Document" at the time of construction just because the Active Document can change behind the scenes, and it might not be the activedocument by the time the "OK" button is clicked.

    Good luck!
    ~Joe


    teamsystemsolutions.com | Enriching TFS through Office integration

    Friday, September 21, 2012 6:42 PM
  • <<I would be worried about passing around the "Active Document" at the time of constru>>

    If that's an issue, then it's no problem to first assign ActiveDocument to a Word.Document object, which will then remain static.

    Word.Document doc = ActiveDocument


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, September 22, 2012 5:55 AM
    Moderator
  • Have implemented Forms to take the active document and application in the constructor.

    Have also implemented custom events for a couple of forms, and both solutions seems to work for the moment, so, thanks to both! :)

    Monday, September 24, 2012 7:30 AM