SOLVED

PowerShell Direct - Unable to open Excel workbook with Excel.Application ComObject

%3CLINGO-SUB%20id%3D%22lingo-sub-1478891%22%20slang%3D%22en-US%22%3EPowerShell%20Direct%20-%20Unable%20to%20open%20Excel%20workbook%20with%20Excel.Application%20ComObject%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1478891%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHoping%20someone%20may%20be%20able%20to%20enlighten%20me%20or%20provide%20an%20alternative.%3C%2FP%3E%3CP%3ERunning%20in%20a%20local%20PS%20session%20on%20a%20Win10%20client%20with%20Office%202019%20installed%2C%20I%20am%20able%20to%20run%20these%20commands%2C%20open%2C%20and%20use%20content%20from%20an%20xlxs%20file%3A%3C%2FP%3E%3CP%3E%24file%20%3D%20%22C%3A%5CTestFiles%5CTest.xlsx%22%3CBR%20%2F%3E%24excel%20%3D%20New-Object%20-ComObject%20Excel.Application%3CBR%20%2F%3E%24workbook%20%3D%20%24excel.Workbooks.Open(%24file)%3C%2FP%3E%3CP%3E%26lt%3B...more%20code%20here%20but%20not%20relevant%20to%20the%20conversation....%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20try%20to%20use%20the%20same%20command%20using%20PowerShell%20Direct%20on%20the%20host%20where%20the%20VM%20is%20running%2C%20I%20get%20this%3A%3C%2FP%3E%3CP%3E%3CENTER-PSSESSION%20-vmname%3D%22%22%20win10.....command%3D%22%22%20already%3D%22%22%20run%3D%22%22%20and%3D%22%22%20connected.%3D%22%22%20user%3D%22%22%20account%3D%22%22%20connecting%3D%22%22%20is%3D%22%22%20a%3D%22%22%20local%3D%22%22%20administrator%3D%22%22%20in%3D%22%22%20the%3D%22%22%20workgroup%3D%22%22%20joined%3D%22%22%20vm...%3D%22%22%3E%3C%2FENTER-PSSESSION%3E%3C%2FP%3E%3CP%3E%5BWIN10%5D%3A%20PS%20C%3A%5CUsers%5CTestUser%5CDocuments%26gt%3B%20%24file%20%3D%20%22C%3A%5CTestFiles%5CTest.xlsx%22%3CBR%20%2F%3E%5BWIN10%5D%3A%20PS%20C%3A%5CUsers%5CTestUser%5CDocuments%26gt%3B%20%24excel%20%3D%20New-Object%20-ComObject%20Excel.Application%3CBR%20%2F%3E%5BWIN10%5D%3A%20PS%20C%3A%5CUsers%5CTestUser%5CDocuments%26gt%3B%20%24workbook%20%3D%20%24excel.Workbooks.Open(%24file)%3CBR%20%2F%3EMicrosoft%20Excel%20cannot%20access%20the%20file%20'C%3A%5CTestFiles%5CTest.xlsx'.%20There%20are%20several%20possible%20reasons%3A%3CBR%20%2F%3E%E2%80%A2%20The%20file%20name%20or%20path%20does%20not%20exist.%3CBR%20%2F%3E%E2%80%A2%20The%20file%20is%20being%20used%20by%20another%20program.%3CBR%20%2F%3E%E2%80%A2%20The%20workbook%20you%20are%20trying%20to%20save%20has%20the%20same%20name%20as%20a%20currently%20open%20workbook.%3CBR%20%2F%3EAt%20line%3A1%20char%3A1%3CBR%20%2F%3E%2B%20%24workbook%20%3D%20%24excel.Workbooks.Open(%24file)%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20OperationStopped%3A%20(%3A)%20%5B%5D%2C%20COMException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20System.Runtime.InteropServices.COMException%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%2C%20the%20file%20is%20there.%20No%20it%20isn't%20opened%20by%20another%20program.%20Not%20trying%20to%20save.%20Again%2C%20the%20commands%20work%20over%20and%20over%20when%20run%20in%20a%20local%20PS%20session.%3C%2FP%3E%3CP%3EI'm%20leaning%20toward%20using%20the%20ImportExcel%20PS%20module%20but%20I'm%20not%20sure%20it%20will%20allow%20me%20to%20find%20formulas%20in%20a%20spreadsheet%20(I've%20not%20seen%20where%20that%20can%20be%20done).%3C%2FP%3E%3CP%3ERegardless%2C%20I've%20repro'd%20this%20on%20three%20different%20Hyper-V%20servers%20and%20it%20seems%20like%20there%20is%20a%20problem%20with%20ComObject.%20I'm%20not%20versed%20well%20enough%20to%20know%20much%20more%20than%20that.%20I've%20read%20about%20DCOM%20Config%20and%20allowing%20permissions%20to%20Excel%20but%20there%20are%20no%20entries%20for%20either%20Excel%20or%20a%20CLSID%20for%20Excel.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20simple%20for%20anyone%20to%20repro.%20Just%20have%20a%20Win10%20VM%20with%20Office%20and%20create%20an%20Excel%20file.%20You%20don't%20need%20to%20do%20anything%20with%20the%20file.%20This%20all%20falls%20apart%20in%20PS%20Direct%20when%20the%20workbook%20is%20being%20opened.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20advance%20for%20any%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETom%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1478891%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%202016%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%202019%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHyper-V%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epowershell%20direct%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1482141%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Direct%20-%20Unable%20to%20open%20Excel%20workbook%20with%20Excel.Application%20ComObject%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1482141%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F134833%22%20target%3D%22_blank%22%3E%40Tom%20Phillips%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hate%20to%20be%20the%20one%20to%20answer%20my%20own%20question%2C%20but%20that%20is%20what's%20happening.%3C%2FP%3E%3CP%3EAfter%20what%20I%20thought%20was%20a%20thorough%20three%20day%20search%2C%20I%20happened%20across%20a%20similar%20solution%20used%20for%20another%20like%20situation.%3C%2FP%3E%3CP%3EI%20found%20that%20going%20into%20DCOM%20Config%20and%2C%26nbsp%3Bin%20the%20properties%20of%20the%20Microsoft%20Excel%20%26gt%3B%20Identity%20tab%2C%20change%20the%20setting%20to%20%22The%20interactive%20user%22.%3C%2FP%3E%3CP%3ESince%20this%20is%20not%20a%20production%20machine%20and%20is%20being%20used%20for%20testing%2C%20I%20have%20not%20looked%20any%20deeper%20than%20solving%20my%20immediate%20problem.%20To%20those%20who%20see%20a%20security%20issue%2C%20again%2C%20this%20was%20not%20a%20consideration%20for%20me%20in%20this%20case.%20If%20it%20was%20production%2C%20I%20would%20spend%20more%20time%20considering%20the%20collateral%20of%20this%20change.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hello All,

 

