简体   繁体   中英

Using Powershell to run a macro on an Excel document when .open causes hang

This may be a combination of two questions, but I'm open to multiple solutions.

I open an Excel file with Powershell using the following code

$step=$args[0]
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $True
$excel.WindowState = 'xlMaximized'
$workBook = $excel.Workbooks.Open($step)

When this Excel file opens, it automatically runs several macros intended to download a second Excel file. I need to close the first Excel file, and it looks like there's a macro to do it that I could call, but Powershell never actually returns to the prompt ( PS C:\\> ) after calling Workbooks.Open() , I'm assuming because the UserForm that's generated by the macros is still open (It just contains a close button at this point, which triggers the exit macro).

So, is there either any way I can get Powershell to return to the prompt to run the exit macro, or is there another way I can close the first Excel file without closing the second one?

Because the second file is generated by macros in the first, it will always be in the same process as the first, so using Stop-Process will close both windows.

Try disabling alerts to suppress the popup:

$step=$args[0]
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $True
$excel.WindowState = 'xlMaximized'

$excel.DisplayAlerts = $false

$workBook = $excel.Workbooks.Open($step)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM