2

I have exposed some functions from my C# VSTO ribbon and I can call them with a VBA Excel button. One of these functions is an asynchronous function - its operations are placed on a separate thread so it does not lock Excel. I would like that when I click it, the button is disabled (cannot be clicked again) until the async function is completed. Is this possible with VBA, if so, how?

Here is an implementation of my code so far:

Sub myButton_Click()
   ' disable this button'
   Dim addIn As AddIn
   Dim automationObject As Object
   Set addIn = Application.AddIns("myAddIn")
   Set automationObject = addIn.Object

   automationObject.myAsyncFunc
   ' enable this button when myAsyncFunc finishes execution' 
End Sub
4
  • Can you use the Excel Application to your AddIn, then call the enable from the add in based on this info? Commented Sep 20, 2018 at 14:05
  • 1
    Idiomatic asynchronous VBA code would leverage events, but then events require early binding AFAIK. I'd look into exposing some AsyncFuncCompleted event on the C# side, and handling in on the VBA side using a Private WithEvents automation As TheEventSourceClass and Private Sub TheEventSourceClass_AsyncFuncCompleted() - if needed you could even pass data through the parameters. Commented Sep 20, 2018 at 14:08
  • Suggestion: expose an object property on the addin to make a clearer API and avoid polluting the add-in class with lower-level concerns that don't really belong there. e.g. Set automation = addIn.AutomationAPI Commented Sep 20, 2018 at 14:09
  • ugh that would be Private Sub automation_AsyncFuncCompleted(), mistyped that, too late to edit Commented Sep 20, 2018 at 14:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.