Macros in SOLIDWORKS are a great way to save time from having to perform repetitive tasks. A single macro can range anywhere from performing a few simple tasks to complex programs that will change depending on user input.
This guide will walk you through how to get started creating macros and how to assign your macro to a button on your user interface for easy access.
CREATING A NEW BLANK MACRO
To create a new blank macro in SOLIDWORKS, go to Tools > Macro > New.
Tech Tip: You can also activate your macro toolbar by Right-Clicking on the Command Manager and selecting Toolbars > Macro.
Name your macro and choose where to save it.
Tech Tip: You have a choice between three filetypes to save your macro. The easiest filetype to use is .swp which uses the Visual Basic for Applications (VBA) programming language and can be edited in the built-in SOLIDWORKS VBA Editor. The .vbproj and .csproj both require Microsoft Visual Studio to edit and compile and use VB.Net and the C# programming languages, respectively.
Once your macro is saved, you will see the following code populate in the VBA Editor in a separate window.
If you know how to program in VBA, you can add or remove code as needed here to complete your macro.
However, if you’re a beginner to macros, or even an experienced user looking for a little help with naming conventions and syntax, you can always use the Macro Record feature.
RECORDING A NEW MACRO
To begin recording your new macro, go to Tools > Macro > Record. Be aware that while your macro is recording, SOLIDWORKS will capture any mouse clicks, keystrokes, menu choices, as well as commands and inputs.
Tech Tip: Keep in mind, the SOLIDWORKS Macro recorder will NOT capture every command while recording. Some commands aren’t supported by the recorder and any missing steps will have to be manually edited into your macro when finished recording.
Continue by performing each task you would like your macro to capture, making sure to limit any unneeded commands or keystrokes.
Once you’ve completed your steps, select the Stop macro button. You will then be prompted to save your macro.
After using the Record Macro command, always edit your code by going to Tools > Macro > Edit. Although the command names and syntax will be correct, the macro may not use the most efficient logic and will almost always need to be cleaned up before use.
Tech Tip: In Tools > Options > System Options > General > Automatically edit after recording, you can set SOLIDWORKS to always edit your macro automatically after recording.
The last step in our process in creating a macro will be mapping the new macro file (.swp) to a button on your user interface.
CREATING A MACRO BUTTON
To create a button on your toolbar, open the Customize menu by Right Clicking Command Manager > Customize, Tools > Customize, or by selecting the drop-down menu next to options and selecting Customize.
Once in the Customize menu, navigate to the Commands tab, and the Macro category. In the Macro category, Drag and Drop the New Macro Button command onto the Command Manager where you want your new macro button to be.
Tech Tip: You can place your new macro button on any Command Manager Tab or Toolbar that is visible.
Once you’ve placed the New Macro Button where you want it, you’ll see the following prompt. Press the browse button (…) and locate your previously saved .swp file.
You can select Choose Image to change the macro icon to a bitmap image of your choosing.
Tech Tip: You can either create your own bitmap image, or select from SOLIDWORKS provided icons located in C:\Program Files\SOLIDWORKS 20xx\SOLIDWORKS\data\user macro icons.
The Tooltip will display the macro name when you hover over it and the Prompt will display the description.
To edit any macro button after creating it, go back to the Customize menu, and Right Click Macro > Properties.
Tech Tip: You can also remove any button completely by dragging it off the toolbar while in the Customize menu.
If you would like to learn more about macros in SOLIDWORKS, check out the API Help files, our API Training courses, or if you would like us to write a custom macro for your needs, we also offer paid consulting services.
Computer Aided Technology, Inc.