Get Names of Open Documents Example (VBA)
This example shows how to get the name of the documents open in a SOLIDWORKS
session.
'-------------------------------------
'
' Preconditions: SOLIDWORKS application is running.
'
' Postconditions: None
'
'-------------------------------------
Option Explicit
Public Enum swDocumentTypes_e
swDocNONE
= 0 '
Used to be TYPE_NONE
swDocPART
= 1 '
Used to be TYPE_PART
swDocASSEMBLY
= 2 '
Used to be TYPE_ASSEMBLY
swDocDRAWING
= 3 '
Used to be TYPE_DRAWING
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swModelTitle As
SldWorks.ModelDoc2
Dim
swModelPath As
SldWorks.ModelDoc2
Dim
cTitle As
New Collection
Dim
vTitle As
Variant
Dim
cPath As
New Collection
Dim
vPath As
Variant
Set
swApp = Application.SldWorks
Set
swModel = swApp.GetFirstDocument
While
Not swModel Is Nothing
'
Assembly components are opened, but are not visible
'
until opened by the user
Debug.Print
swModel.GetPathName & "
["
& swModel.Visible & "]"
Debug.Print
" "
& swModel.GetTitle & "
[" & swModel.GetType
& "]"
'
The document name contains a filename extension
'
if the document has been already saved
'
and is subject to Microsoft Explorer setting;
'
the document name does not contain a
'
filename extension if the document has not been saved;
'
ModelDoc2::GetPathName is blank until the file is saved
cTitle.Add
swModel.GetTitle
cPath.Add
swModel.GetPathName
Set
swModel = swModel.GetNext
Wend
Debug.Print
"--------------------------"
For
Each vTitle In cTitle
Set
swModelTitle = swApp.GetOpenDocument(vTitle):
Debug.Assert Not swModelTitle Is Nothing
Debug.Print
swModelTitle.GetPathName &
" ["
& swModelTitle.Visible &
"]"
Debug.Print
" "
& swModelTitle.GetTitle &
" [" & swModelTitle.GetType
& "]"
Next
vTitle
Debug.Print
"--------------------------"
For
Each vPath In cPath
Set
swModelPath = swApp.GetOpenDocument(vPath)
If
Not swModelPath Is Nothing Then
Debug.Print
swModelPath.GetPathName &
" ["
& swModelPath.Visible &
"]"
Debug.Print
" "
& swModelPath.GetTitle &
" [" & swModelPath.GetType
& "]"
End
If
Next
vPath
Debug.Print
"--------------------------"
End Sub
'-------------------------------------