|
| Menuへ |
| ファイル選択ダイアログ |
フォルダ選択ダイアログと同様に、よく使うTipsです。ここでは、ファイルを1個選択し返す方法と、複数ファイルが選択された場合の取得方法を掲載しています。複数選択された場合は、配列で戻りますのでまず戻り値をIsArrayで配列かどうかチェックしています。配列であれば、ループしUBoundで1個づつ取り出しています。選択された順番の通り取得することはできません。このプロシージャも他のWindowsソフトで一般的に使用するルーチンですので、特に考えることもなく使い回せばいいかと思います。
|
|
■1個のみ選択 |
| コマンドボタン クリックイベント |
|
Private Sub CommandButton1_Click()
CommandButton1.Caption = SelectFile_single
End Sub |
|
|
| 標準モジュールコード |
|
Public Function SelectFile_single()
Dim sfile As String
Dim i As Integer
Dim s As String
sfile = Application.GetOpenFilename("ファイルを選択してください (*.xls), *.xls")
If sfile = "False" Then
SelectFile_single = ""
Else
SelectFile_single = sfile
End If
End Function
|
|
|
|
 |
|
|
| 実行結果 |
|
|
■複数選択 |
| コマンドボタン クリックイベント |
|
Private Sub CommandButton1_Click()
CommandButton1.Caption = SelectFile_multi
End Sub |
|
|
| 標準モジュールコード |
|
Public Function SelectFile_multi()
Dim sfile As Variant
Dim i As Integer
Dim s As String
sfile = Application.GetOpenFilename("ファイルを選択してください (*.xls), *.xls", , , , True)
s = ""
If IsArray(sfile) Then
For i = 1 To UBound(sfile)
If s <> "" Then s = s & vbCrLf
s = s & sfile(i)
Next i
End If
SelectFile_multi = s
End Function
|
|
|
|
 |
|
|
| 実行結果 |
|
|
| Topへ Homeへ |
|