Solidworks VBA Macro - Insert Component

8 minute read

This article has "Beginner ➡ Intermediate" level Macro program.
If you are following my articles then will not be an issue for you.
In this article we did not use 🚀 UserForm for taking inputs, instead we use 🚀 Input Box for value input and 🚀 Message Box to notify user.
I hope you will also like this type of tutorials.
Thank you for reading.

Objective

In this article we create and understand VBA macro of Insert Component in Assembly document of SOLIDWORKS CAD Software.

This method is most updated method, so use this method if you want to Insert Component in Assembly document.

Results We Can Get

After running our macro we successfully Insert Component in Assembly document as a result.

Below image shows the result we get.

insert-component-final-result

We Insert Component in following steps in general.

  1. Insert an open Part in new Assembly document.

To get the correct result please follow the steps correctly.

Macro Video

Below 🎬 video shows Insert Component from SOLIDWORKS VBA Macros.


Above video is just for visualization and there are no explanation.

I have explained each and every line in this article.

It is advisable to watch video, since it help you to better understand the process.

VBA Macro

Below is the VBA macro for Insert Component.

Option Explicit

' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks

' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2

' Variable for Solidworks Assembly
Dim swAssembly As SldWorks.AssemblyDoc

' Variable for Solidworks Component
Dim swComponent As SldWorks.Component2

' Open part file name
Dim partName As String

' Program to insert a Component in assembly
Sub main()
  
  ' Set Solidworks Application variable to current application
  Set swApp = Application.SldWorks
  
  ' Set Solidworks document variable to currently opened document
  Set swDoc = swApp.ActiveDoc
  
  ' Check if Solidworks document is opened or not
  If swDoc Is Nothing Then
    MsgBox "Solidworks document is not opened."
    Exit Sub
  End If
  
  ' Get open part file name
  partName = swDoc.GetTitle
  
  ' Check if we get the file name or not
  If Len(partName) = 0 Then
    MsgBox "Fail to get Part title."
    Exit Sub
  End If
  
  ' Variable for storing default Assembly Template
  Dim defaultAssemblyTemplate As String
  
  ' Setting value of variable to "Default Assembly template"
  defaultAssemblyTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateAssembly)
  
  ' Check if we get the default Assembly template or not
  If Len(defaultAssemblyTemplate) = 0 Then
    MsgBox "Fail to get default Assembly template."
    Exit Sub
  End If
  
  ' Setting Solidworks document to new Assembly document
  Set swDoc = swApp.NewDocument(defaultAssemblyTemplate, 0, 0, 0)
  
  ' Check if Solidworks Assembly is opened or not
  If swDoc Is Nothing Then
    MsgBox "Solidworks Assembly is not opened."
    Exit Sub
  End If
  
  ' Set Solidworks Assembly document
  Set swAssembly = swDoc
  
  ' Insert new component
  Set swComponent = swAssembly.AddComponent5(partName, swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 0, 0, 0)
  
  ' Check new Component inserted or not
  If swComponent Is Nothing Then
    MsgBox "Failed to insert component in assembly."
    Exit Sub
  End If
  
  ' View zoom to fit
  swDoc.ViewZoomtofit2
  
End Sub

Prerequisite

There are some prerequisite for this article.

  • Knowledge of VBA programming language is ❗required.

Since we are creating new part, there are no feature to create.

We will apply checks in this article, so the code we write should be error free most of the time.

Steps To Follow

This Reference Point VBA macro can be divided into following sections:

  1. Create and Initialize required variables
  2. Insert Component
  3. Final work

Every section with each line is explained below.

I also give some links (see icon 🚀) so that you can go through them if there are anything I explained in previous articles.

Create and Initialize required variables

Option Explicit
' Variable for Solidworks application
Dim swApp As SldWorks.SldWorks
  • Purpose: In above line, we create a variable for Solidworks application.
  • Variable Name: swApp
  • Type: SldWorks.SldWorks
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
  • Purpose: In above line, we create a variable for Solidworks document.
  • Variable Name: swDoc
  • Type: SldWorks.ModelDoc2
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Variable for Solidworks Assembly
Dim swAssembly As SldWorks.AssemblyDoc
  • Purpose: In above line, we create a variable for Solidworks Assembly.
  • Variable Name: swAssembly
  • Type: SldWorks.AssemblyDoc
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Variable for Solidworks Component
Dim swComponent As SldWorks.Component2
  • Purpose: In above line, we create a variable for Solidworks Component.
  • Variable Name: swComponent
  • Type: SldWorks.Component2.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Open part file name
Dim partName As String
  • Purpose: In above line, we create a variable for Open part file name.
  • Variable Name: partName
  • Type: String

These all are our global variables.

They are SOLIDWORKS API Objects.

' Program to insert a Component in assembly
Sub main()

End Sub
  • In above line, we create main Program to insert a Component in assembly.
  • This is a Sub procedure which has name of main.
  • This procedure hold all the statements (instructions) we give to computer.
  • Reference: Detailed information 🚀 VBA Sub and Function Procedures article of this website.
' Set Solidworks Application variable to current application
Set swApp = Application.SldWorks
  • In above line, we set value of swApp variable.
  • This value is currently opened Solidworks application.
