Clifton Dunaway
Clifton Dunaway

Reputation: 1

Using PowerShell to access Excel

I have this script working in my code, but, today, it stopped working. Here is what I have:

$Excel=new-object -comobject excel.application;
$PathXLSX = "C:\Temp\TEMP.xlsx"
$worksheetXLSX=$Excel.Workbooks.open($PathXLSX);

When I run the third line, I get this error:

You cannot call a method on a null-valued expression. At line:1 char:1

+ $worksheetXML=$Excel.workBooks.open($PathXML);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Upvotes: 0

Views: 372

Answers (2)

Clifton Dunaway
Clifton Dunaway

Reputation: 1

I finally got my script working today!

What I did was stop using the Powershell 5.1, that comes with Windows 10, and installed the new version, 7.1. When I call the script from the new version, it runs fine.

Thanks everyone for your feedback!

Upvotes: 0

Mike Smith - MCT
Mike Smith - MCT

Reputation: 1231

That error would be generated if the $Excel object was null. Does it exist? Try this to see if returns a type:

$Excel.GetType()

If that is not null then try:

 $excel.GetType().fullname

That should return: Microsoft.Office.Interop.Excel.ApplicationClass

As your codes does not match the error message I'm guessing you have some typos.

$worksheetXLSX=$Excel.Workbooks.open($PathXLSX);
$worksheetXML=$Excel.workBooks.open($PathXML);

Upvotes: 1

Related Questions