Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:05:22+00:00 2026-06-15T08:05:22+00:00

I want to call System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) on some 3rd party COM objects that I’ve instantiated

  • 0

I want to call System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) on some 3rd party COM objects that I’ve instantiated in my VBA code, from my workbook close event, something like the following:

    Public Sub disconnect(obj As Variant)
        Dim refs As Long
        refs = 0

        If Not obj Is Nothing Then
            Do
                refs = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            Loop While (refs > 0)
        End If
    End Sub

However, I get a compile error: invalid qualifier with the System highlighted with the above code. Search doesn’t seem to return any VBA code that calls System.Runtime methods from a VBA macro – I can only find VB.Net automating Excel. I’m not sure it’s even possible.

I’m trying to resolve this issue: Excel 2007 Zombie Process not COM automation but w/ references to 3rd party com objects by ensuring these 3rd party COM objects are properly disposed of before Excel exits.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:05:24+00:00Added an answer on June 15, 2026 at 8:05 am

    I was unable to resolve the zombie problem using the recommended VBA only method, even after careful checking for and removal of circular references.

    Additional searching turned up a way to call the ReleaseCom method using code that wraps System.Runtime.InteropServices.Marshal.FinalReleaseComObject to create a COM visible dll that can be called from VBA

    Using the tutorial “how to create a com object using vs 2008 and consume it from vb6.0 client” and a newly installed copy of VS2010 Express, I was able to create a COM visible dll callable from VBA.

    Here’s the slightly modified wrapper and how to build the dll:

        using System;
        using System.Collections.Generic;
        using System.Runtime.InteropServices;
        using System.EnterpriseServices;
    
    
        namespace ComDisposerLib
        {
            [ClassInterface(ClassInterfaceType.None)]
            [ComponentAccessControl(false)]
            public class ComDisposer : System.EnterpriseServices.ServicedComponent, IDisposable, ComDisposerLib.IComDispose
            {
                private List<Object> _comObjs;
    
                public ComDisposer()
                {
                    _comObjs = new List<Object>();
                }
    
                ~ComDisposer()
                {
                    Dispose(false);
                }
    
                public Object Add(Object o)
                {
                    if (o != null && o.GetType().IsCOMObject)
                        _comObjs.Add(o);
                    return o;
                }
    
                public void Clear()
                {
                    Dispose(true);
                }
    
                protected override void Dispose(bool disposing)
                {
                    if (disposing)
                    {
                        for (int i = _comObjs.Count - 1; i >= 0; --i)
                            Marshal.FinalReleaseComObject(_comObjs[i]);
                        _comObjs.Clear();
                    }
                }
    
                void IDisposable.Dispose()
                {
                    Dispose(true);
                    GC.SuppressFinalize(this);
                }
            }
        }
    

    and the interface:

        using System;
    
        namespace ComDisposerLib
        {
            public interface IComDispose
            {
                Object Add(Object o);
                void Clear();
                void Dispose();
            }
        }
    

    To build, create a new class library project, add references to System.Runtime.InteropServices and System.EnterpriseServices, enable ‘Sign the assembly’ (in the menu under project / properties / signing ) and select or create a key file. Add the class and interface code files. In the AssemblyInfo.cs file (located under properties) add

    using System.Runtime.InteropServices;
    using System.EnterpriseServices;
    

    and

    [assembly: ComVisible(true)]
    [assembly: ApplicationName("ComDisposer")]
    [assembly: ApplicationActivation(ActivationOption.Library)]
    

    and build. If all goes well, you can register you dll as follows:

    regsvcs "C:\Documents and Settings\username\My Documents\Visual Studio 2010\Projects\ComDispose\ComDispose\obj\Release\ComDisposer.dll"
    

    In VBA, after adding a reference to your new COM library, use it as follows:

    Sub disposeGlobalComObjects()
    ' global scope objects used only to simplify example 
    
        Dim cd As ComDisposer
        Set cd = New ComDisposer
    
        If Not SomeGlobalComObject Is Nothing Then
            cd.Add SomeGlobalComObject
            Set SomeGlobalComObject = Nothing
        End If
        If Not AnotherGlobalComObject Is Nothing Then
            cd.Add AnotherGlobalComObject
            Set AnotherGlobalComObject = Nothing
        End If
        cd.Dispose
    End Sub
    

    Early testing indicates that it’s working, i.e. Excel closes cleanly and no longer creates zombie processes.

    Interestingly, I just ran across this method for using your dll from VBA without having to register it first which could be useful if you didn’t have access to the registry on your client machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to make a system call in my python code but the problem
All Here i want to run .sh file via system call in android NDK.
I am using System.Threading.ThreadPool.QueueUserWorkItem(x => MyMethod(param1, param2, param3, param4, param5)); I want to call
I want to call a web service that requires an authentication cookie. I have
I have the following legacy VB6 function that I want to call from C#.
I have a System.Windows.Forms.Form and want to change the Form.Icon at runtime to display
All, I have a number of C# DLLs that I want to call from
I want to call a webservice using google closures, via jsonp since i am
I want to call a html page on click of a button in mvc.
i want to call the command prompt command using Process.Start and then using StandardOutput

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.