Solidworks VBA Macro - Insert Folder

6 minute read

Objective

In this article, we understand โ€œhow toโ€ Insert Folder in Assembly document from VBA macro.

We will insert an empty folder in Feature Tree.

This is most updated method of Insert Folder in an assembly document.

Results We Can Get

Below image shows the result we get.

assembly-insert-folder

We Insert Folder in simple manners.

There are no extra steps required.

To get the correct result, please follow the steps correctly.

Macro Video

Below ๐ŸŽฌ video shows how to Insert Folder from SOLIDWORKS VBA Macros.


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

I have explained every line in this article.

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

VBA Macro

Below is the VBA macro for Insert Folder.

Option Explicit

' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks

' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2

' Variable for Solidworks Feature Manager
Dim swFeatureManager As SldWorks.FeatureManager

' Variable for Solidworks Feature
Dim feature As SldWorks.feature

' Program to Add New Folder
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
  
  ' Set Feature manager
  Set swFeatureManager = swDoc.FeatureManager

  ' Insert Folder
  Set feature = swFeatureManager.InsertFeatureTreeFolder2(swFeatureTreeFolder_EmptyBefore)

  ' Check if Folder is added or not
  If feature Is Nothing Then
    MsgBox "Failed to Insert Folder."
    Exit Sub
  End If

  ' Variable to store user response
  Dim response As String
  response = InputBox("Folder Name:", "Folder")
  
  ' This will handle empty value or cancel case
  If Len(response) = 0 Then
    MsgBox "Empty or no value. Please try again."
    Exit Sub
  End If

  ' Update Folder Name
  feature.Name = response
  
End Sub

Prerequisite

There are some prerequisites for this article.

  • Knowledge of VBA programming language is โ—required.
  • We use existing parts in Assembly document.
  • Both components are fully constraint as shown in below image.
  • For inserting folder atleast one part must be selected. Otherwise this method will not work.

prerequisite

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

Steps To Follow

This VBA macro can be divided into following sections:

  1. Create Global Variables
  2. Initialize Global Variables
  3. Insert Folder

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 Global Variables

In this section, we create global 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 Feature Manager
Dim swFeatureManager As SldWorks.FeatureManager
  • Purpose: In above line, we create a variable for Solidworks Feature Manager.
  • Variable Name: swFeatureManager
  • Type: SldWorks.FeatureManager
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Variable for Solidworks Feature
Dim feature As SldWorks.feature
  • Purpose: In above line, we create a variable for Solidworks Feature.
  • Variable Name: feature
  • Type: SldWorks.feature.
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.

These all are our global variables.

They are SOLIDWORKS API Objects.

' Program to Add New Folder
Sub main()

End Sub
  • In above line, we create Program to Add New Folder.
  • 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.

Initialize Global Variables

In this section, we initialize global variables.

' 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.

Insert Folder

In this section, we perform UnSuppress Component action.

' Set Feature manager
Set swFeatureManager = swDoc.FeatureManager
  • In above line, we set value of swFeatureManager variable.
  • We set value to FeatureManager property of swDoc variable.
' Insert Folder
Set feature = swFeatureManager.InsertFeatureTreeFolder2(swFeatureTreeFolder_EmptyBefore)
  • In above line, we โ€œInsert Folderโ€ by setting the value of feature variable.
  • This InsertFeatureTreeFolder2() method is part of swFeatureManager variable.
  • This InsertFeatureTreeFolder2() method takes following parameter.
    • Type: Type of folder as defined in swFeatureTreeFolderType_e in below table.

      Member Description
      swFeatureTreeFolder_Containing 2 = Create and insert a folder to contain the pre-selected features
      swFeatureTreeFolder_EmptyBefore 1 = Create and insert an empty folder before the pre-selected features
      swFeatureTreeFolder_Mold 3
  • Return Value : This InsertFeatureTreeFolder2() method return ๐Ÿš€ feature data object.

  • In our code, I have used following values:

    Parameter Name Value Used
    Type swFeatureTreeFolder_EmptyBefore

Reference: For more details about

' Check if Folder is added or not
If feature Is Nothing Then
  MsgBox "Failed to Insert Folder."
  Exit Sub
End If
  • In above code block, we check if we successfully added Insert Folder or not.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: feature Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Failed to Insert Folder.
    • Then we stop our macro here.
' Variable to hold user input
Dim response As String
  • In above line, we create a variable as a counter.
  • Variable Name: response
  • Type: String
' Getting Folder Name from user.
response = InputBox("Folder Name:", "Folder")
  • In above line of code we are doing 2 steps in one line.

    Those 2 steps are explained below.

    • Step 1 - Getting Folder Name from user.

    Below image shows the message for Folder Name to the user.

    message-to-folder-name

    • Step 2 - Assigned input value to response variable.
' This will handle empty value or cancel case
If Len(response) = 0 Then
  MsgBox "Empty or no value. Please try again."
  Exit Sub
End If
  • In above code block, we check the length of input value.
  • This check will handle case for empty value or cancel operation case.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: Len(response) = 0
    • Len() is pre-build VBA function which check the length of a object.
    • In above cases, we will get 0 value.
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Empty or no value. Please try again.
    • Then we stop our macro here.
' Update Folder Name
feature.Name = response
  • In above line, we set value of Name property of feature variable.
  • We set value to response, which we asked from user.

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

assembly-insert-folder

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 Folder 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: