Receive Notification when Document Changes Example (VBA)
Certain actions, such as changing the active window, have special code
attached to them that send notifications when the action occurs. Programs
can receive these notifications and execute code based on the notification
that has occurred.
This example shows how to intercept notifications from the SOLIDWORKS
application. Whenever the active document is changed, this program intercepts
the notification and displays the path to the new active document.
NOTE: See the MSDN Library for
details about the SetWindowPos function.
'---------------------------------------------
'Constants that define the actions of SetWindowPos
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const SWP_WNDFLAGS = SWP_NOMOVE Or SWP_NOSIZE
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
'Set up the API call to SetWindowPos
'Declare the SOLIDWORKS object as Private (or Public if
'declared in a public module) WithEvents.
'This sets it up to receive notifications
'from the SOLIDWORKS application.
Private Declare Function SetWindowPos Lib "user32"
_
(ByVal
hwnd As Long, _
ByVal
hWndInsertAfter As Long, _
ByVal
X As Long, _
ByVal
Y As Long, _
ByVal
cx As Long, _
ByVal
cy As Long, _
ByVal
wFlags As Long) As Long
'Define the swApp object so that it responds to events
'Declare a function <ObjectName>_<Notification>
(swApp_ActiveDocChangeNotify)
'with a return type as long. Whenever
the notification occurs,
'this function will be called. In this example, it displays
'the path of the current document.
Private WithEvents swApp As SldWorks.SldWorks
Dim Model As SldWorks.ModelDoc2
Private Sub Form_Load()
Dim
isGood As Long
'Keep
the Visual Basic form on top
isGood
= SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_WNDFLAGS)
'Attach
to the SOLIDWORKS application
Set
swApp = GetObject(, "sldWorks.Application")
Set
Model = swApp.ActiveDoc
End Sub
'Whenever the ActiveDocChangeNotify notification occurs,
'fire off this function
Private Function swApp_ActiveDocChangeNotify() As Long
'Get
the active document
Set
Model = swApp.ActiveDoc
'Display
its path and name on the form
Form1.Label2.Caption
= Model.GetPathName
End Function