Solidworks VBA Macro - Loop All Sheets & Views in Drawing
Objective
In this article, we understand “how to” Loop All Sheets & Views in Drawing in Drawing document from VBA macro.
This is most updated method of Loop All Sheets & Views in Drawing in an drawing document.
Results We Can Get
Below image shows the result we get.
We Loop All Sheets & Views in Drawing 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 Loop All Sheets & Views in Drawing 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 Loop All Sheets & Views in Drawing.
Option Explicit
' Creating variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Creating variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Creating variable for Solidworks Drawing
Dim swDrawing As SldWorks.DrawingDoc
' Creating variable for Solidworks View
Dim swView As SldWorks.View
' Creating variable for Solidworks Sheet
Dim swSheet As SldWorks.Sheet
' Program to Loop All Sheets & Views in Drawing
Sub main()
' Setting Solidworks variable to Solidworks 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 Solidworks Drawing document variable
Set swDrawing = swDoc
' Variable for Sheet names
Dim vSheetName As Variant
' Get the sheets in the drawing document
vSheetName = swDrawing.GetSheetNames
' Variable for Sheet Index
Dim sheetIndex As Integer
' Loop through sheet names in drawing
For sheetIndex = 0 To UBound(vSheetName)
' Set Solidworks Sheet variable
Set swSheet = swDrawing.Sheet(vSheetName(sheetIndex))
' Check if we failed to get sheet
If swSheet Is Nothing Then
MsgBox "Failed to get drawing sheet."
Exit Sub
End If
' Print current sheet name
Debug.Print "Active Sheet Name: " & vSheetName(sheetIndex)
' Variable for drawing views
Dim views As Variant
' Get all views in this sheet
views = swSheet.GetViews
' Variable for drawing view
Dim vView As Variant
' Loop all drawing views
For Each vView In views
' Set Solidworks view variable
Set swView = vView
' Check if we get the view
If swView Is Nothing Then
MsgBox "Failed to get current view."
Exit Sub
End If
' Print View's Name
Debug.Print "View Name: " & swView.Name
Next vView
Next sheetIndex
End Sub
Prerequisite
There are some prerequisites for this article.
- Knowledge of VBA programming language is ❗required.
- There are 2 sheets in drawing.
- Each sheet has some views.
- We did not select anything in the drawing.
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:
- Create Global Variables
- Initialize Variables
- Loop Through Sheets
- Loop Through Views
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
- Purpose: Above line forces us to define every variable we are going to use.
- Reference: 🚀 SOLIDWORKS Macros - Open new Part document article.
' 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.
' Creating variable for Solidworks Drawing
Dim swDrawing As SldWorks.DrawingDoc
- Purpose: In above line, we create a variable for Solidworks Drawing.
- Variable Name:
swDrawing - Type:
SldWorks.DrawingDoc - Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Creating variable for Solidworks View
Dim swView As SldWorks.View
- Purpose: In above line, we create a variable for Solidworks Drawing.
- Variable Name:
swView - Type:
SldWorks.View - Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Creating variable for Solidworks Sheet
Dim swSheet As SldWorks.Sheet
- Purpose: In above line, we create a variable for Solidworks Drawing.
- Variable Name:
swSheet - Type:
SldWorks.Sheet - Reference: Please visit 🚀 online SOLIDWORKS API Help.
These all are our global variables.
They are SOLIDWORKS API Objects.
' Program to Loop All Sheets & Views in Drawing
Sub main()
End Sub
- In above line, we create Program to Loop All Sheets & Views in Drawing.
- This is a
Subprocedure which has name ofmain. - This procedure hold all the statements (instructions) we give to computer.
- Reference: Detailed information 🚀 VBA Sub and Function Procedures article of this website.
Initialize Variables
In this section, we initialize Variables.
' Set Solidworks Application variable to current application
Set swApp = Application.SldWorks
- In above line, we set value of
swAppvariable. - 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
swDocvariable. - 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
swDocvariable. - 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.
' Set Solidworks Drawing document
Set swDrawing = swDoc
- In above line, we set value of
swDrawingvariable. - This value is
swDocvariable.
Loop Through Sheets
In this section, we Loop Through Sheets.
' Variable for Sheet names
Dim vSheetName As Variant
- Purpose: In above line, we create a
Variantvariable for Sheet names. - Variable Name:
vSheetName - Type:
Variant
' Get the sheets in the drawing document
vSheetName = swDrawing.GetSheetNames
- In above code, we Get the sheets in the drawing document.
- For this, we use
GetSheetNamesmethod. - This
GetSheetNamesmethod is part ofswDrawingvariable. -
This method did not takes any parameters.
- Return Value : This
GetSheetNamesmethod return Sheet names in the drawing.
' Variable for Sheet Index
Dim sheetIndex As Integer
- Purpose: In above line, we create a
Variantvariable for Sheet Index. - Variable Name:
sheetIndex - Type:
Variant
' Loop through sheet names in drawing
For sheetIndex = 0 To UBound(vSheetName)
Next sheetIndex
- In above code block, we create
Forloop. - We loop through sheet names in drawing.
- Currently there are 2 sheets i.e.
Sheet1andSheet2. - Hence
sheetIndexrange from0to1. - For more details: 🚀 VBA Looping
' Set Solidworks Sheet variable
Set swSheet = swDrawing.Sheet(vSheetName(sheetIndex))
- In above line, we set Solidworks Sheet variable.
- We are doing following:
- Get current sheet name.
Sheet1:vSheetName(sheetIndex)[When sheetIndex = 0]Sheet2:vSheetName(sheetIndex)[When sheetIndex = 1]
- Get drawing sheet obect.
- Method Used:
Sheet() - Method Parameter: Sheet Name
- Return Value: Solidwork Sheet Object
- Method Used:
- Set Solidworks Sheet object by
Sheet()method.
- Get current sheet name.
' Check if we failed to get sheet
If swSheet Is Nothing Then
MsgBox "Failed to get drawing sheet."
Exit Sub
End If
- In above code block, we check if we successfully set the value of
swSheetvariable. - We use 🚀 IF statement for checking.
- Condition:
swSheet Is Nothing - When this condition is
True,- We show and 🚀 message window to user.
- Message: Failed to get drawing sheet.
- Then we stop our macro here.
' Print current sheet name
Debug.Print "Active Sheet Name: " & vSheetName(sheetIndex)
- In above line, we Print Active Sheet Name in Immediate Window.
- We get Sheet Name by
vSheetName(sheetIndex)method. - This
vSheetNamemethod takesheetIndexparameter. - If Immediate Window is not available then press “
Ctrl + G”.
Loop Through Views
In this section, we Loop Through Views.
' Variable for drawing views
Dim views As Variant
- Purpose: In above line, we create a
Variantvariable for drawing views. - Variable Name:
views - Type:
Variant
' Get all views in this sheet
views = swSheet.GetViews
- In above code, we Get all views in this sheet.
- For this, we use
GetViewsmethod. - This
GetViewsmethod is part ofswSheetvariable. -
This method did not takes any parameters.
- Return Value : This
GetViewsmethod return all views in this sheet.
' Variable for drawing view
Dim vView As Variant
- Purpose: In above line, we create a
VariantVariable for drawing view. - Variable Name:
vView - Type:
Variant
' Loop all drawing views
For Each vView In views
Next vView
- In above code block, we create
For Eachloop. - We loop through drawing views in current sheet.
- For more details: 🚀 VBA Looping
' Set Solidworks view variable
Set swView = vView
- In above line, we set Solidworks view variable.
- We set value to
vView.
' Check if we get the view
If swView Is Nothing Then
MsgBox "Failed to get current view."
Exit Sub
End If
- In above code block, we check if we successfully set the value of
swViewvariable. - We use 🚀 IF statement for checking.
- Condition:
swView Is Nothing - When this condition is
True,- We show and 🚀 message window to user.
- Message: Failed to get current view.
- Then we stop our macro here.
' Print View's Name
Debug.Print "View Name: " & swView.Name
- In above line, we Print View’s Name in Immediate Window.
- We get Sheet Name by
swView.Nameproperty. - If Immediate Window is not available then press “
Ctrl + G”.
Now we run the macro and after running macro we show selected component as shown in below image.
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 Loop All Sheets & Views in Drawing 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!!!

