Hide Table of Contents

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

 



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Notifications Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.