Link to home
Start Free TrialLog in
Avatar of projectja
projectja

asked on

Garbagecollect funciton error with excel

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
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
why grade C?
did i solve your problem or not?