1

我想使用我的 VSTO 加载项中的内置打开文件对话框。显示对话框时我必须设置InitialFileName。不幸的是,Dialog 类中不存在此属性:

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

尝试将其转换为FileDialog也不起作用:

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

我在这里想念什么?

注意:我使用的是 Add-in Express。

4

3 回答 3

1

知道了。我必须强制转换我的应用程序对象Microsoft.Office.Interop.Word.Application才能访问该FileDialog成员。以下代码有效:

var Dlg = ((Microsoft.Office.Interop.Word.Application)Word).get_FileDialog(MsoFileDialogType.msoFileDialogFilePicker);
Dlg.InitialFileName = STRfolderroot + STRfoldertemplatescommon + "\\" + TheModality + "\\" + TheModality + " " + TheStudyType + "\\";
Dlg.Show();
于 2015-07-04T10:36:06.610 回答
0

您帖子中的 Microsoft 页面显示了用于msoFileDialogFilePicker对话框的属性,但您的代码使用的是wdDialogFileOpen. MS 页面上的示例代码工作正常,但尝试使用该属性wdDialogFileOpen也会产生运行时错误。

所以这有效:

Sub ThisWorks()

    Dim fd As FileDialog

    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    Dim vrtSelectedItem As Variant

    With fd
        .InitialFileName = "C:\folder\printer_ink_test.docx"

        'If the user presses the action button...
        If .Show = -1 Then

            For Each vrtSelectedItem In .SelectedItems
                MsgBox "Selected item's path: " & vrtSelectedItem
            Next vrtSelectedItem
        'If the user presses Cancel...
        Else
        End If
    End With

    Set fd = Nothing

End Sub

但这失败了:

Sub ThisFails()

    Dim fd As Dialog

    Set fd = Application.Dialogs(wdDialogFileOpen)

    Dim vrtSelectedItem As Variant

    With fd
        ' This line causes a run-time error
        .InitialFileName = "C:\folder\printer_ink_test.docx"

        'If the user presses the action button...
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
                MsgBox "Selected item's path: " & vrtSelectedItem
            Next vrtSelectedItem
        'If the user presses Cancel...
        Else
        End If
    End With

    Set fd = Nothing

End Sub
于 2015-07-04T08:36:39.093 回答
0

抱歉截图,我正在用我的手机接听。

根据谷歌书籍中的图片,这就是你为 Excel 做的事情:Globals.ThisWorkbook.ThisApplication.FileDialog

在此处输入图像描述

对于根据此链接的 MS Word ,它是这样做的:

Office.FileDialog dialog = app.get_FileDialog(
  Office.MsoFileDialogType.msoFileDialogFilePicker);
//dialog.InitialFileName  <-- set initial file name
dialog.Show();
于 2015-07-04T10:39:33.963 回答