Search code examples
c#.netvbavstoaddin-express

Using Word's Open File Dialog


I want to use built-in Open File dialog from my VSTO add-in. I have to set InitialFileName when showing the dialog. Unfortunately, this property does not exist in the Dialog class:

var Dlg = Word.Dialogs.Item(WdWordDialog.wdDialogFileOpen);
Dlg.InitialFileName = SomePath; //COMPILE ERROR: no such property

Try to cast it to FileDialog also doesn't work:

var Dlg = Word.Dialogs.Item(WdWordDialog.wdDialogFileOpen) as FileDialog;
Dlg.InitialFileName = SomePath; //RUNTIME EXCEPTION: null reference

What am I missing here?

Note: I'm using Add-in Express.


Solution

  • Got it. I had to cast my application object to Microsoft.Office.Interop.Word.Application to get access to the FileDialog member. The following code works:

    var Dlg = ((Microsoft.Office.Interop.Word.Application)Word).get_FileDialog(MsoFileDialogType.msoFileDialogFilePicker);
    Dlg.InitialFileName = STRfolderroot + STRfoldertemplatescommon + "\\" + TheModality + "\\" + TheModality + " " + TheStudyType + "\\";
    Dlg.Show();