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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:41:53+00:00 2026-05-20T14:41:53+00:00

I have a C# helper class ( ExcelMacroHelper ) that assists with injecting VBA

  • 0

I have a C# helper class (ExcelMacroHelper) that assists with injecting VBA macro code into open Excel workbooks and running the resulting macros. I just realized that the following steps would break my code:

  1. C# application injects macro code into the active workbook, which causes the ExcelMacroHelper to mark its state as ready to run macros

  2. The user switches to a different workbook in Excel.

  3. The C# application attempts to run a macro. The ExcelMacroHelper will think it is ready to run macros, but the VBA code was injected into a different workbook so the call will fail.

To fix this, I think I need some way of setting a temporary property on the Workbook object indicating that my macro code has been injected into it, or at least a way of maintaining a list of Workbooks that have been handled. Any ideas?

  • 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-20T14:41:53+00:00Added an answer on May 20, 2026 at 2:41 pm

    I ended up using custom document properties. Something like this:

    private bool needToInjectMacroCode() {
        // Get custom document property with name tagPropertyName
    
        object properties, property, propertyValue;
    
        properties = excel.ActiveWorkbook.GetType().InvokeMember(
            "CustomDocumentProperties",
            BindingFlags.Default | BindingFlags.GetProperty,
            null, excel.ActiveWorkbook, null);
    
        try {
            property = properties.GetType().InvokeMember(
                "Item",
                BindingFlags.Default | BindingFlags.GetProperty,
                null, properties, new object[] { tagPropertyName });
        } catch (TargetInvocationException) {
            return true;
        }
    
        propertyValue = property.GetType().InvokeMember(
            "Value",
            BindingFlags.Default | BindingFlags.GetProperty,
            null, property, null);
    
        return (tagString != (propertyValue as string));
    }
    
    // ...
    
    private void setMacroCodeInjected() {
        // Set custom property with name tagPropertyName to value tagString
    
        object properties = excel.ActiveWorkbook.GetType().InvokeMember(
            "CustomDocumentProperties",
            BindingFlags.Default | BindingFlags.GetProperty,
            null, excel.ActiveWorkbook, null);
    
        try {
            properties.GetType().InvokeMember(
                "Add",
                BindingFlags.Default | BindingFlags.InvokeMethod,
                null, properties, new object[] {
                    tagPropertyName, false,
                    Office.MsoDocProperties.msoPropertyTypeString,
                    tagString
                });
        } catch (TargetInvocationException) {
            object property = properties.GetType().InvokeMember(
                "Item",
                BindingFlags.Default | BindingFlags.GetProperty,
                null, properties, new object[] { tagPropertyName });
    
            property.GetType().InvokeMember(
                "Value",
                BindingFlags.Default | BindingFlags.SetProperty,
                null, property, new object[] { tagString });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a helper class pulling a string from an XML file. That string
I have a helper class that is just a bunch of static methods and
I have a base class for many tests that has some helper methods they
I have a helper class that implements ITypedList, to provide objects for databinding against
I have a helper class that does a simple but repetitive process on a
I have some ASP.NET web services which all share a common helper class they
I have a Browser Helper Object project in c# that (obviously) references shdocvw.dll. I
Let's say that I have the following code in application_helper.rb : def do_something if
I have an helper class with some static functions. All the functions in the
In my console application have an abstract Factory class Listener which contains code for

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.