none
Problem with Document Change Event RRS feed

  • Question

  • I have developed an MS Word 2010 Addin using Visual Studio 2010. This addin uses XML for backstage UI.

    <?xml version="1.0" encoding="utf-8" ?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <backstage>
        <button id="ODTConvertor" getEnabled="GetEnabled" insertAfterMso="TabInfo" label=" Convert ODT To MS Word" imageMso="FileSaveAs" onAction="OnAction" isDefinitive="true" />
      </backstage>
    </customUI>

    Now I have a condition in which if only when the user opens .ODT file this button remains enabled. Rest of th time it should remain disabled. For that i have written this code

    1) First at OnStartComplete() even I created an event of OnDocumentChange, as you can see in Bold

    public void OnStartupComplete(ref System.Array custom)
    {
         //oWord = new MSWord.Application();
         oWord = (MSWord.Application)applicationObject;
         oWord.Visible = true;
    
         ribbon.InvalidateControl("ODTConvertor");
         oWord.DocumentChange += new MSWord.ApplicationEvents4_DocumentChangeEventHandler(oWord_DocumentChange);
    }

    Now inside DocumentChange event I check if the currently loaded document has .odt extension name. I call

    void oWord_DocumentChange()
    {
          File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
          Extension = Path.GetExtension(File);
          ribbon.InvalidateControl("ODTConvertor");
    }
    
    public bool GetEnabled(IRibbonControl control)
    {
         return (Extension == ".odt") ? true : false;
    }

    Now for Invalidate Control I have use Ribbon_Load event

    public void Ribbon_Load(Office.IRibbonUI ribbonUI)
    {
         this.ribbon = ribbonUI;
    }

    This whole thing works perfectly fine. But in 2 cases it doesn't work

    1) When someone opens an .ODT document. The button is enabled. But when the user closes that .odt document the button remains enabled only. On other hand when a user loads some other document after loading .odt file it successfully disables the button.

    2) When user opens a new document or an existing word document (.docx). The button is disabled because its not an .ODT document. But when the user saves that document into .odt using File>Save As then the currently loaded document is an .odt file. But the buttons still remains disabled.

    Is it that InvalidateControl is not properly getting fired? Can someone help me in resolving these 2 issues?

    Thanks in Advance


    Adeel

    Wednesday, August 1, 2012 12:53 PM

Answers

  • If you want code to run when a document closes, you can use the DocumentBeforeClose. Here are a list of events.
    • Edited by JosephFox Friday, August 3, 2012 11:04 AM
    • Marked as answer by maverick786us Friday, August 3, 2012 11:52 AM
    Friday, August 3, 2012 10:45 AM

All replies

  • I think that's the DocumentChange event working as Microsoft intended.
    Wednesday, August 1, 2012 8:54 PM
  • Then what is the solution to this? Closing a document too should come into DocumentChange event so why is it not happening?

    Adeel


    Friday, August 3, 2012 3:57 AM
  • If you want code to run when a document closes, you can use the DocumentBeforeClose. Here are a list of events.
    • Edited by JosephFox Friday, August 3, 2012 11:04 AM
    • Marked as answer by maverick786us Friday, August 3, 2012 11:52 AM
    Friday, August 3, 2012 10:45 AM
  • Hi,

    Try to use Application_WindowActivate event instead of change.

    Thanks.


    Hope this helps!! Regards, Pranav Vaidya PS: If my reply has resolved the issue you are facing please mark it as answer so that it will help others on the forum who may have similar issue.

    Tuesday, August 7, 2012 5:55 PM
  • Hello,

    Why not use all three events? (Application.DocumentChange, Document.BeforeSave, Application.DocumentBeforeClose)

    Best regards,

    Silviu.


    http://www.rosoftlab.net/

    Tuesday, August 14, 2012 6:07 AM