Hoping someone may be able to enlighten me or provide an alternative.

Running in a local PS session on a Win10 client with Office 2019 installed, I am able to run these commands, open, and use content from an xlxs file:

$file = "C:\TestFiles\Test.xlsx"
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open($file)

<...more code here but not relevant to the conversation....>

 

When I try to use the same command using PowerShell Direct on the host where the VM is running, I get this:

<Enter-PSSession -VMName WIN10.....command already run and connected. User account connecting is a local administrator in the Workgroup joined VM...>

[WIN10]: PS C:\Users\TestUser\Documents> $file = "C:\TestFiles\Test.xlsx"
[WIN10]: PS C:\Users\TestUser\Documents> $excel = New-Object -ComObject Excel.Application
[WIN10]: PS C:\Users\TestUser\Documents> $workbook = $excel.Workbooks.Open($file)
Microsoft Excel cannot access the file 'C:\TestFiles\Test.xlsx'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
At line:1 char:1
+ $workbook = $excel.Workbooks.Open($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

 

Yes, the file is there. No it isn't opened by another program. Not trying to save. Again, the commands work over and over when run in a local PS session.

I'm leaning toward using the ImportExcel PS module but I'm not sure it will allow me to find formulas in a spreadsheet (I've not seen where that can be done).

Regardless, I've repro'd this on three different Hyper-V servers and it seems like there is a problem with ComObject. I'm not versed well enough to know much more than that. I've read about DCOM Config and allowing permissions to Excel but there are no entries for either Excel or a CLSID for Excel.

 

This is simple for anyone to repro. Just have a Win10 VM with Office and create an Excel file. You don't need to do anything with the file. This all falls apart in PS Direct when the workbook is being opened.

 

Thank you in advance for any help.

 

Tom

 

 

1 Reply
Best Response confirmed by Tom Phillips (New Contributor)
Solution

@Tom Phillips 

I hate to be the one to answer my own question, but that is what's happening.

After what I thought was a thorough three day search, I happened across a similar solution used for another like situation.

I found that going into DCOM Config and, in the properties of the Microsoft Excel > Identity tab, change the setting to "The interactive user".

Since this is not a production machine and is being used for testing, I have not looked any deeper than solving my immediate problem. To those who see a security issue, again, this was not a consideration for me in this case. If it was production, I would spend more time considering the collateral of this change.