カレントディレクトリを取得する方法と、設定する方法になります。
【関数化】カレントディレクトリを取得します。
' カレントディレクトリを取得
Function GetCurrent() As String
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' カレントディレクトリを取得する
GetCurrentDirectory = fso.GetAbsolutePathName("./")
Set fso = Nothing
End Function
【関数化】カレントディレクトリを設定します。
ネットワークドライブを使用する場合は使用できません。
Sub SetCurrent(dir As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
ChDrive fso.GetDriveName(dir)
ChDir dir
End Sub
【関数化】カレントディレクトリを設定します。
ネットワークドライブも使用する場合はこちらを使用してください。
32bit版と、64bit版でAPI宣言の方法が違ってきます。
' 64bit 版のAPI宣言
Private Declare PtrSafe Function SetCurrentDirectory Lib _
"kernel32" Alias "SetCurrentDirectoryA" _
(ByVal lpPathName As String) As Long
' カレントディレクトリを設定するサンプル
Sub GetCurrent_Test()
SetCurrentDirectory "\\TestNet\ExcelVBA\"
' 確認
MsgBox GetCurrent()
End Sub
' 32bit 版のAPI宣言
Private Declare Function SetCurrentDirectory Lib _
"kernel32" Alias "SetCurrentDirectoryA" _
(ByVal lpPathName As String) As Long
' カレントディレクトリを設定するサンプル
Sub GetCurrent_Test()
SetCurrentDirectory "\\TestNet\ExcelVBA\"
' 確認
MsgBox GetCurrent()
End Sub
32bit版と64bit版の確認は、Excelのメニューから、「ファイル」を選択し、アカウント、Excelのバージョン情報で確認できます。




コメント