' Set Solidworks document variable to currently opened document
Set swDoc = swApp.ActiveDoc
  • In above line, we set value of swDoc variable.
  • This value is currently opened part document.
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
  MsgBox ("Solidworks document is not opened.")
  Exit Sub
End If
  • In above code block, we check if we successfully set the value of swDoc variable.
  • We use 🚀 IF statement for checking.
  • Condition: swDoc Is Nothing
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: SOLIDWORKS document is not opened.
    • Then we stop our macro here.
' Get open part file name
partName = swDoc.GetTitle
  • In above line, we set value of partName variable.
  • This value is Title or File Name of opened part.
' Check if we get the file name or not
If Len(partName) = 0 Then
  MsgBox "Fail to get Part title."
  Exit Sub
End If
  • In above code block, we check if we successfully set the value of partName variable.
  • We use 🚀 IF statement for checking.
  • Condition: Len(partName) = 0
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: Fail to get Part title.
    • Then we stop our macro here.
' Variable for storing default Assembly Template
Dim defaultAssemblyTemplate As String
  • Purpose: In above line, we create a variable for storing default Assembly Template path.
  • Variable Name: defaultAssemblyTemplate
  • Type: String
' Setting value of variable to "Default Assembly template"
defaultAssemblyTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateAssembly)
  • In above line, we set value of defaultAssemblyTemplate variable.
  • This value is set by GetUserPreferenceStringValue() method from swApp object.
' Check if we get the default Assembly template or not
If Len(defaultAssemblyTemplate) = 0 Then
  MsgBox "Fail to get default Assembly template."
  Exit Sub
End If
  • In above code block, we check if we successfully set the value of defaultAssemblyTemplate variable.
  • We use 🚀 IF statement for checking.
  • Condition: Len(defaultAssemblyTemplate) = 0
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: Fail to get default Assembly template.
    • Then we stop our macro here.
' Set Solidworks document variable to new Assembly document
Set swDoc = swApp.NewDocument(defaultAssemblyTemplate, 0, 0, 0)
  • In above line, we set value of swDoc variable.
  • This value is new Assembly document.
' Check if Solidworks Assembly is opened or not
If swDoc Is Nothing Then
  MsgBox ("Solidworks Assembly is not opened.")
  Exit Sub
End If
  • In above code block, we check if we successfully set the value of swDoc variable.
  • We use 🚀 IF statement for checking.
  • Condition: swDoc Is Nothing
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: SOLIDWORKS document is not opened.
    • Then we stop our macro here.

Insert Component

In this section, we Insert Component.

' Set Solidworks Assembly document
Set swAssembly = swDoc
  • In above line, we set value of swAssembly variable.
  • This value is swDoc variable.
' Insert new component
Set swComponent = swAssembly.AddComponent5(partName, swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 0, 0, 0)
  • In above line, we set the value of variable swComponent by AddComponent5 method.

  • This AddComponent5 method takes following parameters as explained:

    • CompName - Path name of a pre-loaded part or assembly to add as a component.

    • ConfigOption - Type of Scale as defined in swAddComponentConfigOptions_e:

      Member Description
      swAddComponentConfigOptions_CurrentSelectedConfig 2
      swAddComponentConfigOptions_NewConfigWithAllReferenceModels 3
      swAddComponentConfigOptions_NewConfigWithAsmStructure 4
    • NewConfigName - Name for the new assembly configuration.

    • UseConfigForPartReferences - If true, the configuration specified in ExistingConfigName is used.

    • ExistingConfigName - Name of the configuration of the loaded component.

    • X - X coordinate of the component center.

    • Y - Y coordinate of the component center.

    • Z - Z coordinate of the component center.

  • Return Value : This AddComponent5 method return 👉 Component2 data object.

  • In our code, I have used following values:

    Parameter Name Value Used
    CompName partName
    ConfigOption swAddComponentConfigOptions_CurrentSelectedConfig
    NewConfigName ""
    UseConfigForPartReferences False
    ExistingConfigName ""
    X 0
    Y 0
    Z 0

Reference: For more details about

' Check new Component inserted or not
If swComponent Is Nothing Then
  MsgBox "Failed to insert component in assembly."
  Exit Sub
End If
  • In above code block, we check if we successfully insert component in assembly or not.
  • We use 🚀 IF statement for checking.
  • Condition: swComponent Is Nothing
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: Failed to insert component in assembly.
    • Then we stop our macro here.

Now we run the macro and after running macro we Insert component as shown in below image.

assembly-insert-component-final-result

Final work

In this section, after Insert component, we have to do some cleaning work so that we can use this macro frequently.

' View zoom to fit
swDoc.ViewZoomtofit2
  • In above line, we make our view zoom to fit the model.
  • For this we use ViewZoomtofit2 method which is part of SOLIDWORKS Document variable i.e swDoc variable.

This is it !!!

I hope my efforts will helpful to someone!

If you found anything to add or update, please let me know on my e-mail.

Hope this post helps you to Insert component with SOLIDWORKS VBA Macros.

For more such tutorials on SOLIDWORKS VBA Macro, do come to this website after sometime.

If you like the post then please share it with your friends also.

Do let me know by you like this post or not!

Till then, Happy learning!!!

Updated: