続行する前にプロセスが終了するまでVBScriptを待機させることについて以前に投稿しました(詳細:VBScript-プロセスが終了するまでプログラムを待機させる方法は? ) 。
いくつかの議論の後、私は適切な答えを与えられました。ただし、解決策が別の問題を提示したため、コードを使用して新しい方向に進んでいるようです。あなたが私を助けてくれることを願っています。
基本的に、私は以下に提供するいくつかのコードを持っています。4つの引数を取ります。そのうちの1つは、VBAマクロで他の3つと一緒に使用したい多くのファイルを含むフォルダーへのPATHです。
If WScript.Arguments.Count = 4 Then
' process input argument
Set args = WScript.Arguments
arg1 = args.Item(0)
arg2 = args.Item(1)
arg3 = args.Item(2)
arg4 = args.Item(3)
' Create a WshShell instance
Dim WShell
Set WShell = CreateObject("WScript.Shell")
' Create an Excel instance
Dim x1
Set x1 = CreateObject("Excel.Application")
' Disable Excel UI elements
x1.DisplayAlerts = False
x1.AskToUpdateLinks = False
'x1.AlertBeforeOverwriting = False
x1.FeatureInstall = msoFeatureInstallNone
' Open the Workbooks specified on the command-line
Dim x1WB
Dim x2WB
Dim x3WB
Dim x4WB
Dim strWB1
Dim strWB2
Dim strWB3
Dim strWB4
Dim FSO
Dim FLD
Dim FIL
Dim strFolder
strWB1 = arg1
Set x1WB = x1.Workbooks.Open(strWB1)
' Show the workbook/Excel program interface. Comment out for silent running.
x1WB.Application.Visible = True
strWB2 = arg2
Set x2WB = x1.Workbooks.Open(strWB2)
' Show the workbook/Excel program interface. Comment out for silent running.
x2WB.Application.Visible = True
strWB3 = arg3
Set x3WB = x1.Workbooks.Open(strWB3)
' Show the workbook/Excel program interface. Comment out for silent running.
x3WB.Application.Visible = True
'To hold the string of the PATH to the multiple files
strFolder = arg4
Set FSO = CreateObject("Scripting.FileSystemObject")
'Get a reference to the folder I want to search
set FLD = FSO.GetFolder(strFolder)
Dim strMyMacro
strMyMacro = "my_excel_sheet_with_vba_module.xlsm!Sheet1.my_vba_macro"
'loop through the folder and get the file names
For Each Fil In FLD.Files
WshShell.run """C:\Program Files\Microsoft Office\Office14\EXCEL.exe"" " & Fil, 1, true
x1.Run strMyMacro
'~~> Problem - How do I get the macro to run before opening the above file but run after it has opened (due to setting the bWaitOnReturn to true)
'~~> Problem - How do I get the file on current iteration to close after the macro has completed?
'~~> Problem - If this is not the issue, can you identify it?
Next
x1WB.close
x2WB.close
x3WB.close
'x4WB.close
' Clean up and shut down
Set x1WB = Nothing
Set x2WB = Nothing
Set x3WB = Nothing
Set x4WB = Nothing
Set FSO = Nothing
Set FLD = Nothing
x1.Quit
Set x1 = Nothing
Set WshShell = Nothing
WScript.Quit 0
Else
WScript.Quit 1
End If
スクリプトは次のように機能します。
- 4つの引数がスクリプトに渡されます。3番目の引数は、VBAマクロを含む.xlsmファイルです。最後の引数は、複数のファイルを含むフォルダーへのPATHです。
- 次に、最初の3つのExcelファイルを開きます。
- 次に、ループを実行し
Fil
て、4番目の引数として指定されたフォルダー内のファイルを反復処理します。AFAIKこれはWScript.shell
、メソッドを使用して.run
実行する必要があります。これにより、スクリプトの残りの部分は、処理中のExcelファイルが終了するまでハングし、その後、フォルダーを閉じて次のファイルを開きます。 - ファイルを開いた後
Fil
、マクロを実行します(現時点では失敗しましたが)。
オブジェクトを使用してすべてのExcelファイルを単純に開くように誘惑されましたがWScript.shell
、この方法でマクロを実行することはできませんでした。
うまくいけば、このVBScriptの目的を定義できたと思いますが、私に知らせていない場合は、明確にしておきます。手伝ってくれますか?
ありがとう、QF。