PowerShell から VBA を実行させる
以前、NuGet 無し PowerShell 経由で Excel 操作を断念したわけですが、それでは処理自体は VBA に任せて PowerShell は処理実行をキックする役目を持ってはどうか?と思案が推移しました。無駄技術を極めるシリーズの始まりです!
目次
動作イメージ
bat
test.bat
@echo off echo PowerShell から VBA を実行しています... powershell -NoProfile -ExecutionPolicy Unrestricted .\test.ps1
PowerShell
test.ps1
$file = "C:\Users\User\~\test.xlsm" $excel = New-Object -ComObject Excel.Application try { $book = $excel.Workbooks.Open($file) $excel.Run("Hello") $excel.Run("Say", "太郎", "山田") $book.Close() } finally { $excel.Quit() [System.Runtime.InteropServices.Marshal]::FinalReleaseComObject($excel) | Out-Null }
Excel
test.xlsm(画面は省略、標準モジュール)
Sub Hello() MsgBox "Hello, World!" End Sub Sub Say(ByVal firstName As String, ByVal lastName As String) MsgBox "名前は、" + lastName + " " + firstName + "さんです!" End Sub