WinMergeをエクセルマクロで起動する方法
エクセルを使用してWinMergeを起動する方法を記載する。
エクセルフォーマット
マクロを組み込むエクセルのフォーマット。
※A,B,D列はマクロ上使用しません。
エクセル関数
エクセルに設定する関数
セルK2:シート名
セルK3:比較対象1(WinMerge起動後の一番左)
セルK4:比較対象2(WinMerge起動後の比較対象1の右側)
セルK5:比較対象3(WinMerge起動後の比較対象2の右側)
マクロボタンの関数
マクロボタンに設定する関数名
※最後に実施で問題なし
セルF2当たりのボタン:compareFile
セルF6当たりのボタン:compareDirectory
マクロ
マクロ内容
以下を標準モジュールとして登録
'差分比較(ファイル単位)ボタン押下時メソッド
Sub compareFile()
Dim aCell As String 'アクティブセル内容
Dim project As String 'プロジェクト名
Dim leftPath As String '左辺のファイルパス
Dim rightPath As String '右辺のファイルパス
Dim leftFile As String '左辺の比較ファイル
Dim rightFile As String '右辺の比較ファイル
Dim result As String '比較結果
Dim optPath As String 'オプション(3ファイル目)のファイルパス
Dim optFile As String 'オプション(3ファイル目)の比較ファイル
'アクティブセルの内容を取得
aCell = getActiveCell()
'プロジェクト名を取得
project = Cells(2, 11).Value
'左辺のファイルパスを取得
leftPath = Cells(3, 11).Value
'右辺のファイルパスを取得
rightPath = Cells(4, 11).Value
'オプションのファイルパスを取得
optPath = Cells(5, 11).Value
'左辺及び右辺のフルパス(ファイル名込み)を取得
leftFile = getFullPathFile(leftPath, project, aCell)
rightFile = getFullPathFile(rightPath, project, aCell)
'オプションのフルパス(ファイル名込み)を取得
If optPath <> "" Then
optFile = getFullPathFile(optPath, project, aCell)
End If
'WinMerge(ファイル単位)起動
result = exeWinMerge(leftFile, rightFile, optFile)
End Sub
'差分比較(ディレクトリ単位)ボタン押下時メソッド
Sub compareDirectory()
Dim aCell As String 'アクティブセル内容
Dim aCellPath As String 'アクティブセルから抽出したファイルパス(ファイル名除く)
Dim project As String 'プロジェクト名
Dim leftPath As String '左辺のファイルパス
Dim rightPath As String '右辺のファイルパス
Dim leftFile As String '左辺の比較ファイル
Dim rightFile As String '右辺の比較ファイル
Dim result As String '比較結果
Dim optPath As String 'オプション(3ファイル目)のファイルパス
Dim optFile As String 'オプション(3ファイル目)の比較ファイル
'アクティブセルの内容を取得
aCell = getActiveCell()
'アクティブセルから抽出したファイルパス(ファイル名除く)を取得
aCellPath = extractFilePath(aCell)
'プロジェクト名を取得
project = Cells(2, 11).Value
'左辺のファイルパスを取得
leftPath = Cells(3, 11).Value
'右辺のファイルパスを取得
rightPath = Cells(4, 11).Value
'オプションのファイルパスを取得
optPath = Cells(5, 11).Value
'左辺及び右辺のフルパス(ファイル名込み)を取得
leftFile = getFullPathFile(leftPath, project, aCellPath)
rightFile = getFullPathFile(rightPath, project, aCellPath)
'オプションのフルパス(ファイル名込み)を取得
If optPath <> "" Then
optFile = getFullPathFile(optPath, project, aCellPath)
End If
'WinMerge(ファイル単位)起動
result = exeWinMerge(leftFile, rightFile, optFile)
End Sub
'アクティブセルの内容を取得
Function getActiveCell() As String
Dim result As String 'セル内容
Dim col As Integer 'セルの列番号用変数
Dim row As Integer 'セルの行番号用変数
'セル番号を取得する
col = ActiveCell.Column
row = ActiveCell.row
'列番号確認
'C列以外を選択している場合はNGのため、処理を終了する。
If col <> 3 Then
MsgBox ("C列を選択してください。")
'全処理終了
End
End If
'セル内容の取得
result = Cells(row, col).Value
getActiveCell = result
End Function
'フルパス(ファイル名込み)取得
'param path 左辺または右辺のファイルパス
'param project プロジェクト名
'param aCell アクティブセルのファイル名
Function getFullPathFile(path As String, project As String, aCell As String) As String
Dim result As String '返却値
'文字結合
result = path & "/" & project & aCell
getFullPathFile = result
End Function
'Winmerge実行メソッド
'param leftFile 左辺のファイルパス
'param rightFile 右辺のファイルパス
'param optFile オプションのファイルパス
Function exeWinMerge(leftFile As String, rightFile As String, optFile As String)
Dim exeStr As String '実行コマンド
Dim rc As Long '実行結果
Dim winMergeExe As String 'WinMerge実行オプション -ignorews:空白を無視 -ignoreeol:改行コードを無視
winMergeExe = "C:/Program Files/WinMerge/WinMergeU.exe -r"
'winMergeExe = "C:/Program Files/WinMerge/WinMergeU.exe -ignorews -ignoreeol -xq"
'WinMerge実行
exeStr = winMergeExe & " " & leftFile & " " & rightFile & " " & optFile
rc = Shell(exeStr, vbNormalFocus)
'結果確認
If rc = 0 Then
result = "起動に失敗しました。"
End
End If
End Function
'ファイルパス抽出処理
'param file 抽出対象ファイル
Function extractFilePath(file As String) As String
Dim result As String '抽出結果
'抽出実行
result = Left(file, WorksheetFunction.Find("★", WorksheetFunction.Substitute(file, "/", "★", Len(file) - Len(WorksheetFunction.Substitute(file, "/", ""))), 1) - 1)
extractFilePath = result
End Function