As the title suggested, in this post, we open new document from Solidworks VBA macros.
Also we understand each and every line of written code. So that you can understand why we written those lines and get some knowledge about how to write macro properly.
Please see below video for visual details.
To do this, we first need to create a new empty macro. If you don’t know how to create an empty macro; then please go to this VBA in Solidworks post.
After creating an empty macro, you need to copy paste below code into this empty macro.
Option Explicit ' Creating variable for Solidworks application Dim swApp As SldWorks.SldWorks ' Creating variable for Solidworks document Dim swDoc As SldWorks.ModelDoc2 ' Main function of our VBA program Sub main() ' Setting Solidworks variable to Solidworks application Set swApp = Application.SldWorks ' Creating string type variable for storing default part location Dim defaultTemplate As String ' Setting value of this string type variable to "Default part template" defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart) ' Setting Solidworks document to new part document Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0) End Sub
This code opens a new default part template in Solidworks.
Now let us walk through each line in the above code, and understand the meaning of every line.
This line forces us to define every variable we are going to use.
This is very important because if you don’t declare above line, it is very difficult to caught typo errors in variable names.
This type of error comes, when you mistakenly type wrong spelling of your defined variable.
In this case, VBE thinks that you have defined a new variable and use this variable.
This causes issues because your program runs perfectly but you didn’t get the desired result.
This most of the time discourage people and ultimately they left the programming.
So be on safe side and use this Option Explicit line in your every module.
' Creating variable for Solidworks application Dim swApp As SldWorks.SldWorks ' Creating variable for Solidworks document Dim swDoc As SldWorks.ModelDoc2
As the comments in above code sample shows, in these 2 lines we are creating variables of different type.
' Creating variable for Solidworks application Dim swApp As SldWorks.SldWorks
In this line, we are creating a variable which we named as
swApp and the type of this
swApp variable is
If we omit 1
SldWorks, then our VBE show error if we try to run this macro.
' Creating variable for Solidworks document Dim swDoc As SldWorks.ModelDoc2
In this line, we are creating a variable which we named as
swDoc and the type of this
swDoc variable is
Here, if we omit
SldWorks, the compiler won’t give error. I used it to know who is the parent object.
ModelDoc2 is the a object, which holds properties and methods related to this a document.
These methods and properties are common to part, assembly and drawing documents.
You can see more about
ModelDoc2 in this help link.
Next is our
Sub procedure named
main. This procedure hold all the statements (instructions) we give to computer.
' Setting Solidworks variable to Solidworks application Set swApp = Application.SldWorks
In this line, we are setting the value of our Solidworks variable which we define earlier to Solidworks application.
Now it important to know that when you defined a variable of different type, which is not a common type, then you need to set the variable also.
This is a standard way to set Solidworks application. This way is given in many Solidworks API VBA example.
' Creating string type variable for storing default part location Dim defaultTemplate As String ' Setting value of this string type variable to "Default part template" defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
In 1st statement of above example, we are defining a variable of
string type and named it as
defaultTemplate, hold the location the location of Default Part Template.
In 2nd line of above example. we assign value to our newly define
We assign the value by using a Method named
GetUserPreferenceStringValue(). This method is a part of our main Solidworks variable
To access this method, we need to write
swApp variable and then use a “.” to access the Public properties and Methods inside this variable.
This “.” is called Dot operator. This operator provides us the access to the Public properties and Methods inside an object.
When you type after a “.” you will notice that Visual Basic Editor automatically provides a list of properties and methods inside this
swApp object. This helps us to write correct name for these methods and properties.
Now we get the function
GetUserPreferenceStringValue(). But this function needs some input to work with. This inputs are generally called Parameters.
In programming voculabury, we need to pass the parameter to this function so that this function can worked.
This input parameter is a single value from a list of other values. This list is stored in
In Solidworks API, if anything has
_e after it, it means that this object contains some type of lists. It is important to know because we frequently use these type of lists. They are called enum. The value they hold is called Constant.
So our function
GetUserPreferenceStringValue() needs some constant value from
swUserPreferenceStringValue_e enum list to work.
Since we want Default part template, we use
swDefaultTemplatePart constant value from the
swUserPreferenceStringValue_e enum list.
Please note that there are lots of values inside this enum list. You can see these values from this link].
Now we just need to set the value of our
swDoc variable to new document. We set the value as shown in below code snippet.
' Setting Solidworks document to new part document Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)
To set the value of
swDoc, we use
NewDocument() method. This method is inside
swApp, hence we first need to invoke
swApp and then by using Dot operator we access the
Now this method needs 4 parameters (or input values) to work. If we don’t provide any of these required value we get errors.
These 4 parameters are as follows:
- This can be a full path of the template, which we use to create New document.
- Size of paper
- Width of paper
- Height of paper
When successfully implemented, this method creates a new document.
If this method failes to create a new document in that case this return
NULL value. We can use this
NULL value to check if the operation is successfull or not.
In our example, we use
defaultTemplate variable as TemplateName parameter and use 0 in all other 3 parameter.
Please note that PaperSize, Width and Height is used only if we want to create a new Drawing document.
This is all for now. This post is getting too long.
In next post I will tell you how to create a new Assembly document and new Drawing document.