Edit or Debug SOLIDWORKS Macro
Edit or debug SOLIDWORKS macros using Microsoft VBA or Microsoft VSTA.
NOTES:
VSTA Requires Microsoft .NET Framework 3.5.
-
When debugging Microsoft VSTA macros with user-interface
components such as PropertyManager pages, manipulators, or other objects
that use events or handler objects, the debugger must continue to run
after the main() method of the VSTA macro exits. Either deselect the user-interface
system option Stop VSTA debugger on macro
exit (located on the Tools >
Options > System
Options dialog) or set swUserPreferenceToggle_e
swStopDebuggingVstaOnExit to false to keep the debugger running after
the main() method of the Microsoft VSTA macro exits.
To
automatically edit a macro after recording it, click Tools
> Options > Systems Options.
On the General tab, select Automatically edit macro after recording
and click OK. This setting is
persistent across SOLIDWORKS sessions unless you selected to save a just
recorded macro as SW All Macros Types
(*.swp, *.csproj, *.vbproj). This setting has no effect when you
select to save a macro as multiple macro types.
If
you recently edited the macro, you can select it from the menu when you
click Tools > Macro.
This menu lists the last nine macros that you edited.
To edit or debug a SOLIDWORKS macro:
Click Edit Macro
on the Macro toolbar, or click Tools
> Macro > Edit.
In the dialog, select a macro file and click Open.
To edit a Microsoft VBA macro, select SW VBA Macros (*.swp) in Files of type
(selected by default).
To edit a Microsoft VSTA macro, select SW VSTA VB Macro (*.vbproj) or SW VSTA C# Macro (*.csproj).
NOTE: You can also edit .swb
files, which are older-style SOLIDWORKS macro files. When you run or edit
a .swb file, it is automatically
converted to a .swp file.
Edit or debug the macro. If it is a new macro,
be sure to:
Delete extra lines
of code automatically inserted in the macro when it was created:
For example,
the following variables are declared automatically in a SOLIDWORKS VBA
macro. Delete any variables not used in the macro.
Dim
swApp As Object
Dim
Part As Object
Dim
boolstatus As Boolean
Dim
longstatus As Long, longwarnings As Long
Dim
FeatureData As Object
Dim
Feature As Object
Dim
Component As Object
Delete all lines
of code that change the view.
Delete all IModelDocExtension::SelectByID2
calls appearing immediately before IModelDoc2::ClearSelection2
calls. However, do not delete IModelDocExtension::SelectByID2 calls appearing
immediately after ModelDoc2::ClearSelection2 calls.
Delete all IModelDoc2::ClearSelection2
calls appearing immediately before IModelDocExtension::SelectByID2.
Explicitly
declare all variables in a Microsoft VBA macro.
Early
bind all variables.