Combobox Events in an Add-in

I have an application that displays a list of “documents” for a particular product. Most of the documents are pdfs, but some are iso images for burning a CD. The display sheet shows each CD associated with the product, and for each CD the iso, the label that’s stuck to the CD, and the pdfs that should accompany it. The user selects the product, operating system, and version from comboboxes to display the appropriate documents.

Normally, the code for a combobox lives in the containing sheet’s class module (like Sheet1). However, good coding practice dictates that all of the VBA be in the add-in and not in the template. To accomplish this, I made a custom class module to house the three comboboxes and respond to their events. The class is named CSheetEvents and contains these variable declarations:

The variables are private to the class module and I use Property Get and Property Set procedures to assign them. An example is:

When the user selects the appropriate menu item, the code opens the template and assigns the three comboboxes. The relevant portion of that code is:

The variable gclsSheet is a global variable that holds an instance of the CSheetEvents so that it doesn’t go out of scope. With the comboboxes properly assigned inside the class, they will now respond to events. The change event of cbxProduct fills cbxSystem. The change event of cbxSystem fills cbxVersion. And the change event of cbxVersion lists the CDs and hyperlinks to all of the documents.

When the user has printed the documents, he closes the sheet. Since I’ve used a template, there is an unsaved document named AllCDConfigurations1 – a workbook created from the template AllCDConfiguration.xlt. I don’t want the user to be faced with a message to save this document, so I also included a workbook object in my class module. By defining a workbook variable with the WithEvents keyword, I can capture the Before_Close event and eliminate that save message.

The Cancel = True part stops the normal close operation and the mobjWb.Close False part closes the workbook without saving changes.

11 thoughts on “Combobox Events in an Add-in

  1. This morning I was wondering if I could put a text box in a class. I figured I would find something about it on DDOE if I searched hard enough. Lo and behold, this was the top entry at the time! Thanks very much.

    It appears, however, that some events don’t fire, even when run from an interactive form. It is the events that aren’t supposed to fire when the event occurs through code. So even if I am using the form interactively, the textbox _AfterUpdate event doesn’t fire because the event handler is residing in a class. Am I doing this wrong?

  2. Bob: There are few Textbox events that aren’t exposed via automation and AfterUpdate is one of them. If I remember correctly, the AfterUpdate event is at the container layer (userform or sheet) so it shows in the container class module. But since only the control is defined in your custom class module, there is no container layer to house that event. If you can figure out how to use the Change event, that’s about the only work-around I know.

  3. I didn’t realize this about a textbox’s AfterUpdate events. I often use its Change or Exit event for the same purpose.

  4. Jon, I’m almost certain that the Exit event is also a container event, and that’s why Dick said that the Change event is the only work-around, and of course it’s not a very good one.

  5. jkp: I tried that yesterday. I put a withevents userform variable and a withevents textbox variable in a custom class, but I wasn’t able to “contain” one inside the other – they were both just contained in the custom class. The linking mechanism must not be exposed. Also, you can’t WithEvents another custom class because it doesn’t “source automation objects” or some such thing.

  6. Dick wrote:
    “jkp: I tried that yesterday. I put a withevents userform variable and a withevents textbox variable in a custom class, but I wasn’t able to “contain” one inside the other – they were both just contained in the custom class. The linking mechanism must not be exposed. Also, you can’t WithEvents another custom class because it doesn’t “source automation objects” or some such thing.”

    It gets worse. I’ve done — or at least tried to do — things along these lines for several years now.

    One can declare a WithEvents MSForms.Control variable. Once that’s done, it exposes several events including Enter and Exit.

    However, when one tries to set the variable to a real control, VBA raises a runtime error about “control does not support these events.” Even if one just declares the variable and has no event procedures, VBA still raises the same error.

    I have a fair bit more on this subject. Hopefully, I’ll put it on my website over the weekend. The material includes a tutorial on how to deal with certain events raised for userform controls (very much like Dick’s blog post), an extension to create a jigsaw puzzle in an userform, and a large “black box” class module to deal with events for every standard userform control.

  7. Tushar Mehta wrote:

    “I have a fair bit more on this subject. …I’ll put it on my website over the weekend. The material includes a tutorial on how to deal with certain events raised for userform controls …, and a large “black box” class module to deal with events for every standard userform control.”

    Anyone know if this is available?

    thx


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.