question

EliValero-6042 avatar image
0 Votes"
EliValero-6042 asked CliftonDunaway-2783 answered

Why powershell does not open excel?

$Excel = New-object -ComObject Excel.application
$Path = "Path"
$Excel.Workbooks.Open($Path)



It works most of the time, but sometimes, like today, is not working.
The error is:

You cannot call a method on a null-valued expression
At line: "x" Char: "s"
$Excel.Workbooks.Open($Path)


Any suggestions?

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

CliftonDunaway-2783 avatar image
0 Votes"
CliftonDunaway-2783 answered

I have the same problem. When I tried the Try-Catch code, here is my error:

your failure message here
At line:9 char:6
+ Throw "your failure message here" # or 'Throw $_'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (your failure message here:String) [], RuntimeException
+ FullyQualifiedErrorId : your failure message here

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

EliValero-6042 avatar image
0 Votes"
EliValero-6042 answered RichMatheisen-8856 commented

Hello Rich,

This is what I have now. That is giving me the mentioned error. I apologize for not being detailed enough.


$Excel = New-Object -ComObject Excel.application
$FilePath = "Path"
$Workbook = $Excel.Workbooks.Open($FilePath)
$Excel.visible = $True
$app = $excel.Application
$app.Run("Macro")

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That doesn't provide much more information than your original post. Wrap all six of those lines in the "Try" block as I suggested, adding "-ErrorAction STOP" to the New-Object, of course.

If the New-Object fails the $Excel variable will be $null and you'll get the "null-valued expression" error. You're trying to discover why the New-Object failed.

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered SQLReports-4344 published

Try wrapping that in a Try/Catch:

 Try{
     $Excel = New-object -ComObject Excel.application -ErrorAction STOP
     $Path = "Path"
     $Excel.Workbooks.Open($Path)
 }
 Catch{
     $_      # just display the exception (or send it into the pipeline)
     Throw "your failure message here"    # or 'Throw $_'
 }

Without knowing what the problem is I don't think anyone can offer advice that's meaningful.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello Rich,

This is what I have now. That is giving me the mentioned error. I apologize for not being detailed enough.


$Excel = New-Object -ComObject Excel.application
$FilePath = "Path"
$Workbook = $Excel.Workbooks.Open($FilePath)
$Excel.visible = $True
$app = $excel.Application
$app.Run("Macro")

0 Votes 0 ·