troubleshooting Question

Garbagecollect funciton error with excel

Avatar of projectja
projectja asked on
Powershell
2 Comments1 Solution523 ViewsLast Modified:
Hi,

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)
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
}

 $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
ASKER CERTIFIED SOLUTION
Meir Rivkin
Full stack Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros