1. Declare swConfigMgr object (Dim swConfigMgr As SldWorks.ConfigurationManager).
2. Add following lines after 'Set swModel = swView.ReferencedDocument':
Set swConfigMgr = swModel.ConfigurationManager
Set swConfig = swConfigMgr.ActiveConfiguration
If swConfig.Name <> swView.ReferencedConfiguration Then
If (swModel.ShowConfiguration2(swView.ReferencedConfiguration)) = False Then
MsgBox "Error: Failed to activate configuration '" & swView.ReferencedConfiguration & "' in document '" & swModel.GetTitle & "'."
If swModel.EditRebuild3 = False Then
MsgBox "Error: Failed to rebuilt document '" & swModel.GetTitle & "'."
I know this is old, but I'm working on something similar.
The problem with Deepak Gupta's macro in this case is that it does not ensure that the proper configuration has been opened. Instead, Deepak's answer should be combined with Nilesh's answer to ensure that the configuration from every view is rebuilt. There should be a way to ensure that views are only rebuilt once instead of rebuilding them over and over again if different views reference the same document, but I'm not quite there yet.
First do step 1 from Nilesh's answer, and also "Dim RetVal As Boolean" in the same place
After the first line of Deepak's "Do While Not Nothing Is swView" there is a line that says Set swDrawModel = swView.ReferencedDocument
Add this line underneath: "Set swConfigMgr = swDrawModel.ConfigurationManager"
Change the "Set swDrawModel = swApp.ActivateDoc3(sFileName, True, swRebuildActiveDoc, nErrors)" line to
"Set swDrawModel = swApp.ActivateDoc3(sFileName, True, swDontRebuildActiveDoc, nErrors)"
Underneath that line, add these two lines:
"RetVal = swDrawModel.ShowConfiguration2(swView.ReferencedConfiguration)
So after seeing a post here: Macro: Is file from the Design Library? I understood how to prevent the document from rebuilding unnecessarily. The resulting macro is attached if anyone is interested. (Only four lines were added, a variable declaration, an if statement, and a string concatenation line)
I concatenate the referenced document's path name and configuration name, and then use the InStr() function to see if that string exists in the variable sConfigList. If the string does not exist, then it concatenates the path and configuration name onto the end of sConfigList, and then rebuilds the referenced document. If the string does exist then it just moves on to the next view without opening or rebuilding the referenced document, because it knows that the configuration has already been rebuilt.
Rebuild Referenced File.swp.zip 11.9 KB