none
Getting File path from a document when they close RRS feed

  • Question

  • I am trying to retrieve a filename from a word document on the clsoe of the document.

    What we do is we create a file from a template and open it up for the user to modify.  I want to be able to capture the filepath if the user does a saveas and saves it to a different location then the one we give them. I then want to insert that file path into our database.

    I am using events and there does not seem to be an event for after save, so that moves me to the documentbeforeclose event.  In this event, the document object that I have no longer seems to be available. The event itself has an input of type handle that is supposed to be the document, but it is invalid as well.

    Not sure if I have to go a completely different route, or if there is some unknown event I can trap this info in or what.

    Any help would be appreciated.

    Troy

    Monday, January 10, 2011 8:11 PM

Answers

  • Hello!

    In fact there is no AfterSave event. You don´t need to get the new document's path in the before close event.

    Instead I'd suggest you to do this in your document before save event callback:

     

    void Application_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {
                if (SaveAsUI == true)
                {
                    Cancel = true;
                    string oldPath = Application.ActiveDocument.FullName;
                    SaveAsUI = false;
                    Word.Dialog dlg = this.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileSaveAs];
                    object oDlg = (object)dlg;
                    dlg.Show(ref missing);

                    MessageBox.Show(Application.ActiveDocument.FullName);//it should print the new path

    ...    

     

    I hope I help you! :)


    Booleana
    • Marked as answer by Bessie Zhao Wednesday, January 19, 2011 12:21 PM
    Saturday, January 15, 2011 6:50 PM

All replies

  • Hi Troy,

    You can intercept the Save As process by creating a macro with the name 'FileSaveAs'. By doing so, you can capture any change in the path.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Monday, January 10, 2011 10:47 PM
  • Hello!

    In fact there is no AfterSave event. You don´t need to get the new document's path in the before close event.

    Instead I'd suggest you to do this in your document before save event callback:

     

    void Application_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {
                if (SaveAsUI == true)
                {
                    Cancel = true;
                    string oldPath = Application.ActiveDocument.FullName;
                    SaveAsUI = false;
                    Word.Dialog dlg = this.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileSaveAs];
                    object oDlg = (object)dlg;
                    dlg.Show(ref missing);

                    MessageBox.Show(Application.ActiveDocument.FullName);//it should print the new path

    ...    

     

    I hope I help you! :)


    Booleana
    • Marked as answer by Bessie Zhao Wednesday, January 19, 2011 12:21 PM
    Saturday, January 15, 2011 6:50 PM
  • Hi Troy

    If the answers you've received don't help you, please specify

    1. Version of Word you're targeting

    2. The programming language you're using

    3. The type of solution (VSTO, WinForm, ConsoleApp, VBA macro embedded in document)


    Cindy Meister, VSTO/Word MVP
    Sunday, January 16, 2011 8:42 AM
    Moderator
  • Thanks this looks like it would work.  We were in a time crunch and just altered our program to force them to pick the filename before we open up word.  But this is good to know.

    Thanks

    Wednesday, January 19, 2011 3:04 PM