Sub OpenAllDocuments(byval SwAssy as assemblydoc)
dim Varcomp as variant
varcomp = swassy.getcomponents(true)
For i = lbound(varcomp) to ubound(varcomp)
Dim DocType as integer
Dim SwComp as component2
set swcomp = varcomp(i)
if not swcomp.getmodeldoc2 is nothing then 'Might be a virtual component or something if this is empty
DocType = swcomp.getmodeldoc2.gettype
'if the document is NOT visible, it is loaded in memory but you cant see that it is opened.
if swcomp.getmodeldoc2.visible = false then
Dim Errs as integer
dim warns as integer
Dim OpenedDoc as modeldoc2
set openedDoc = Swapp.OpenDoc6(Comp.GetPathName,doctype, swopendocoptions_e.swOpenDocOptions_LoadModel,swcomp.referencedConfiguration, errs,warns)
'HERE IS THE FUN PART
if Doctype = swdocumenttype_e.SwAssembly then
Dim SwSubAssy as assemblydoc
Set SwSubAssy = OpenedDoc
' DO NOT FEAR THE RECURSIVE FUNCTION
i havent tested it but it should work.
Thank you for a quick response, Jacob
How to declare the SwSubAssy?
IAssemblyDoc::GetComponents(bool TopLevel) would return an array of all components in the main assembly if you had TopLevel as false.
Why would you do a recursion ?
The GetModeDoc2 method will return a null if:
- a component is suppressed or lightweight.
- the component ID is not loaded into memory by SOLIDWORKS.
GetModelDoc2 should work as expected if the component is virtual
You can simply traverse the components array and based on extension from GetPathName() attempt to open the document. If OpenDoc6 returns the already open error, simply traverse all documents and make them visible.
Because recursion is funner. And I forgot about the setting that to true. Hehehe.
Why do you want to open them all? Are you looking to add/edit some property in them?