We help IT Professionals succeed at work.
Get Started

Garbagecollect funciton error with excel

projectja asked
Last Modified: 2012-08-13

I am preparing a script to work with excel sheet. The thing is when I try to use Garbagecollect I get an error.

This is the error message:

Exception calling "ReleaseComObject" with "1" argument(s): "Object reference not set to an instance of an object."
At E:\Preparacion\scripts\CASINO-CGM-pruebas.ps1:522 char:64
+     ([System.Runtime.Interopservices.Marshal]::ReleaseComObject <<<< ([System.__ComObject]$ref) -gt 0)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

An extact of the script code:
you can see the code below. Line 522 is:
... ReleaseComObject([System.__ComObject]$ref) -gt 0)..


Function GarbageCollect ([object]$ref){
    ([System.Runtime.Interopservices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)

 $a = GarbageCollect($c)
 $a = GarbageCollect($Wb)
 $a = GarbageCollect($xl)


Please, I would thank your help. I am absolutelly lost with this issue. I have used that function before, however, I dont sure what is happening in this case
Watch Question
Full stack Software Engineer
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE