ScottWisker
Mechanical
- Jan 31, 2007
- 15
Does anyone know of code that will list the components within an assembly without having to open the file?
Thanks
Thanks
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Const DOCMGRLICENSEKEYSTRING As String = "Replace this string with your key for SW<'07"
Const PATHNAME As String = "Replace this with your file path"
Sub main()
Dim myDocMgrClassFactory As SwDocumentMgr.SwDMClassFactory
Dim myDocMgr As SwDocumentMgr.SwDMApplication
Dim mySwDoc As SwDocumentMgr.SwDMDocument7
Dim extRefs As Variant
Dim i As Long
Dim SrchOptions As SwDocumentMgr.SwDMSearchOption
Dim RefString As String
Dim MsgString As String
Set myDocMgrClassFactory = CreateObject("SwDocumentMgr.SwDMClassFactory")
On Error Resume Next
Set myDocMgr = myDocMgrClassFactory.GetApplication(DOCMGRLICENSEKEYSTRING)
On Error GoTo 0
Set mySwDoc = myDocMgr.GetDocument(PATHNAME, swDmDocumentUnknown, True, Empty)
Set SrchOptions = myDocMgr.GetSearchOptionObject
extRefs = mySwDoc.GetAllExternalReferences(SrchOptions)
RefString = (UBound(extRefs) + 1) & " External References Found:" & vbCrLf
For i = 0 To UBound(extRefs)
RefString = RefString & vbCrLf & extRefs(i)
Next i
If Len(RefString) > 1024 Then
MsgBox Left(RefString, 950) & vbCrLf & vbCrLf & "Number of characters exceeds MsgBox capacity"
Else
MsgBox RefString
End If
End Sub
Const DOCMGRLICENSEKEYSTRING As String = "Replace this string with your key for SW<'07"
Const PATHNAME As String = "Replace this with your file path"
Sub main()
Dim myDocMgrClassFactory As SwDocumentMgr.SwDMClassFactory
Dim swDocMgr As SwDocumentMgr.SwDMApplication
Dim myDoc As SwDocumentMgr.SwDMDocument7
Dim ParentDoc As SwDocumentMgr.SwDMDocument7
Dim i As Long
Dim RefString As String
Dim MsgString As String
Dim myCollection As Collection
Dim eachRef As Variant
Dim ParentString As String
Set myCollection = New Collection
Set myDocMgrClassFactory = CreateObject("SwDocumentMgr.SwDMClassFactory")
On Error Resume Next
Set swDocMgr = myDocMgrClassFactory.GetApplication(DOCMGRLICENSEKEYSTRING)
On Error GoTo 0
Set myDoc = swDocMgr.GetDocument(PATHNAME, swDmDocumentUnknown, True, Empty)
If Nothing Is myDoc Then
myCollection.Add PATHNAME & " [NOT FOUND]"
Else
myCollection.Add myDoc.FullName
ParentString = myDoc.FullName
GetRefs myCollection, myDoc, swDocMgr, ">", ParentString
End If
''''''''''''''''''''''''
'At this point, myCollection is essentially a 1-dimensional array containing
'structured reference data. For large assemblies this data will not fit
'in the immediate window.
''''''''''''''''''''''''
For Each eachRef In myCollection
Debug.Print eachRef
Next eachRef
End Sub
Sub GetRefs(ByRef AddTo As Collection, ByVal ParentDoc As SwDocumentMgr.SwDMDocument7, ByRef myDocMgr As SwDocumentMgr.SwDMApplication, ByVal NumIndent As String, ByVal Parents As String)
Dim mySwDoc As SwDocumentMgr.SwDMDocument7
Dim SrchOptions As SwDocumentMgr.SwDMSearchOption
Dim extRefs As Variant
Dim i As Long
Set SrchOptions = myDocMgr.GetSearchOptionObject
extRefs = ParentDoc.GetAllExternalReferences(SrchOptions)
If IsEmpty(extRefs) Then
'do nothing
Else
For i = 0 To UBound(extRefs)
Set mySwDoc = myDocMgr.GetDocument(extRefs(i), swDmDocumentUnknown, True, Empty)
If Nothing Is mySwDoc Then
AddTo.Add Left(NumIndent, Len(NumIndent) - 1) & "x" & extRefs(i) & " [NOT FOUND]"
ElseIf InStr(1, Parents, mySwDoc.FullName, vbTextCompare) > 0 Then
'Don't add - circular.
Else
AddTo.Add NumIndent & mySwDoc.FullName
GetRefs AddTo, mySwDoc, myDocMgr, "-" & NumIndent, mySwDoc.FullName & Parents
End If
Next i
End If
End Sub