Hide Table of Contents

Edit or Debug SolidWorks Macro

Edit or debug SolidWorks macros using Microsoft VBA or Microsoft VSTA.

NOTES:

  • 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:

  1. Click Edit Macro on the Macro toolbar, or click Tools > Macro > Edit.

  1. 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.

  2. 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.



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:   Edit or Debug SolidWorks Macro
*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) 2010 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.