问题
I'm writing a Word COM Add-in that replaces the Open & Save dialogs with my own.
For the save dialog, I'm handling the documentBeforeSave event from the application events. This works fine.
For the open dialog, there is no such event, so I'm currently handling the onClick of the Open... menu item, canceling the default handling. This works ok if the user indeed uses this menu item, but if the user presses CTRL-O in stead they still get the original dialog.
Is there a better way to hook into this dialog? And if there isn't, is there a way to elegantly handle this keypress, or should I resolve to keyboard hooks?
Note: The add-in should eventually work on Office 2003, 2007 and 2010, but using different code-paths on different targets is of course perfectly fine. I am interested in any solutions on any version.
回答1:
In Word 2007+, this turns out to be incredibly simple to implement. Simply repurpose the FileOpen command through the ribbon XML
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<commands>
<command idMso="FileOpen" onAction="doOpen" />
</commands>
...
The doOpen method then has two parameters, the second being an in/out parameter allowing you to cancel the event.
For previous version of Office, I've never implemented a fully bulletproof solution.
来源:https://stackoverflow.com/questions/1395434/replacing-words-open-file-dialog-in-a-com-add-in