Hide Table of Contents

Select All in Part, Assembly, or Drawing (VBA)

This example shows how to select everything in the graphics area of a part or assembly document or in the sheet of a drawing document, as if you box-selected everything in the graphics area or the sheet.

'--------------------------------------------------------------------------
' Preconditions:
' 1. Verify that the part, assembly, and drawing documents opened by the macro
'    exist.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Examine:
'    * Sheet to verify that all of the entities in the drawing
'      are selected.
'    * Immediate window to see how many entities are selected.
' 2. Click Window > bolt-assembly.sldasm to switch to the assembly
'    document.
' 3. Examine:
'    * Graphics area to verify that the all of the components
'      in the assembly are selected.
'    * Immediate window to see how many components are selected.
' 4. Click Window > bolt.sldprt to switch to the part document.
' 5. Examine:
'    * Graphics area to verify that the all of the edges
'      in the part are selected.
'    * Immediate window to see how many edges are selected.
'
' NOTE: Because these documents are used elsewhere, do not save changes.
'--------------------------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swSelMgr As SldWorks.SelectionMgr
Dim partFile As String
Dim assemblyFile As String
Dim drawingFile As String
Dim errors As Long
Dim warnings As Long
Dim selCount As Integer
Sub main()
    Set swApp = Application.SldWorks
    
    ' Open a part document and select all edges in the part
    partFile = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2017\introsw\bolt.sldprt"
    Set swModel = swApp.OpenDoc6(partFile, swDocPART, swOpenDocOptions_Silent, "", errors, warnings)
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    'Select all edges in part
    SelectAllinDocument
    
    ' Open an assembly document and select all components in the assembly
    assemblyFile = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2017\introsw\bolt-assembly.sldasm"
    Set swModel = swApp.OpenDoc6(assemblyFile, swDocASSEMBLY, swOpenDocOptions_Silent, "", errors, warnings)
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    'Select all components in assembly
    SelectAllinDocument
    
    ' Open a drawing document and select all entities in the drawing
    drawingFile = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2017\introsw\bolt-assembly.slddrw"
    Set swModel = swApp.OpenDoc6(drawingFile, swDocDRAWING, swOpenDocOptions_Silent, "", errors, warnings)
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    'Select all entities in drawing
    SelectAllinDocument
    
End Sub
Sub SelectAllinDocument()
    ' Select all edges in a part, all components in an assembly,
    ' or all entities in a drawing
     swModelDocExt.SelectAll    
    ' Get and print the number of selections
    selCount = 0
    selCount = swSelMgr.GetSelectedObjectCount2(-1)    
    Select Case swModel.GetType
    Case swDocPART
        Debug.Print "Number of edges selected in part          = " & selCount
    Case swDocASSEMBLY
        Debug.Print "Number of components selected in assembly = " & selCount
    Case swDocDRAWING
        Debug.Print "Number of entities selected in drawing    = " & selCount
    Case Else
        Debug.Print "Unknown type of document."
    End Select
    
End Sub

 



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Select All in Part, Assembly, or Drawing (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2018 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.