1

In Word, I have an open document - I navigate in the 'Save As' dialog to a directory and select an existing file. When I now click 'Save' instead of 'Cancel', I get the message if I want to overwrite/merge the existing document.

Is it possible to intercept the 'Save' event in the 'Save As' Dialog so that I can change the file name of the open document, suppressing the overwrite/merge message? Any suggestions are greatly appreciated!

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
alpensturm
  • 39
  • 3
  • https://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.beforesave.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2 – Jim Hewitt Aug 29 '16 at 18:49

2 Answers2

3

Yes, it is totally possible to intercept Word commands. In the VBA days, it was as easy as creating a macro with the same name as the internal Word command.

In VSTO, you need to add the command overwrite to your Ribbon XML and then add a callback to your code.

The entire procedure is described in MSDN: Temporarily Repurpose Commands on the Office Fluent Ribbon

Sample Ribbon XML (overwriting the standard Save command)

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" 
   onLoad="OnLoad" > 
   <commands> 
     <command idMso="FileSave" onAction="mySave" /> 
   </commands> 
   <ribbon startFromScratch="false"> 
     <tabs> 
       <tab id="tab1" label="Repurpose Command Demo" > 
         <group id="group1" label="Demo Group"> 
           <toggleButton id="togglebutton1"  
             imageMso="AcceptInvitation"  
             size="large"  
             label="Alter Built-ins"  
             onAction="changeRepurpose" /> 
         </group> 
       </tab> 
     </tabs> 
   </ribbon> 
</customUI>

Ribbon Callback

public void mySave(IRibbonControl control, bool cancelDefault)
{
    MessageBox.Show("The Save button has been temporarily repurposed.");
    cancelDefault = false;
}
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • I guess, you would also need to catch the Save event in case some one saves via CTRL+S or F12? – PetLahev Aug 26 '16 at 09:32
  • @PetLahev: That's already done -- the entine `FileSave` command is intercepted, not a single click somewhere in the Ribbon. – Dirk Vollmar Aug 26 '16 at 09:38
1

You can replace the Office dialog by your own like this

https://msdn.microsoft.com/en-us/library/sfezx97z(v=vs.110).aspx

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Globals.ThisAddIn.Application.DocumentBeforeSave += Application_DocumentBeforeSave;

}    

void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
{
    SaveFileDialog dgSave = new SaveFileDialog();
    dgSave.Title = "This is my save dialog";
    dgSave.FileName = "This is the initial name";
    dgSave.InitialDirectory = "C:\\Temp";
    dgSave.FileOk += dgSave_FileOk;
    DialogResult result = dgSave.ShowDialog();
}

void dgSave_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
    var test = (SaveFileDialog)sender;
    MessageBox.Show("You clicked on SAVE and this file is selected " + test.FileName);
}

Note: usually you will work with the result and then doing your action rather than catching the FileOk event but it sounds like in this case you want to do that

PetLahev
  • 688
  • 3
  • 18
  • Thanks a lot - very helpful - I need to stick with the Office (Word) dialog though, because if I create a new SaveFileDialog dialog, my namespace extension is not working properly. – alpensturm Aug 31 '16 at 13:27
  • Why? Note: I incorrectly wrote *your own* but I meant the standard .NET dialog. Basically you use my code which uses the Windows SaveFileDialog, and I think that MS Office uses the same, set the Cancel = true and handle it in that method. There should not be any problem with namespace – PetLahev Aug 31 '16 at 14:27