0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBAでダイアログからファイル・フォルダを選択してフルパスを取得する方法

Last updated at Posted at 2020-05-10

はじめに

VBAでツールを作るとよくダイアログからファイルやフォルダを選択する処理が出てきます。
ネットで調べるといろいろな方法が出てきますが、私が使っている方法をご紹介します。

ファイル選択

メソッド

'ダイアログからファイルを1つ選択する。
'選択したファイルのフルパスを戻り値とする。
'
'@param(Optional) dialogTitle ダイアログのタイトル
'@param(Optional) fileFilter ダイアログに表示するファイルの拡張子
'@param(Optional) defaultPath ダイアログのデフォルトパス
'@return openFileFullPaht 選択したファイルのフルパス。キャンセルを選択した時は「False」(String型)。
Function GetSelectFileFullPath(Optional dialogTitle As String = "ファイルを選択してください。", Optional fileFilter As String = "*", Optional defaultPath As String = "") As String

    '現在のカレントディレクトリのバックアップを取得する
    Dim currentFolderPath As String
    currentFolderPath = CurDir
    
    'カレントディレクトリの変更
    '省略又はフォルダが存在しない場合は、マクロのフォルダをデフォルトにする
    If defaultPath = "" Or Not IsExistFolder(defaultPath) Then
        CreateObject("WScript.Shell").CurrentDirectory = ThisWorkbook.path & "\"
    Else
        CreateObject("WScript.Shell").CurrentDirectory = defaultPath
    End If
    
    'フィルタに使用する文字列を作成
    Dim argFileFilter As String
    argFileFilter = ",*." + fileFilter
    
    'ダイアログの表示
    GetSelectFileFullPath = Application.GetOpenFilename(title:=dialogTitle, fileFilter:=argFileFilter)
    
    'カレントディレクトリの戻し
    CreateObject("WScript.Shell").CurrentDirectory = currentFolderPath

End Function

ダイアログの表示

'ダイアログの表示
GetSelectFileFullPath = Application.GetOpenFilename(title:=dialogTitle, fileFilter:=argFileFilter)

「Application.GetOpenFilename」を使用してダイアログ表示して、選択したファイルのフルパスを取得します。
引数でダイアログに表示するタイトル、ダイアログで表示する拡張子を指定できます。

カレントディレクトリ

ダイアログ表示時の最初のフォルダを変更しています。
選択したいファイルは、マクロを格納しているフォルダの近くにあることが多いのでデフォルトはマクロの格納場所にしています。引数を指定することで変更することができます。

「CreateObject("WScript.Shell").CurrentDirectory」で指定している理由は、現在のカレントディレクトリと異なるドライブやネットワークドライブでも動作するようにするためです。

また、ツールの実行前後でカレントディレクトリが変わらないようにバックアップを取得して、メソッドの最後で元に戻しています。

フォルダ選択

メソッド

'ダイアログからフォルダを選択する。
'選択したフォルダのフルパスを戻り値とする。
'
'@param(Optional) dialogTitle ダイアログのタイトル
'@return 選択したフォルダのフルパス。
Function GetSelectFolderFullPath(Optional dialogTitle As String = "フォルダを選択してください。") As String

    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = ThisWorkbook.path & "\"
        .title = dialogTitle
        If .Show = True Then
            GetSelectFolderFullPath = .SelectedItems(1)
        Else
            GetSelectFolderFullPath = "False"
        End If
    End With
    
End Function

ファイル選択時とほとんど同じです。
「Application.FileDialog(msoFileDialogFolderPicker)」を使用してダイアログを表示してフォルダを選択します。

デフォルトのフォルダは、「InitialFileName 」プロパティで指定しています。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?