SolidWorks Macros with Visual Basic for Applications (VBA)

SolidWorks Macros with Visual Basic for Applications (VBA)

Introduction



VBA Basic Syntax


Visual Basic is very similar to C++, with some small differences.

Variables

Creating a variable follows a general format " Dim-name-as-type". To create a double the code would look like Dim Test_Variable as Double. Creating a constant is very similar instead of Dim use Const.

Commenting

Comment in VBA is done with an apostrophe similar to the "//" that is used in C++. When used that entire line is commented out. Unlike C++ there is no block comment functionality that surrounds a whole block of text but, there is a button auto inserts an apostrophe at the beginning of each each line that is selected. 

Block Comment Button

 Ending a line

In C++ a semi colon is needed to indicate the end of a line. In VBA nothing is needed to indicate the end of a line, writing on two different lines in the editor is considered to separate executable lines of code.

Modules/ UserForms

Modules are just places to write the code, Adding different modules can keep the code more organized as they can quickly get very larger and hard to find things. Adding more modules gives the user the ability to separate a projects sub routines, functions and the main code. UserForms is how the macro communicates and takes in data from the user. When a UserForm is open it can be edited with the toolbox. In the tool box things can be added like command buttons, text boxes, label and, combo lists. Below is an image of a UserForm and where is insert them: 


UserForm ExampleInserting UserForms and Modules

 Creating a New Sub

Creating a new sub routine is very easy. There are three things to do:

  1.  Indicate is the sub is private or public. If the sub is public it can accessed in any part of the project. If the sub is private it can only be accessed in the module it was written in.   
  2. Naming the sub
  3. Adding variables
    1. Variables need to added to each newly created sub other than the main sub this is because if there are two sub with no variables and the macro is ran outside the complier (through SolidWorks) SolidWorks doesn't know what sub to execute first causing an error. A simple fix to this problem is to just add a variable that is un-used if your sub doesn't have any data coming in from other parts of the code

When these three things are down the new sub should look similar to this:

Private Sub Test(placeholder As Double)

End Sub

Creating a Function

Creating a function works in the same way as creating a sub routine:

  1.  Indicate is the sub is private or public. If the sub is public it can accessed in any part of the project. If the sub is private it can only be accessed in the module it was written in.
  2. Naming the function
  3. Adding variables
    1. Variables need to added to each newly created sub other than the main sub this is because if there are two sub with no variables and the macro is ran outside the complier (through SolidWorks) SolidWorks doesn't know what sub to execute first causing an error. A simple fix to this problem is to just add a variable that is un-used if your sub doesn't have any data coming in from other parts of the code.
    2. The variables also to be either pass by value or pass by reference, to indicate this write add ByVal or ByRef before defining the variables.
  4. Define the type the function outputs. This can be done by writing "as-type" after the function.
  5. To have the function return the value don't use a return statement instead write the name of the function '=' then the value to be returned.

All together the function should look generally like this:

Public Function A_B(ByVal A As Double, ByVal B As Double) As Double

A_B = A + B

End Function

Basic Functions


Below is a word document containing some basic functions and there variables in SolidWorks VBA:

  File Modified

Microsoft Word Document SolidWorks VBA Code Variables Wiki.docx

Dec 17, 2020 by Former user

Setting Up Macros in SolidWorks


Macro Tab

In order to be able to write macros in SolidWorks macros need to be enabled. To do this go to settings click the drop down menu and select Customize.

In the toolbar tab scroll to macro, check off the box and press Okay.

After this the macro toolbar should appear next to the other tool bars.

Macro Tab Buttons

When in the Macro Toolbar there should be 5 buttons available Run Macro, Stop Macro, Record\Pause Macro, New Macro, Edit Macro. If any of these buttons are missing go to Customize >> Commands >> Macro. From there you can drag and drop the required buttons.

  • Run Macro- this buttons allows the user to select the an already made made macro from file.
  • Stop Macro- Stops a macro being run.
  • Record\Pause Macro- this is a very handy button to use. This button allows for the user to record the design of a part and have SolidWorks autogenerate the code to recreate what the user just modelled. This function saves lots of time that would be wasted on write many lines of code for simple tasks. This button also allows for pausing the recording process. Pausing is very important to do when changing the view of the model as it adds potentially hundreds of extra lines of code that are unnecessary to execute that macro and makes for editing the code much harder to organize.
  • New Macro- This button creates a new macro and opens it in the SolidWorks VBA editor.
  • Edit Macro- The button allows for a macro to be edited in the SolidWorks VBA editor.
  • Custom Macro- This type of button allows the user to create a button that runs a premade button. To add these buttons go to  Customize >> Commands >> Macro and drag and drop the New Macro Button.

Contributors:

No contributors found for: authors on selected page(s)