FileDialog オブジェクト (Office)

Microsoft Office アプリケーションでファイルを開いたり保存する標準的な [ファイルを開く] および [保存] ダイアログ ボックスに類似するファイル ダイアログ ボックスの機能を提供します。

解説

FileDialog プロパティを使用して FileDialog オブジェクトを返します。 FileDialog プロパティは、各 Office アプリケーションの Application オブジェクトにあります。 プロパティは、プロパティから返される FileDialog オブジェクトの型を決定する 1 つの引数 MsoFileDialogType を受け取ります。 FileDialog オブジェクトには、次の 4 つの種類があります。

  • [ファイルを開く] ダイアログ ボックス: ユーザーは 1 つ以上のファイルを選択し、Execute メソッドを使用して、ホスト アプリケーションでそのファイルを開くことができます。

  • [名前を付けて保存] ダイアログ ボックス: ユーザーは 1 つのファイルを選択し、Execute メソッドを使用して、そのファイルを保存できます。

  • [ファイル ピッカー ] ダイアログ ボックス: ユーザーは 1 つ以上のファイルを選択することができます。 ユーザーが選択するファイル パスは、FileDialogSelectedItems コレクションにキャプチャされます。

  • [フォルダー ピッカー ] ダイアログ ボックス: ユーザーはパスを選択することができます。 ユーザーが選択するパスは、FileDialogSelectedItems コレクションにキャプチャされます。

各ホスト アプリケーションが作成できる FileDialogオブジェクトのインスタンスは 1 つだけです。 そのため、複数の FileDialog オブジェクトを作成した場合でも、FileDialog オブジェクトのプロパティの多くは保持されます。 この理由から、ダイアログ ボックスを表示する前に、全てのプロパティで目的に合った設定が行われていることを確認してください。

FileDialog オブジェクトを使用して [ファイル] ダイアログ ボックスを表示するには、Show メソッドを使用する必要があります。 ダイアログ ボックスが表示されたら、ユーザーがダイアログ ボックスを閉じるまでコードの実行は致停止します。 次の例では、 ファイルの選択ダイアログ ボックスを作成して表示し、選択された各ファイルをメッセージ ボックスに表示します。

Sub Main() 
 
 'Declare a variable as a FileDialog object. 
 Dim fd As FileDialog 
 
 'Create a FileDialog object as a File Picker dialog box. 
 Set fd = Application.FileDialog(msoFileDialogFilePicker) 
 
 'Declare a variable to contain the path 
 'of each selected item. Even though the path is aString, 
 'the variable must be a Variant because For Each...Next 
 'routines only work with Variants and Objects. 
 Dim vrtSelectedItem As Variant 
 
 'Use a With...End With block to reference the FileDialog object. 
 With fd 
 
 'Use the Show method to display the File Picker dialog box and return the user's action. 
 'The user pressed the button. 
 If .Show = -1 Then 
 
 'Step through each string in the FileDialogSelectedItems collection. 
 For Each vrtSelectedItem In .SelectedItems 
 
 'vrtSelectedItem is aString that contains the path of each selected item. 
 'Use any file I/O functions that you want to work with this path. 
 'This example displays the path in a message box. 
 MsgBox "The path is: " & vrtSelectedItem 
 
 Next vrtSelectedItem 
 'The user pressed Cancel. 
 Else 
 End If 
 End With 
 
 'Set the object variable to Nothing. 
 Set fd = Nothing 
 
End Sub

関連項目

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。