How to check that Word 2007 invoke autosave into w

2019-07-18 14:20发布

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?

2条回答
你好瞎i
2楼-- · 2019-07-18 15:02

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).

查看更多
Summer. ? 凉城
3楼-- · 2019-07-18 15:10

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.

查看更多
登录 后发表回答