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 に答える