違うブック
実行ファイル側(エンジン部分)
記述例
Sub サンプル()
'Call load_sub("E:\bbb.dll.mydocs", "Module1.hoge")
'MsgBox load_function("E:\bbb.dll.mydocs", "Module1.hello", "world")
'MsgBox load_function("E:\bbb.dll.mydocs", "hello", "world")
'MsgBox load_function("E:\bbb.dll.mydocs", "Module1.fuga", "aaa,bbb", "ccc")
End Sub
外部プロシージャの呼び出し
Sub load_sub(xlsmPath As String, Module As String, Procedure As String)
Workbooks.Open Filename:=xlsmPath, ReadOnly:=True
Application.Run Mid(xlsmPath, InStrRev(xlsmPath, "\") + 1) & "!" & Module & "." & Procedure
Windows(Mid(xlsmPath, InStrRev(xlsmPath, "\") + 1)).Close
End Sub
Function load_function(xlsmPath As String, Procedure As String, Optional ByVal arg As String = "", Optional ByVal arg2 As String = "", Optional ByVal arg3 As String = "", Optional ByVal arg4 As String = "", Optional ByVal arg5 As String = "")
Workbooks.Open Filename:=xlsmPath, ReadOnly:=True
If arg = "" Then
load_function = Application.Run(Mid(xlsmPath, InStrRev(xlsmPath, "\") + 1) & "!" & Procedure)
ElseIf arg2 = "" Then
load_function = Application.Run(Mid(xlsmPath, InStrRev(xlsmPath, "\") + 1) & "!" & Procedure, arg)
ElseIf arg3 = "" Then
load_function = Application.Run(Mid(xlsmPath, InStrRev(xlsmPath, "\") + 1) & "!" & Procedure, arg, arg2)
ElseIf arg4 = "" Then
load_function = Application.Run(Mid(xlsmPath, InStrRev(xlsmPath, "\") + 1) & "!" & Procedure, arg, arg2, arg3)
ElseIf arg5 = "" Then
load_function = Application.Run(Mid(xlsmPath, InStrRev(xlsmPath, "\") + 1) & "!" & Procedure, arg, arg2, arg3, arg4)
Else
load_function = Application.Run(Mid(xlsmPath, InStrRev(xlsmPath, "\") + 1) & "!" & Procedure, arg, arg2, arg3, arg4, arg5)
End If
Windows(Mid(xlsmPath, InStrRev(xlsmPath, "\") + 1)).Close
End Function
メモ
- 実行ファイル側にも「ファイル名を取得」と同様の処理をしているが、使用が限られるため直接記述しています
- 「Application.Run」は最大30個の引数を指定できるがとりあえず5つまで記述
外部ファイル側
Function ファイル名を取得(ファイルパス As String) As Variant
' サンプル ---------------------------------------------------------------------
' Call ファイル名を取得(thisworkbook.FullName)(0) ... ファイル名(拡張子を含む)
' Call ファイル名を取得(thisworkbook.FullName)(1) ... ファイル名(拡張子を除く)
' Call ファイル名を取得(thisworkbook.FullName)(2) ... 拡張子
' ------------------------------------------------------------------------------
' 区切記号
Dim ディレクトリ位置 As Long: ディレクトリ位置 = InStrRev(ファイルパス, "\")
' フルファイル名
Dim フルファイル名 As String: フルファイル名 = Mid(ファイルパス, ディレクトリ位置 + 1)
' 区切記号
Dim ピリオド位置 As Long: ピリオド位置 = InStrRev(フルファイル名, ".")
' ファイル名
Dim ファイル名 As String: ファイル名 = Left(フルファイル名, ピリオド位置 - 1)
' 拡張子
Dim 拡張子 As String: 拡張子 = Mid(フルファイル名, ピリオド位置 + 1)
'MsgBox フルファイル名 & " → " & ファイル名 & " + " & 拡張子
' 返値(配列)
Dim 返値(2) As String
返値(0) = フルファイル名 ' 例) hoge.txt
返値(1) = ファイル名 ' 例) hoge
返値(2) = 拡張子 ' 例) txt
ファイル名を取得 = 返値
End Function