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 461123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:56:35+00:00 2026-05-12T22:56:35+00:00

I have an Excel add-in with a class module. I want to instantiate the

  • 0

I have an Excel add-in with a class module. I want to instantiate the class module in C# and call a method on it. How do I do that?

  • 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-05-12T22:56:35+00:00Added an answer on May 12, 2026 at 10:56 pm

    If you really need access to an instance of the class, you could do the following:

    • Generate a type library for a COM interface that you want to expose from your VBA class (e.g. IMyComInterface)

    • Add a reference to this type library in your VBA project

    • Implement the interface in your VBA class module – e.g. MyVbaClass (use the Implements keyword):

      Option Explicit
      Implements IMyComInterface
      
      Private Sub IMyComInterface_SomeMethod(...)
          ...
      End Sub
      ...
      
    • Reference the same type library in your C# project

    • Create a ComVisible C# class with a method that accepts a reference to the VBA interface instance. Something like:

      public class MyVbaLoader
      {
          public IMyComInterface MyComInterface
          {
              get { return myComInterface; }
              set { myComInterface = value; }
          }
      }
      
    • Write a “factory” method in a VBA standard module, that takes an object as a ByRef parameter. This object should assume the object passed as an argument has a property “MyComInterface” and should set this property to a new instance of the VBA class MyClass.

      Public Sub MyFactoryMethod(MyVbaLoader As Object) 
          Dim objClass As MyVbaClass
          Set objClass = New MyVbaClass
          ... any initialization of objClass here ...
      
          ' Pass a reference to the VBA class to the C# object MyVbaLoader
          MyVbaLoader.MyComInterface = objClass
      End Sub
      
    • Call the factory method from your C# code. Assuming you have opened the workbook and have a refence “workbook” in your VBA code, the code will look something like:

      MyVbaLoader loader = new MyVbaLoader();
      workbook.Application.Run("MyModule.MyFactoryMethod", loader, Type.Missing, ... , Type.Missing);
      // we now have a reference to the VBA class module in loader.MyComInterface
      // ...
      

    As you can see, it’s rather complex. Without more detail of the problem you’re trying to solve it’s difficult to say whether this complexity is justified, or whether there’s a simpler solution.

    If the above isn’t clear, let me know and I’ll try to clarify.

    Basically you can’t return a value from a VBA macro called from your C# code using Application.Run, so you have to resort to passing an object by value that has a method or property that can be called from VBA to set the instance.

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

Sidebar

Related Questions

I have Excel add-in which I add so many class modules that it is
I have a class module in an Excel project that has a property called
I have a simple class library that I use in Excel. Here is a
I have created an Automation Add-in for Excel,implemented as a c# class library, which
I have an Excel Automation Add-in that is registered with COM by my installer.
I have created an Automation Add-in for Excel,implemented as a C# class library, which
I have create an Excel Add-In. In that add-in are some modules and let's
I have made a new Excel 2010 Add-in project, with VS10. When I run
I have written a few Excel 2003/2007 add-ins using VSTO, and I usually end
I have an Excel spreadsheet that I have exported from some other program. It

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.