none
Document Save Event RRS feed

  • Question

  • Just like the way we have DocumentChange event

    Example:

    oWord.DocumentChange += new MSWord.ApplicationEvents4_DocumentChangeEventHandler(oWord_DocumentChange);

    I also want to Capture an event that is fired when the document is saved ( It could be normal save or Save As) So which event will it be? There is an event named DocumentBeforeSave. But I want an event that is DocumentAfterSave which doesn't exist.


    Adeel

    Wednesday, August 1, 2012 9:47 AM

Answers

  • I don't know why that didn't work. Here's a solution I've just tested:

            private bool bAllowSave = false;
    
            void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {
                if (!bAllowSave)
                {
                    bAllowSave = true;
    
                    if (SaveAsUI)
                    {
                        // Display Save As dialog
                        Word.Dialog d = this.Application.Dialogs[Word.WdWordDialog.wdDialogFileSaveAs];
                        object timeOut = 0;
                        d.Show(ref timeOut);
                    }
                    else
                    {
                        // Save without dialog
                        Doc.Save();
                    }
    
                    File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
                    Extension = Path.GetExtension(File);
                    ribbon.InvalidateControl("ODTConvertor");
    
                    bAllowSave = false;
                    Cancel = true;
                    return;
                }
            }
    You may be able to set the default save format to ODT...but I haven't attempted it before. If you're interested, start looking here and here.

    • Marked as answer by maverick786us Monday, August 6, 2012 4:58 AM
    Friday, August 3, 2012 1:57 PM

All replies

  • Yeah it would have saved me a ton of hassle if Word a DocumentAfterSave event.

    There are workarounds, but they're not hassle free. Probably the easiest is to handle the DocumentBeforeSave event, cancel the save (assuming you're doing this from within an AddIn), and instead save it with your own command, after which you can enact more code.

    You need to couch this with a global (or class level) 'ignore' bool, so when you enact a save it won't get trapped, endlessly saving....something like:

    bool allowSave = false;

    BeforeSaveEvent(Document doc, ref bool cancel)

    {

      if (!allowSave)

      {

        // Before save code

        allowSave = true;

       doc.Save();

        // After save code

        allowSave = false;

        cancel = true; // Stop Word doing its normal save: we have already saved the document

      }

    }

    Wednesday, August 1, 2012 11:18 AM
  • Can you suggest me some trick in this case? Because if I use this code DocumentBeforeSaveAs...

    void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
          File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
          Extension = Path.GetExtension(File);
          ribbon.InvalidateControl("ODTConvertor");
    }

    Now when a user want to save this document as .ODT the above code should enable the button the moment it encounters extension to be .ODT. But the problem is the above mentioned code is executed BEFORE the save as dialog box where the user choose .ODT format for saving the file. So in this case it will never enable the button the moment .ODT document that was saved is loaded by default.

    Adeel

    Friday, August 3, 2012 11:48 AM
  • I think you want:

    bool allowSave = false;
    
    void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
      if (!allowSave)
    
      {
        allowSave = true;
    
        Doc.Save();
    
        File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
        Extension = Path.GetExtension(File);
        ribbon.InvalidateControl("ODTConvertor");
    
        allowSave = false;
    
        Cancel = true; // Stop Word doing its normal save: we have already saved the document
    
      }
    }


    • Edited by JosephFox Friday, August 3, 2012 12:14 PM forgot to update variable names to your situation
    Friday, August 3, 2012 12:13 PM
  • I think you want:

    bool allowSave = false;
    
    void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
      if (!allowSave)
    
      {
        allowSave = true;
    
        Doc.Save();
    
        File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
        Extension = Path.GetExtension(File);
        ribbon.InvalidateControl("ODTConvertor");
    
        allowSave = false;
    
        Cancel = true; // Stop Word doing its normal save: we have already saved the document
    
      }
    }


    Thanks. This is perfect the only thing that is missing is ...

    void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
         if (!bAllowSave)
         {
               bAllowSave = true;
               Doc.Save();
    
               File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
               Extension = Path.GetExtension(File);
               ribbon.InvalidateControl("ODTConvertor");
    
               bAllowSave = false;
               Cancel = true;
         }
    }
    this Doc.Save doesn't invoke the SaveAs dialog where the user will specify the .ODT format. So by default it saves in .docx format instead of .odt format. Is there a way to invoke save as dialog?

    Adeel

    Friday, August 3, 2012 12:38 PM
  • bool allowSave = false; bool showSaveAsUI = false; void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel) { if (!allowSave) { allowSave = true; showSaveAsUI = SaveAsUI; Doc.Save(); File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString(); Extension = Path.GetExtension(File); ribbon.InvalidateControl("ODTConvertor"); allowSave = false; Cancel = true; } SaveAsUI = showSaveAsUI; }

    Well, as you may guess the SaveAsUI ref bool sets that. So I guess you need to propagate that value to the invoked save.
    • Edited by JosephFox Friday, August 3, 2012 12:59 PM
    Friday, August 3, 2012 12:59 PM
  • I am afraid this thing doesn't seem to show SaveAs Dialog

    private bool bAllowSave = false;
    private bool showSaveAsUI = false;
    
    void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
          if (!bAllowSave)
          {
                bAllowSave = true;
                showSaveAsUI = SaveAsUI;
                Doc.Save();
    
                File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
                Extension = Path.GetExtension(File);
                ribbon.InvalidateControl("ODTConvertor");
    
                bAllowSave = false;
                Cancel = true;
          }
          SaveAsUI = showSaveAsUI;
    }

    I hope something isn't missed out

    Adeel

    Friday, August 3, 2012 1:20 PM
  • I don't know why that didn't work. Here's a solution I've just tested:

            private bool bAllowSave = false;
    
            void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {
                if (!bAllowSave)
                {
                    bAllowSave = true;
    
                    if (SaveAsUI)
                    {
                        // Display Save As dialog
                        Word.Dialog d = this.Application.Dialogs[Word.WdWordDialog.wdDialogFileSaveAs];
                        object timeOut = 0;
                        d.Show(ref timeOut);
                    }
                    else
                    {
                        // Save without dialog
                        Doc.Save();
                    }
    
                    File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString();
                    Extension = Path.GetExtension(File);
                    ribbon.InvalidateControl("ODTConvertor");
    
                    bAllowSave = false;
                    Cancel = true;
                    return;
                }
            }
    You may be able to set the default save format to ODT...but I haven't attempted it before. If you're interested, start looking here and here.

    • Marked as answer by maverick786us Monday, August 6, 2012 4:58 AM
    Friday, August 3, 2012 1:57 PM
  • Finally it worked. It was the Word Dialog solved the thing. Thank you for all the help that you've recently provided in this forum section

    Adeel


    Monday, August 6, 2012 7:20 AM