○特定のフォルダを開ければ良い場合
(たとえばセルに記入した等の何かデータを拾ってそのフォルダを開きたい場合も同じ)
例:Cドライブのtestフォルダ
Sub ボタン1_Click()
Dim ret
ret = Shell("explorer.exe " & "c:\test", vbNormalFocus)
End Sub
○フォルダを選んで開きたい場合
Sub ボタン2_Click()
Dim Shell, myPath
Set Shell = CreateObject("Shell.Application")
Set myPath = Shell.BrowseForFolder(&O0, "フォルダを選択", &H1 + &H10)
If Not myPath Is Nothing Then Shell.Open myPath.Items.Item.Path
Set myPath = Nothing
Set Shell = Nothing
End Sub
1つの決ったフォルダを開くのか、フォルダ一覧を出させて、選択させ、最終的にファイルを選択させるのか。
聞く前に自分のしたいことをもう少し幅広く説明できなきゃ。
下記のようなのは直ぐWEBで見つかるよ。
Sub test01()
FolderSpec = "C:\Documents and Settings\XXXX\My Documents\"
Set FolderDlg = Application.FileDialog(msoFileDialogFolderPicker)
With FolderDlg
'最初に表示するフォルダパス
.InitialFileName = FolderSpec
.AllowMultiSelect = False
End With
Result = FolderDlg.Show()
If Result = -1 Then
NewFolderPath = FolderDlg.SelectedItems(1)
End If
Set FolderDlg = Nothing
End Sub
詳細は
http://www.geocities.jp/cbc_vbnet/tips/dialog.html
を参照してください。
Private Sub CommandButton1_Click()
'ファイルの保存(初期フォルダの指定)
With Application.FileDialog(msoFileDialogSaveAs)
'初期フォルダ設定
.InitialFileName = "C:\EXCEL\" & ThisWorkbook.Name
'ダイアログのタイトル
.Title = "ブックを保存します"
If .Show Then
.Execute
End If
End With
End Sub
Private Sub CommandButton2_Click()
'ファイルを開く(初期フォルダの指定)
With Application.FileDialog(msoFileDialogOpen)
'初期フォルダ設定
.InitialFileName = "C:\EXCEL\"
'ダイアログのタイトル
.Title = "選択したブックを開きます"
'選択できるファイルの拡張子の選択
.Filters.Clear
.Filters.Add "Excelブック", "*.xls;*.xlsx"
.Filters.Add "すべてのファイル", "*.*"
If .Show Then
.Execute
End If
End With
End Sub
お礼
御忙しい中、有難う御座います。vba初心者ですので助かりました。