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
Macro provides several options to format the title which can be modified by changes in the values of the constants in the macro.
Const NMB_SRC_FILE_PATH AsString = "D:\prt.txt"'path to store the current part indexConst NMB_FORMAT AsString = "000"'padding for the number, e.g. 001, 002, instead of 1, 2Const BASE_NAME AsString = "PRT-"'Base prefix for file naming
Follow the video tutorial in the Run Macro On Document Load article for the guide of running this macro automatically for each newly created model.
Const NMB_SRC_FILE_PATH AsString = "D:\prt.txt"Const NMB_FORMAT AsString = "000"Const BASE_NAME AsString = "PRT-"Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
Dim lastNumber AsInteger
lastNumber = ReadNumber(NMB_SRC_FILE_PATH)
Dim thisNumber AsInteger
thisNumber = lastNumber + 1
Dim name AsString
name = BASE_NAME & Format(thisNumber, NMB_FORMAT)
IfFalse = swModel.SetTitle2(name) Then
Err.Raise vbError, "", "Failed to set title"EndIf
StoreNumber NMB_SRC_FILE_PATH, thisNumber
EndSubFunction ReadNumber(filePath AsString) AsIntegerDim fileNo AsInteger
fileNo = FreeFile
Dim number AsString
Open filePath For Input As #fileNo
Line Input #fileNo, number
Close #fileNo
ReadNumber = CInt(number)
EndFunctionSub StoreNumber(filePath AsString, number AsInteger)
Dim fileNo AsInteger
fileNo = FreeFile
Open filePath For Output As #fileNo
Print #fileNo, CStr(number)
Close #fileNo
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