ASD
ASD

Reputation: 65

Excel open workbook not found

I have two Excel files with Macros in them.

One is already open, the second one is opened out of powershell and a Macro is run.

Now in the Macro I want to switch back to the first file:

Windows("file1.xlsm").Activate
Sheets("sheet1").Activate

Doesn't work -> runtime error (file / window not found).

If I open the second file by hand, everything works.

So it looks like if the second file is opened through powershell it doesn't "see" the already open workbooks.

Any idea how to fix this anybody?

Upvotes: 1

Views: 489

Answers (1)

ASD
ASD

Reputation: 65

This is how i solved this:

Instead of starting a new session for Excel:

$Excel = New-Object -ComObject Excel.Application

I just open the new file in the same session:

$excel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')

Upvotes: 1

Related Questions