Over the course of this series, we have explored some concepts of using VBA to create simple macros. However, all of the programs we have created so far only runs once, and there is no way for users to interact with the program. This is where UserForms comes in.
UserForms provide a graphics user interface (GUI) to allow users to utilize the program that you have written. VBA provides a simple drag and drop interface for creating these GUIs. The simplicity of VBA’s UserForm does comes with the drawback of less customizability and dynamics found in modern GUIs. Nonetheless, it is still sufficient for most simple projects.
We will be exploring how to create UserForms and ways to make interactive programs in this lesson.
The VBA UserForm
To create a UserForm, right click anywhere within the Project Explorer and select Insert > UserForm.
Once you have created a new UserForm, you will be greeted with a blank UserForm aptly named UserForm1.
By default, the toolbox should show up as a new UserForm is created. However, in the event that it does not, simple click on view and click on toolbox. You should see a layout similar to the one below. The icons of your toolbox may look different depending on the version of SolidWorks installed, but the functionality is still the same.
In the properties window on the left, you have access to the hidden properties of the UserForm. In there you are free to customize many different aspects of your UserForm such as the color, size, fonts, etc.
Take note that the property field, (Name), refers to the name of the UserForm used in the program. You will be calling methods and properties from the name given in this field. The property field, Caption, allows you to change the text displayed on the top of the UserForm. For this lesson we will keep all of the properties to their default values, but feel free to explore some of them yourself.
Toolbox and controls
Next we will be talking about the Toolbox. Within the Toolbox lies all of the controls that you can place within your UserForm. You can find a summary of what each control does in this page, under the summary of form controls section.
To add a control to your UserForm, simply drag and drop the desired control onto your UserForm. Position and resize the controls using the drag handles on the controls.
Each control has their own properties as well. They can be accessed in the properties window. The properties windows will automatically show the properties of the control that is clicked and selected.
It is good practice to give each control a proper name to be able to easily identify the controls in your code. Names such as CommandButton1, CommandButton2 and CommandButton3 will prove to be very confusing as your project scales.
Personally, I stick to using a format for naming controls, by placing the control name in front followed by the function of the control. For example, if a button is used to close a document, I will name it CommandButtonCloseDocument. Although the names may be lengthier, it saves me the time of finding out the names of the buttons repeatedly.
Example UserForm Program
Setting up UserForm
We will be using the code from the previous lesson to create a new program where there is a GUI for users to switch between open documents as well as close documents. We will be reusing the same main program and class, with some minor modification to the main program.
First, we will setup our UserForm. We will be using a label control, a listbox control and a commandbutton control. Place the controls similar to the example below. All controls will use their default properties with the exception of their caption modified to show the proper text.
The label control is used to show a static text, “Open Documents:”. The listbox control will be used to list out all of the currently open documents. Clicking on any of the listed documents will also make the document active.
Lastly, the commandbutton will be used to close the selected document in the listbox.
I did not change any of their names as there are only one of each control, and it is immediately obvious what each control does. However feel free to change their names as a practice.
Adding code to controls
With the UserForm ready, we will need to add some code to the background to define how each control behaves. To do so, go to the project explorer, right click on the UserForm, and click on View Code.
You will now see a new blank area in the code window. This area is where you program all of the interactions for your UserForm.
At the top of the code window, you will see two comboboxes. The one on the left lists all of the controls in your UserForm, while the one on the right lists all of the event/procedure available for the control.
By firstly selecting the control, followed by the procedure, VBA will automatically insert a blank Sub for the procedure.
We will be inserting two click procedures, one each for the CommandButton1 and ListBox1. I will include the code first before explaining how they work.
UserForm1 code:
Private Sub CommandButton1_Click() For X = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(X) = True Then Dim swapp As SldWorks.SldWorks Set swapp = Application.SldWorks swapp.CloseDoc(ListBox1.List(X)) ListBox1.RemoveItem(X) End If Next X End Sub Private Sub ListBox1_Click() For X = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(X) = True Then Dim swapp As SldWorks.SldWorks Set swapp = Application.SldWorks swapp.ActivateDoc(ListBox1.List(X)) End If Next X End Sub
CommandButton1_Click()
The first procedure, CommandButton1_Click, is tied to the CommandButton1. Whenever the CommandButton1 is clicked, the code within this procedure will run once.
A listbox works similar to a collection used in the previous lesson. You can add or remove strings from the listbox. The listbox comes with the methods AddItem and RemoveItem to help with this process.
The AddItem method takes in a string and adds the string to the end of the listbox. Meanwhile, the RemoveItem takes in an integer and removes the item at the index of the integer.
The main program code in the next section of this lesson shows how each open document is added to the listbox.
As CommandButton1 is used to close the selected document in ListBox1, we will have a “For” loop to check each document within the listbox if it is selected. When the “For” loop finds the selected document in the listbox, it will run the SldWorks::CloseDoc method to close the selected document.
Lastly, the document is removed from the listbox by using ListBox1.RemoveItem.
ListBox1_Click()
The second procedure, ListBox1_Click, is tied to the ListBox1. The code within this procedure will run once when an item is clicked in the ListBox1, .
Similarly, we will use a “For” loop to find which item is clicked in the ListBox1. This is then followed by using the SldWorks::ActivateDoc method to activate the selected document.
Main program and class code
Main Program:
Dim swapp As SldWorks.SldWorks Dim swDoc As SldWorks.ModelDoc2 Dim openDocs As Variant Dim i As Integer Dim numberOfDoc As Integer Dim swDocs As Collection Sub main() Set swapp = Application.SldWorks openDocs = swapp.GetDocuments Set swDocs = New Collection numberOfDoc = swapp.GetDocumentCount For i = 0 To numberOfDoc - 1 Dim comp As swComponent Set comp = New swComponent Set swDoc = openDocs(i) comp.Initial swDoc swDocs.Add comp Debug.Print(comp.Name + " | " + comp.FileType + " | " + CStr(i + 1)) 'Add each open document to the listbox. UserForm1.ListBox1.AddItem comp.Name Next i UserForm1.Show vbModeless End Sub
In the main program, we added two new lines of code. The first is the “UserForm1.ListBox1.AddItem comp.Name” line in the “For” loop. This adds a new item for each open document to our ListBox1.
The other line is the “UserForm1.Show vbModeless”. This will activate and show the UserForm1 after the code finds all the open documents and added them to the ListBox1.
The UserForm.Show method has two options as documented here. These are vbModal and vbModeless.
In short, when vbModal is passed into the method, users will not be able to interact with SolidWorks when the UserForm is shown. Meanwhile, vbModeless allows users to continue using SolidWorks as usual while the UserForm is shown.
swComponent Class:
'Private members of class Private pName As String Private pFileType As Integer Private pDoc2 As ModelDoc2 'Methods to get the values from the private members Public Property Get Name() As String Name = pName End Property Public Property Get FileType() As String If pFileType = swDocumentTypes_e.swDocPART Then FileType = "Part File" ElseIf pFileType = swDocumentTypes_e.swDocASSEMBLY Then FileType = "Assembly File" End If End Property Public Property Get Doc2() As String Doc2 = pDoc2 End Property 'Sub to initialize the values for the members of this class. Call once after 'a new instance of the class is created Public Sub Initial(ModDoc2 As ModelDoc2) Set pDoc2 = ModDoc2 pName = pDoc2.GetTitle pFileType = pDoc2.GetType End Sub
Summary
In this lesson, we have created a simple macro with a GUI using VBA’s UserForm. UserForms opens up a realm of possibilities to create more user friendly macros and more powerful applications. We will explore further on the ways to use UserForms in the next lesson.