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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:03:47+00:00 2026-05-20T08:03:47+00:00

I am writing (and teaching myself how to write) an experimental assembly in VB.NET

  • 0

I am writing (and teaching myself how to write) an experimental assembly in VB.NET 3.5 that is exposed via COM and then called from some Excel VBA code to initiate an instance of a class that traps some Excel events and then perform some functions on the active workbook in Excel.

To initiate the class, a reference to the Excel Application is passed from the VBA (I am using a PIA for Excel in the assembly).

I needed to perform a time-consuming operation on the active workbook from my assembly so I decided to use a BackgroundWorker in a WinForm so that I can display a progress dialog for the operation whilst the operation completes in the background.

I would like to know if there are any problems with interacting with Excel via COM using a background worker in this way? Reason for asking is that the main class in the assembly holds the reference to the Excel application object, and this is then passed to the BackgroundWorker so that it can determine the active workbook and then perform some operations on it. I am only accessing the workbook itself (not other objects) through one procedure.

For some reason I have it in my head that Excel might not like this – am I right?

  • 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-20T08:03:48+00:00Added an answer on May 20, 2026 at 8:03 am

    If you are refering to this:

    • Getting a method call from VBA to your VB.NET code
    • Spin off a new thread (or a worker thread) and handing over a reference to the workbook (or to Excel.Application, a Range; it doesn’t matter) (via parameters, statics… it doesn’t matter)
    • Use the original thread to show a UI while the background thread accesses Excel

    Then, no; you cannot do that.

    Excel VBA is an STA environment. All access to the Excel Object Model, in and out, must happen from the same thread. You don’t get to pick the thread either; it has to be the thread that called your method to begin with.

    To run code accessing Excel from a worker thread, you would have to “marshal” the interface pointer to your worker thread. Mashalling creates a pair of COM wrappers (called ‘proxy/stub’ pairs) that shuttle the method call accross threads as needed – this is similar to what happens when you use the Form.Invoke() method in .NET. I don’t know off the top of my head how to do that in .NET. This would not be my first approach anyway.

    Another alternative is NOT to access the Excel object from your worker thread, but to use helpers in the main form object (you do want a form, don’t you?). Let your worker thread call those helpers via Form.Invoke() to make sure they run from the UI thread (the thread that VBA used to call you). This is functionally the same as the first alternative, except that .NET does the work, instead of COM.

    A third approach, is to not use worker threads at all – do it the VB way: create your UI from the one and only thread; show it, and then do your work from the Load event of the Form. Call Application.DoEvents() once in a while (a few times a second) to let the UI operate with some reseamblance of smoothness.

    A fourth alternative is to turn the problem around. Use a brand new thread instead of a pool thread (which is what you get from BackgroundWorker) and handle the UI from there. We reverse roles here: the main thread would do the fancy work, while the extra thread creates a form and shows the UI. Again, make sure you use Form.Invoke() to update the progress window. Make sure you Dispose() your thread when you’re done. This might just be my preferred approach.

    Or just use an hourglass mouse pointer… Just how “slow” is your “slow”?

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

Sidebar

Related Questions

I am teaching myself PowerShell by writing a simple parser. I use the .Net
I'm trying to learn some html/css/javascript, so I'm writing myself a teaching project. The
I've been teaching myself functional programming, and I'm currently writing different higher order functions
I am writing my first project that will use autoconf and teaching it to
Writing documentation in html requires some code examples. What to do with characters that
I'm teaching myself C++, and in the process I'm writing simple little programs to
Writing my first silverlight application. I need to deliver some bitmap that the customer
I'm teaching myself jQuery by writing the game of memory (where you turn tiles
Writing GWT application. I have CellTable and a code that i'v got from google-example
Writing some test scripts in IronPython, I want to verify whether a window is

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.