This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
Place your code into the main sub of the HandlerModule module. The pointer to IModelDoc2 document is passed as the parameter. Use this pointer instead of ISldWorks::ActiveDoc to properly handle invisible documents.
Sub main(model As SldWorks.ModelDoc2)
'TODO: add your routine hereEndSub
Dim swFileLoadWatcher As FileLoadWatcher
Sub main()
Set swFileLoadWatcher = New FileLoadWatcher
WhileTrue
DoEvents
Wend
EndSub
FileLoadWatcher Class Module
Class which handles SOLIDWORKS API notifications
DimWithEvents swApp As SldWorks.SldWorks
PrivateSub Class_Initialize()
Set swApp = Application.SldWorks
EndSubPrivateFunction swApp_DocumentLoadNotify2(ByVal docTitle AsString, ByVal docPath AsString) AsLongDim swModel As SldWorks.ModelDoc2
If docPath <> ""ThenSet swModel = swApp.GetOpenDocumentByName(docPath)
ElseDim vDocs AsVariant
vDocs = swApp.GetDocuments
Dim i AsIntegerFor i = 0 To UBound(vDocs)
Dim swDoc As SldWorks.ModelDoc2
Set swDoc = vDocs(i)
If swDoc.GetTitle() = docTitle ThenSet swModel = swDoc
ExitForEndIfNextEndIf
OnModelLoad swModel
EndFunctionSub OnModelLoad(model As SldWorks.ModelDoc2)
HandlerModule.main model
EndSub
HandlerModule Module
Custom VBA code which needs to be run for each opened document
Sub main(model As SldWorks.ModelDoc2)
'TODO:implement the procedure
MsgBox "File Loaded: " & model.GetTitle()
EndSub
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more