How to fire an event from VBA? Or what to do?

Started by TomasL, January 10, 2011, 05:35:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TomasL

I have a number of functions in a class-module, declared "with-events",
I.e. When a new page is added, data and stuff should be copied from the first page.
This works well within the GUI, if I add a page manually, the event fires, and the code is executed.
But, when I do the same from within VBA, page.add, this event is not fired, and obviously the code is not executed.
Do I need to call the procedure within the class-module, or copy the code to my standard module and run it with a normal call, or is there any other trick I can use.
my sites:
www.ebaman.com www.enrad.se www.tlec.se

Jumpy

Can't help you directly there, but encountered the same problem with opening documents with VBA. The Document_open event didn't fire.
My workarround: Since I was opening the doc with VBA anyway, I added a line in my code to start the Macros I needed, too. Some of those macros only had the job to fill With Events variables.

TomasL

It seems like the event fires sometimes, and sometimes not.
Correct me if I'm wrong:
If the code that adds a page runs straight on, without any brakepoints in any modules, the event PageAdded (and probably others as well) fires.
If I have a BP somewhere in the code so I have to either single-step (F8) or Run (F5) then the event will not fire.

WHat I have to do is to use CellExist on the page to check if the pageAdded code has run or not, if not, I hav to run it.
my sites:
www.ebaman.com www.enrad.se www.tlec.se

aledlund

If I write a routine that I suspect may have to be called outside of an eventhandler, I create it as a subroutine and then just add any necessary setup (and the call) to the eventhandler.

"WHat I have to do is to use CellExist on the page to check if the pageAdded code has run or not, if not, I hav to run it.", IMHO is not a bad thing to do anytime you start adding sections/cells to a drawing.
al

TomasL

What is best practice?
Having all code that should happen after the PageAdded event in the classmodule, and if needed call this function from a normal code-module
Or have all code in a normal code-module and do a simpel call from the PageAdded function in the class-module
Or having two sets of the same code, one in the classmodule, that do all events, and one in the normal code-module, if the ecvent for some reason didn't fire.
my sites:
www.ebaman.com www.enrad.se www.tlec.se

aledlund

I'm not an expert (and certainly not one to give out advice on best practices) but given the differences between platforms (vba vs. .Net) and languages (vb vs. c#, etc.). I'd suggest you code dive on some of the examples in the Visio SDK to see how they accomplish it. When I'm working in an area that I feel might need a stronger platform for data and event handling, I use vb.net.
al