none
Displaying custom Save dialog in word application RRS feed

  • Question

  • Hi,

    I have created a custom Addin in word application. The addin contains a search button that displays a customtaskpane on click of it. To keep track of active document and related customtaskpane, i have used dictionary object with document absolute path as key and customtaskpane collection as pair. If a new document is saved i need to update the dictionary object for which i have used the following code:


            private void Application_DocumentBeforeSave(InteropWord.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {
                try
                {               
                    string docName = Doc.FullName;
                    OfficeTools.CustomTaskPane pane = null;
                  
                    if(SaveAsUI==true && Cancel== false)
                    {
                        Doc.Save ();
                      
                        if (Doc.Saved == true)
                        {
                            foreach (KeyValuePair<string, Microsoft.Office.Tools.CustomTaskPane> pair in Globals.ThisAddIn.paneCollection)
                            {
                                if (pair.Key == docName)
                                {                             
                                    pane = pair.Value;
                                    paneCollection.Remove(pair.Key);
                                    paneCollection.Add(Doc.FullName, pane);
                                    break;
                                }
                            }
                        }
                    }
                }
                catch(COMException ex)
                {
                      
                }   
             
            } 

    Doc.Save() displays a SaveAs dialog when the document is not yet saved. But on cancewl button click its throws an exception "Command Failed." Please let me know if this the right way of calling the save method. And also i tried using Application.FileDialog[Office.MsoFileDialogType.msoFileDialogSaveAs].Show(); But this doesn't show the prepopulated file name and location in save as dialog.

    Regards,

    Megarekaa.

     

     

     

    Friday, December 30, 2011 8:06 AM

Answers

All replies

  • It seems what you really need is an 'after save' event, and there isn't one.

    The save dialogue will appear after the code in the 'before save' event handler, assuming you don't change the cancel bool. What you're doing is invariably going to cause problems, because you're commanding it to begin the save process as a corrollary of saving.

    Can you not purely have:

     foreach (KeyValuePair<string, Microsoft.Office.Tools.CustomTaskPane> pair in Globals.ThisAddIn.paneCollection)
    {
    if (pair.Key == docName)
    {
    pane = pair.Value;
    paneCollection.Remove(pair.Key);
    paneCollection.Add(Doc.FullName, pane);
    break;
    }
    }

    and then let it save afterwards?

    Friday, December 30, 2011 2:04 PM
  • Hi Megarekaa

    One possibility would be for you to set the Cancel argument = True and SaveAsUI = False so that Word doesn't try to use the built-in functionality itself.

    Then you can display the built-in SaveAs dialog box yourself, using the Display method. In that mode, the dialog box doesn't try to execute. You can get the settings and perform the Save yourself if Cancel hasn't been chosen.

    But since you're using C# you'd have to work with late binding (PInvoke) if you're not targeting .NET 4.0.

    I might use this in combination with re-purposing the FileSave and FileSaveAs commands using Ribbon XML. Then you wouldn't need to bother with the BeforeSave event.

    There are some discussions that show the basics of working with PInvoke and the Save As dialog box:
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/1b0e9dd6-599e-41be-a8a6-2a2d0df778e6/
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/4f9d4fa8-5dea-4010-8a16-4c59fb58b3c7
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/24b9172d-9af6-41e9-8cf3-580607436543


    Cindy Meister, VSTO/Word MVP
    Saturday, December 31, 2011 11:06 AM
    Moderator
  • Hi,

    Thanks for the reply. The above links were useful and was able to solve the issue using the same.

    Regards,

    Megarekaa.

     

    Wednesday, January 4, 2012 9:03 AM