0

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
4
  • 3
    powershellgallery.com/packages/ImportExcel/7.1.0 if you can install modules this will help you greatly Commented Apr 23, 2021 at 15:18
  • 3
    Agree with @SantiagoSquarzon, however the specific problem is you specified the wrong or mispelled variable to the .Open() method. $PathXML instead of ..XLSX So says the error, though I don't know why the code and the error don't align... Probably a typo somewhere. Commented Apr 23, 2021 at 15:23
  • I Did use the powershellgallery.com/packages/ImportExcel/7.1.0, when I started my script. As for the typo, I don't think that is the issue. The code worked fine a few days ago, but today, the same code does not. Commented Apr 23, 2021 at 16:45
  • Still waiting for more ideas to try... Commented Apr 23, 2021 at 20:03

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

9 Comments

When I ran the $Excel.GetType() I get this: $excel.GetType().fullname I then ran this:$excel.GetType().fullname and got this response: Microsoft.Office.Interop.Excel.ApplicationClass As for the typo, I do not see this in my script. It was successfully for a week, before it stopped.
Mike, can you send a script that will open the workbook then?
Clifton, you code now looks OK. Here's what I used: $Excel = New-Object -ComObject Excel.Application; $PathXLSX = "C:\Test\TEMP.xlsx"; $worksheetXLSX=$Excel.Workbooks.open($PathXLSX);$worksheetXLSX.Sheets.count;
Could the Excel file currently be opened and locked by another user or process?
No, it is on my computer. Would you mind sending me the script that would work?
|
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.