以下のAPIを使用しますので、モジュールの定義を行います。
Declare Function SHGetPathFormIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lparam As Long
iImage As Long
End Type
Public S1 As Object
フォルダーのダイアログを表示する関数です。
Sub Folder_Show()
Dim Msg As String, rc As String
Set S1 = Workbooks("PG管理.xls").Sheets("メニュー")
rc = S1.Cells(5, 3).Value
If MsgBox(rc, vbYesNo) = vbYes Then
Exit Sub
End If
Msg = "フォルダを選択してください"
rc = GetDirectory(mag)
If rc = "" Then Exit Sub
S1.Cells(5, 3).Value = rc
End Sub
以下の記述はブラウズフォルダーを呼出す関数です。
Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO, path As String
Dim r As Long, x As Long, pos As Integer
bInfo.pidlRoot = 0&
If IsMissing(Msg) Then
bInfo.lpszTitle = "フォルダの選択"
Else
bInfo.lpszTitle = Msg
End If
bInfo.ulFlags = &H1
x = SHBrowseForFolder(bInfo)
path = Space$(512)
r = SHGetPathFormIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
|