【问题标题】:Using Word's Open File Dialog使用 Word 的打开文件对话框
【发布时间】:2015-07-04 07:44:25
【问题描述】:

我想使用我的 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。

【问题讨论】:

    标签: c# .net vba vsto addin-express


    【解决方案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();
    

    【讨论】:

    • 不错的一个!很高兴你把它整理好了:)
    【解决方案2】:

    您帖子中的 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
    

    【讨论】:

    • 感谢您的输入,但我在 VSTO 的任何地方都没有看到 FileDialog 选项。请注意,与 MSDN 页面不同,我没有使用 VBA。
    • 文章告诉我应该在 C# 中使用 get_FileDialog() 方法,但即使在 Application 类中也不存在。如果这在这里发挥了作用,我正在使用 Add-in Express。
    【解决方案3】:

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

    根据谷歌图书中的图片,这就是您在 Excel 中的操作方式:Globals.ThisWorkbook.ThisApplication.FileDialog

    根据link,对于 MS Word,它是这样完成的:

    Office.FileDialog dialog = app.get_FileDialog(
      Office.MsoFileDialogType.msoFileDialogFilePicker);
    //dialog.InitialFileName  <-- set initial file name
    dialog.Show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      相关资源
      最近更新 更多