Learn how to develop modern SOLIDWORKS® add-ins from scratch at 3DExperience® World 2020
This VBA macro allows to disable (or password protect) the editing of the sheet format in SOLIDWORKS drawing using SOLIDWORKS API.
This macro can be useful where it is required to disallow users to modify the standard sheet format.
Macro provides 2 options which could be configured by changing the constants below:
Public Const LOCK_WITH_PASSWORD As Boolean = False Public Const PASSWORD As String = ""
Locking the editing
Set the value of LOCK_WITH_PASSWORD to False.
The following message is displayed every time the Edit Sheet Format command is called and command is cancelled:
Password protecting the editing
Set the value of LOCK_WITH_PASSWORD to True. Set the value of PASSWORD to the target password.
It is recommended to password protect the VBA macro, so the password cannot be seen from the macro itself.
The following prompt is displayed every time the Edit Sheet Format command is called
If password matches, the sheet format can be edited, otherwise the command is cancelled and error message is displayed.
Creating the macro
- Create new macro
- Add new class module and name it SheetFormatEditorHandler. The files tree should look similar to below image.
- Paste the code below into corresponding modules.
- Follow the Run Macro On SOLIDWORKS Start for the instruction of setting up the automatic run of the macro on SOLIDWORKS startup.
Public Const LOCK_WITH_PASSWORD As Boolean = True Public Const PASSWORD As String = "admin" Dim swSheetFormatEditorHandler As SheetFormatEditorHandler Sub main() Set swSheetFormatEditorHandler = New SheetFormatEditorHandler End Sub
Dim WithEvents swApp As SldWorks.SldWorks Private Sub Class_Initialize() Set swApp = Application.SldWorks End Sub Private Function swApp_CommandOpenPreNotify(ByVal Command As Long, ByVal UserCommand As Long) As Long Const swCommands_Edit_Template As Long = 1501 If Command = swCommands_Edit_Template Then Dim cancel As Boolean cancel = True If LOCK_WITH_PASSWORD Then Dim pwd As String pwd = InputBox("Sheet format editing is locked. Please enter password to unlock") If pwd = PASSWORD Then cancel = False Else swApp.SendMsgToUser2 "Password is incorrect", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk End If Else swApp.SendMsgToUser2 "Sheet format editing is locked", swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk End If swApp_CommandOpenPreNotify = IIf(cancel, 1, 0) End If End Function