Reputation: 703
I have a running script that creates an excel object via
$Excel = New-Object -ComObject Excel.Application
$ExcelApp = $Excel.Application
Although there are some issues with FULLY disabling an XLL I load so I wanted to try a different approach that I know others use manually
If I use below, I can create new instance of excel, and get the process info (pid, main window, etc). This should help with loading my XLL and disabling on quit
$test = Start-process excel.exe /x -PassThru
However I’m not sure how to get the -comObject from this
I have seen examples like
$Excel = [RunTime.Interopservices.Marshal]::GetActiveObject(‘Excel.Application’)
But I think this comes with the assumption that there can only be one excel whereas I want to ensure I get the excel ComObject only for a specific excel (for which I have the process info for)
Is there a way to tie the two together?
I’ve seen another way like
$Excel = [Microsoft.VisualBasic.Interaction]::GetObject($null,’Excel.Application’)
But I’m not sure how to use this as replacing $null with my $test.Id won’t work
Can anyone help? How do I get the $Excel object from the $test process?
Upvotes: 2
Views: 251
Reputation: 440162
I suggest taking the opposite route:
Every call to New-Object -ComObject Excel.Application
creates a new EXCEL.exe
process.
Assuming that no other process on your machine is making such calls concurrently, you can use the following to determine the EXCEL
process associated with the most recent New-Object -ComObject Excel.Application
call:
$Excel = New-Object -ComObject Excel.Application
$ExcelProcess = (Get-Process Excel | Sort-Object -Descending StartTime)[0]
Note:
Upvotes: 0