Event listeners are used to execute code when a certain event takes place, such as a user creating a new part, or opening an assembly.
How to program event listeners using macros
Event listeners allow you to run code when a user performs a certain action. For example when the user clicks new document an event listener could trigger code to save the currently active document.
To set up your event listener you need to start by creating a new macro.
To create an event listener a class module is necessary, it is okay to remove all of the swApp references in the main method as they are not required. the only tricky step to setting up your event listener is this step, you have to get the syntax correct here:
Private WithEvents swApp as sldworks.sldworks
Once that is typed in it is possible to use the pull down to do most of the setup. First on the left pulldown select class, this will create an initialize function for our class, in here type:
set swap = application.Sldworks
Next on the left pulldown select swapp, this will create a file open listener, on the right pulldown select FileNewNotify. The FileOpenNotify function can be deleted if you don’t want to handle the event. Set the FileNewNotify function to run the following statement:
swApp.SendMsgToUser “New Document has been clicked”
This will let us know that the event listener is working properly when we create a new document in SolidWorks.
Return to the main module, this is the code that run when the macro is run, so it must use the class for events to be handled.
First you must declare a global variable to hold the event listener class, then you must set it to an instance of the class.
Dim eventListener As classEvents
Set eventListener = New classEvents
Now run your macro. When you click new and select a template a message box should pop up with the message you typed.
Leave a Reply