How to check that Word 2007 invoke autosave into word addin?

故事扮演 提交于 2020-01-04 17:33:11

问题


I have some functionality inside DocumentBeforeSave event handler.
That's should work only when user manually invoke Save (press Save button).
But word 2007 have autosave function and event DocumentBeforeSave throws each time when autosave work. How to check that save is invoked over Autosave or User manually invoke Save?


回答1:


It looks like there's no build-in way of doing that because the object model simply doesn't support it (per this link), but you can use VBA to override the default save hotkey and button click, and send those calls to your .NET assembly (per this link). Just make sure you invoke save manually afterwards to make sure the document actually saves.




回答2:


There are actually multiple ways to tell the difference.

Option 1 (best)

Application.WordBasic.IsAutosaveEvent

Option 2 (what I did before finding option 1)

  • Intercept the FileSave (and FileSaveAs) command from a ribbon:

    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
       <commands>
         <command idMso="FileSave" onAction="SaveIntercept" />
       </commands>
     </customUI>
    
  • Set a flag in the SaveIntercept method, and set CancelDefault to false so the save continues.

    public void SaveIntercept(IRibbonControl control, ref bool CancelDefault)
    {
        logger.Info("Intercepted Manual Save");
        ManualSave = true;
        CancelDefault = false;
    }
    
  • Also implement BeforeDocument_BeforeSave, and check the flag there. If the flag was set, it was manual, otherwise it's an autosave (or potentially comes from another Add-In; not sure if that works).

    bool quit = !customizations.ManualSave;
    if (quit)
    {
        logger.Info("Autosave. Allowing Word to handle this save.");
        e.Cancel = false;
        return;
    }
    else
    {
        logger.Info("Manual save. Proceeding.");
        customizations.ManualSave = false;
    }
    

This covers saves through the backstage buttons, the quick access toolbar buttons, and the save shortcut (even if they redefine the keyboard shortcut)

Interestingly, there is a way to tell after the save as well, as described here (updated version here).



来源:https://stackoverflow.com/questions/6414506/how-to-check-that-word-2007-invoke-autosave-into-word-addin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!