none
set document path in word saveas dialog

    Question

  • Hi all,

     

    I hope someone can help me. I'm programming in Visual basic.

    In my word template project I would like to preset the document path (preferably to a sharepoint document library) and the document name in the built-in word dialog when the user presses the save commandbar button or chosses the file save as menu. I haven't had any luck with this.

    Thanks for any help

    Greets

    Speedtriple 

    Thursday, June 01, 2006 9:15 AM

Answers

  • If you were working without VSTO, in a Word template or document, you'd include two procedures in an embedded VBA project: FileSave and FileSaveAs.

    For anything else, you need to sink the DocumentBeforeSave event. For example, in C#:

            private void ThisDocument_BeforeSave(object sender, Microsoft.Office.Tools.Word.SaveEventArgs e)
            {
    //Don't let Word show the dialog box
                e.ShowSaveAsDialog = false;
                Word.Dialog dlg = this.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileSaveAs];
    //Word dialog box parameters have to be accessed via Late-Binding, as they originate in the old WordBasic. So you have to use System.Reflection. To preset the path, use the Name property
                object oDlg = (object)dlg;
                object[] oArgs = new object[1];
                oArgs[0] = (object)@"C:\Test\";
                oDlg.GetType().InvokeMember("Name", BindingFlags.SetProperty, null, oDlg, oArgs);
                dlg.Show(ref missing);
            }

    If you have Option Strict On, then you need to use the above approach, with late-binding. With it off, you should be able to do it similarly to VBA, which is like this:

    With Dialogs(wdDialogFileSaveAs)
      .Name = "C:\Test"
      .Show
    End With

    Friday, June 02, 2006 8:49 AM

All replies

  • If you were working without VSTO, in a Word template or document, you'd include two procedures in an embedded VBA project: FileSave and FileSaveAs.

    For anything else, you need to sink the DocumentBeforeSave event. For example, in C#:

            private void ThisDocument_BeforeSave(object sender, Microsoft.Office.Tools.Word.SaveEventArgs e)
            {
    //Don't let Word show the dialog box
                e.ShowSaveAsDialog = false;
                Word.Dialog dlg = this.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileSaveAs];
    //Word dialog box parameters have to be accessed via Late-Binding, as they originate in the old WordBasic. So you have to use System.Reflection. To preset the path, use the Name property
                object oDlg = (object)dlg;
                object[] oArgs = new object[1];
                oArgs[0] = (object)@"C:\Test\";
                oDlg.GetType().InvokeMember("Name", BindingFlags.SetProperty, null, oDlg, oArgs);
                dlg.Show(ref missing);
            }

    If you have Option Strict On, then you need to use the above approach, with late-binding. With it off, you should be able to do it similarly to VBA, which is like this:

    With Dialogs(wdDialogFileSaveAs)
      .Name = "C:\Test"
      .Show
    End With

    Friday, June 02, 2006 8:49 AM
  • Hello

    I am developing  a word addin 

    and I'm trying and trying  :) to get  the filename of a document after a  "saveAs file dialog"  have been raised.

    can you give me any advice ?

     

     

    Wednesday, January 24, 2007 8:46 AM
  • Wednesday, January 24, 2007 8:11 PM
  •         void Document_BeforeSave(object sender, Tools.SaveEventArgs e)
            {
    	    e.ShowSaveAsDialog = false;
                WordNS.Dialog dialog = this.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileSaveAs];
                dialog.GetType().InvokeMember("Name", BindingFlags.SetProperty, null, dialog, new object[] { "New name" });
                dialog.Show();
            }

    • Edited by Denys W Friday, January 18, 2013 9:31 AM
    Friday, January 18, 2013 9:30 AM