Solidworks Macro - Open Saved Documents
In this post, I tell you how to open a saved document in Solidworks using VBA Macro.
We open document with 2 different methods.
-
By
OpenDoc
method -
By
OpenDoc6
method
Video of Code on YouTube
Please see below video on how to Open Saved Documents from Visual Studio.
Please note that there are no explaination in the video.
Explaination of each line and why we write code this way is given in this post.
By OpenDoc method
This is the simplest method to open a saved part from your computer.
In this method we just need two information.
-
Location of document to open
-
Type of document which we want to open
Below is the example code for opening a saved document using OpenDoc
method.
Option Explicit
' Creating variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Creating variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
' Main function of our VBA program
Sub main()
' Setting Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Open a saved document
Set swDoc = swApp.OpenDoc("H:\Solidworks studies\API Studies\Chapter 1 - The Basics\1st example part.SLDPRT", swDocumentTypes_e.swDocPART)
' Selecting Front Plane
BoolStatus = swDoc.SelectByID("Front Plane", "PLANE", 0, 0, 0)
End Sub
I have already explained every line except middle line in above code sample in previous posts.
To open a saved document we used following lline.
' Open a saved document
Set swDoc = swApp.OpenDoc("H:\Solidworks studies\API Studies\Chapter 1 - The Basics\1st example part.SLDPRT", swDocumentTypes_e.swDocPART)
Here, we set the ModelDoc2
variable
swDoc
to a value.
This value is return or provided by OpenDoc
method.
This method is part of Solidworks document.
Since we define swApp
variable as
Solidworks document hence we 1st call swApp
and then using Dot operator
we access the OpenDoc
method.
OpenDoc
method takes 2
arguments or parameter.
FileName : Document name or full path if not in current directory, including extension.
Type : Document type as define in swDocumentTypes_e
as follows.
-
swDocASSEMBLY
-
swDocDRAWING
-
swDocLAYOUT
-
swDocNONE
-
swDocPART
-
swDocSDM
If you want to open a Library feature part then we use
swDocPART
as document type.
Return Value - If the document opens then this method returns True
and otherwise False
.
If you just want to open a saved document then this method is what you are looking for.
For most of the part, OpenDoc
method
works well.
If you want more option while opening a document, then next method is for you.
By OpenDoc6 method
OpenDoc6
method is extension to OpenDoc
with some additional parameters.
How OpenDoc6
works is shown in below
code sample:
Option Explicit
' Creating variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Creating variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
' Main function of our VBA program
Sub main()
' Setting Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Open an saved document
Set swDoc = swApp.OpenDoc6("H:\Solidworks studies\API Studies\Chapter 1 - The Basics\1st example part.SLDPRT", swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)
' Selecting Front Plane
BoolStatus = swDoc.SelectByID("Front Plane", "PLANE", 0, 0, 0)
End Sub
This code sample is similar is to previous example code except for OpenDoc6
method takes extra 4 parameters.
OpenDoc6
method takes 6
arguments or parameter.
FileName : Document name or full path if not in current directory, including extension.
Type : Document type as define in swDocumentTypes_e
as follows.
-
swDocASSEMBLY
-
swDocDRAWING
-
swDocLAYOUT
-
swDocNONE
-
swDocPART
-
swDocSDM
If you want to open a Library feature part then we use
swDocPART
as document type.
Options : Mode in which to open the document as defined in swOpenDocOptions_e
.
For more details about Options parameters, please visit this page of Solidworks API Help.
Configuration : Configuration in which you want to open this document.
-
Applies to Part and Assemblies, not drawings.
-
If this argument is empty or the specified configuration is not present in the model, the model is opened in the last-used configuration.
I used an ""
in the above code sample,
because I want to open part file in last saved configuration.
If you don’t know about ""
, then this
symbol represent an empty string.
When we don’t want to pass any value as string
, at that time I use ""
.
You can also use ""
when you want to
pass an empty string in VBA.
Errors : Load errors as defined in swFileLoadError_e
.
For more details about Errors parameters, please visit this page of Solidworks API Help.
Since this parameter is long
type, and
I don’t want to enter any value from the provided list; I used 0 as value.
If you want to use options from option link then you can use values from there.
It is just I don’t want to load any error information about the part.
Warnings : Warnings or extra information generated during the open operation as defined
in swFileLoadWarning_e
.
For more details about Warnings parameters, please visit this page of Solidworks API Help.
As in the previous parameter, I use 0 as value.
Return Value - If the document opens then this method returns True
and otherwise False
.
As you can see, in OpenDoc6
method, we
need to defined the extra parameters compared to OpenDoc
method.
It is worth noted that, OpenDoc6
method
is the most updated method for opening a saved document.
Hence if did not use any of the above method, I would recommend you to use OpenDoc6
method.
Hope this post helps you to understand opening methods with Solidworks VB Macros.
For more such tutorials on Solidworks VBA Macros, do come to this blog after sometime.
Till then, Happy learning!!!