Solidworks Macro - Open Assembly and Drawing document

3 minute read

In this post, we see how to open following documents with Solidworks VBA macro:

  1. Solidworks Assembly document

  2. Solidworks Drawing document

    • Without Pre-defined Sheet size

    • With Pre-defined Sheet size

    • With Custom Sheet size

Solidworks Assembly Document

The code for opening default Assembly document is identical to the default Part template with only one change.

First, let us see the code to open default Assembly document.

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 Assembly location
    Dim defaultTemplate As String
    ' Setting value of this string type variable to "Default Assembly template"
    defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateAssembly)

    ' Setting Solidworks document to new Assembly document
    Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)

End Sub

As you can see in the above code and the code is given in the previous post is almost identically.

In case you have not read my previous post (Solidworks Macros - Open new Part document), I recommend you to read that post first.

I have already explained each and every line in this code there. So I will not explain them in this post.

To open default assembly template, you just need to change defaultTemplate variable and use swDefaultTemplateAssembly in place of swDefaultTemplatePart.

With this you can open a new assembly document.


Solidworks Drawing Document without Defining Paper size

To open new Default drawing document we use same code as used above but with slight modification.

If we make similar change as we have done for Assembly document then we open Default drawing document without specifying sheet size.

The code sample shows how to do it.

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 drawing location
    Dim defaultTemplate As String
    ' Setting value of this string type variable to "Default drawing template" without define paper size
    defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateDrawing)

    ' Setting Solidworks document to new drawing document
    Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)

End Sub

Solidworks Drawing Document with Default Paper size

To open a new Drawing with pre-define sheet size we use following code sample:

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 drawing location
    Dim defaultTemplate As String
    ' Setting value of this string type variable to "Default drawing template" with pre-define paper size
    defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateDrawing)

    ' Setting Solidworks document to new drawing document
    Set swDoc = swApp.NewDocument(defaultTemplate, swDwgPaperSizes_e.swDwgPaperA4sizeVertical, 0, 0)

End Sub

This code is similar to what we have used in the previous section but has a one modification.

That is while setting the document (at Set Doc) we define Paper size or Sheet size.

I used A4 Sheet with vertical orientation by using swDwgPaperSizes_e.swDwgPaperA4sizeVertical enumarator.

You can use other values from this list.


Solidworks Drawing Document with Custom Paper size

To open a new Drawing with Custom sheet size we use following code sample:

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 drawing location
    Dim defaultTemplate As String
    ' Setting value of this string type variable to "Default drawing template" with custom paper size
    defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateDrawing)

    ' Setting Solidworks document to new drawing document
    Set swDoc = swApp.NewDocument(defaultTemplate, swDwgPaperSizes_e.swDwgPapersUserDefined, 2, 3)

End Sub

For custom paper size, we need to use swDwgPaperSizes_e.swDwgPapersUserDefined value of paper size.

Since we are using custom value, we need to define paper width and paper height also.

It important to remember that API use Metric system only. So you need to use the converted value in defining paper width and paper height.

This is all for now. In the next post I will tell you how select a plane in a part document and if possible how to create a skecth segment.

Updated: