I have a simple macro that notifies whenever left mouse button is pressed. and shows the coordinates of the mouse pointer. There is a form with a button on it. When you press the button it starts the mouse event. I want to know how do i bring the command back to code (main) after the event it fired. If you look below i want "Hey" to be displayed after the i press the left mouse button and coordinates at the mouse tip are shown.
I have attached the code as well
I am new to mouse events and did not find anything that could solve this problem. Any help will be appreciated.
Sub main()
UserForm1.Show vbModeless
MsgBox ("Hey")
End Sub
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swView As SldWorks.ModelView
Dim WithEvents swMouse As SldWorks.Mouse
Dim X_, Y_ As Double
Dim listenForClick As Boolean
Private Sub CommandButton1_Click()
Set swModel = swApp.ActiveDoc
Set swView = swModel.ActiveView
Set swMouse = swView.GetMouse
listenForClick = True
End Sub
Private Function swMouse_MouseLBtnDownNotify(ByVal X As Long, ByVal Y As Long, ByVal WParam As Long) As Long
If listenForClick Then
X_ = X
Y_ = Y
MsgBox (X & "; " & Y)
listenForClick = False
Unload UserForm1
End If
End Function
Private Sub UserForm_Initialize()
Set swApp = Application.SldWorks
End Sub
SolidworksApi macros
Hi Prashant,
I think the simplest approach for you is to show your form as Modal form. This would solve your issue.
Sub main()
UserForm1.Show vbModal
MsgBox ("Hey")
End Sub
Hope it helps!!!
Rajat Jain
Hi Rajat,
This does not solves the issues. As when you make the form modal it stays on the top and does not let me click on the screen.
Regards,
Prashant Mohan
Hi Prashant
Did you see this example in the help file?
2016 SOLIDWORKS API Help - Run SOLIDWORKS Commands and Synthesize Mouse Events Example (VBA)
Hi Rob,
Yes i have looked at it too. It has the same issue, mouse events occur after the code is finished running. I want my code to wait till at least one event is fired (wait for me to click the mosue, fire the event and then proceed with code.
Do you really need userform here?
I have attached both, with userform (Macro1) and without userform (Macro2) here.
Hope this helps....
Omkar
Hi Omkar,
I tried both the versions. However, the macro goes directly to the sub function and displays the message "Hey". It does not even invoke the form or fire mouse event.
Regards,
Prashant
Prashant,
Please study attached.
Or, just in case you actually want to use the X and Y values...
That worked Josh,
thank you much. Highly appreciated.
Regards,
Prashant
Prashant
I can see you got your answer which is great:) I am interested in learning how to handle events so had a go at this myself and got something that seems to work, but I am not sure if it is a good way or not. Whilst I was working it out I found I had to be very careful or Solidworks would crash when I unloaded the form or set the mouse to Nothing in the wrong place.
I would love to hear from an expert if the following apporoach is 'a good' or 'a bad' idea. . Please would you be kind enough to comment
<p>Public e As MyEvents</p><p></p><p>Sub main()</p><p> Set e = New MyEvents</p><p>End Sub</p><p></p><p>Public Function ShowForm()</p><p> UserForm1.Show vbModeless</p><p>End Function</p><p></p><p>Public Function CloseForm()</p><p> Unload UserForm1</p><p>End Function</p><p></p><p>Public Function DoNextThing()</p><p> MsgBox ("Hey!")</p><p>End Function</p><p></p><p>Public Function Finish()</p><p> Debug.Print ("finish events")</p><p> Set e = Nothing</p><p>End Function</p>Rob... It seems like you're trying to make a macro-in-progress sit and wait. You really don't want to do that.
The reason Prashant has a UserForm is (I assume) that you can't declare WithEvents variables in a regular module. You have to use a Class Module or a UserForm.
Once you declare something WithEvents, you'll get notified whenever the event you're interested in happens to the object that's WithEvents'd. As long as that object exists (isn't Nothing and doesn't go out of scope), you'll get the notification.
As long as a form is loaded, it's variables declared at Module Level don't go out of scope. They belong to the form. Once the form is unloaded, they go out of existence and you don't get the notify anymore.
A class module is sort of like the form, except that it's not visible. Once you instantiate it, it won't go away until you set it to Nothing. The class instance will sit there just like the form does and wait for you to do something with it. It can close itself too.
This may or may not make sense. I'm typing stream of consciousness.
Look at this one.
Very Nice... Thankyou
Yes that is correct. Also with form this gives me the ability to let user actually click on a button (like select a face) and then proceeds to select a face on an open part/assembly. This makes it more intuitive.
Regards,
Prashant
i thought you will run it step by step.
Also, you could have created a shortcut with proper starting procedure.
Anyways, Its good that you've found the solution though...
Omkar