Results 1 to 9 of 9
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Event not available if control added by macro (Word97 (NT))

    Hi

    I have a set of macros that build a dialog box on the fly (based on data held in a separate Word document).

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Event not available if control added by macro (Word97 (NT))

    Dale,

    Just curious how you are going about trying to get the Change event to run.
    For a Change event for a control to run, you've got to have a Change event procedure; if you're creating the control programatically then you would need to create the Change event procedure programatically at the same time that you create the control.

    In my experience, I've never been able to get that to work.
    There may have been some previous threads on this here - perhaps one posted by Troy Wells, and someone may have posted a link to some code by John Walkenbach that purports to do allow you to do this - I couldn't get it to work. Let me know if you have trouble tracking down earlier links and I'll have a dig.

    Gary

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Event not available if control added by macro (Word97 (NT))

    Thanks, Gary.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Event not available if control added by macro (Word97 (NT))

    You can programmatically insert lines into a module using the InsertLines method of the module object, although it requires finding the end of previous routines and I would recommend using a module that does nothing but handle the insertion of code into other modules. Then you would need to set the event property of the form or control to "[Event Procedure]" in order to make the event fire. <img src=/S/crossfingers.gif border=0 alt=crossfingers width=17 height=16>

    It might be easier to use a class module that already contains the code you want to use and simply bind the form or control to that class at runtime using WithEvents automation. You still would have to use code inside the class module to set the event procedure property to "[Event Procedure]", but the class itself can take care of that in its initialization routine. You could pass in arguments to tell the class which events to use or how to use them. I do this a lot in Access and it should work the same in any VBA container app.
    Charlotte

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Event not available if control added by macro (Word97 (NT))

    Dale,

    Haven't tested it, but I'd expect an event procedure, written at design time for a control which does not yet exist, to cause a compile error.
    But even if it doesn't cause a compile error, I wouldn't expect VBA to be smart enough to associate a subsequent, newly-created instance of a control, with a procedure which had been previously written.

    There have been some detailed discussions here on creating code programatically (Charlotte touches on the basics) - have a look for these, but the problem I've had in this relation is that I can't programatically create code in a class module which is already initialized (your userform being an example of one) - causes Word to crash with all kinds of ugly messages. <img src=/S/devil.gif border=0 alt=devil width=15 height=15>

    The class module method that Charlotte suggests sounds interesting - I've never used it; don't know whether that will work in Word or whether it's just an Access thing.

    Gary

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Event not available if control added by macro (Word97 (NT))

    Charlotte,

    This sounds intriguing, particularly if it can be used in non-Access Office apps.
    Would you happen to have a simple sample that illustrates what the code in the form module and class module should look like?

    Thanks,
    Gary

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Event not available if control added by macro (Word97 (NT))

    Not for Word, unfortunately. However, the principles are the same regardless of the specific app. You could use code in the form itself to test for the existence of a particular control and bind the particular control to a Word-version of a class similar to the atached Access clsTextBox by passing the control as an argument to the initialization routine of the class. In the form, you could declare a module level object variable of the class type and in the appropriate event of the form, bind the object variable to an instance of the class object. In the class, you would have code for the events you need to trap and handle for that control.
    Attached Files Attached Files
    Charlotte

  8. #8
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Event not available if control added by macro (Word97 (NT))

    Hi Dale,
    The attached is an extremely simple version of what Charlotte was talking about for Word. If you want different textboxes to have different routines you could either declare different textbox variables in the clsTB class or have a simple select case statement that checks to see what it ought to be doing. How easy that will be to implement obviously depends on the specifics of what you're doing, but I hope this gives you an idea of where to start!
    Oh, you'll need to go into the VBEditor and run the form - I was too lazy to set that up! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Attached Files Attached Files
    Regards,
    Rory

    Microsoft MVP - Excel

  9. #9
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Event not available if control added by macro (Word97 (NT))

    Thanks, all, for your help. I'll give it a go.
    Dale

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •