Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Files in Macro

Status
Not open for further replies.

MJDavis

Industrial
Mar 2, 2005
7
0
0
US
I have a master file, driven by a design table. The macro is supposed to;
- save each configuration of this master file out as a separate file
- open this new file, delete the design table and all other configurations
- re-save modified file
- switch back to original master file, index to next config and repeat.
- master file is to remain unchanged.

The macro runs correctly for 1st configuration (creates new file, opens new file, strips out DT & configs, re-saves new file.

The problem comes at trying to switch back to original master file. I get a run-time error "The object invoked has disconnected from its clients"

What am I missing here? Below is the section of code giving the error.

CT = 0
For x = 0 To ListBoxUserSelect.ListCount - 1 ' each item
If ListBoxUserSelect.Selected(x) = True Then ' item select
CT = CT + 1
End If
Next ' Get next cfg
LabelStatus.Caption = "" ' Clear status

DocName = ModelDoc2.GetTitle ' Doc title
DocPathname = ModelDoc2.GetPathName ' Doc path
For i = 1 To Len(DocPathname)
If Mid$(DocPathname, i, 1) = "\" Then z = i ' last slash
Next i
DocPath = Left$(DocPathname, z) ' remove doc name

Count = 0
For x = 0 To ListBoxUserSelect.ListCount - 1 ' each item
If ListBoxUserSelect.Selected(x) = True Then ' item select
ThisConfigName = ListBoxUserSelect.List(x, 0)
ExportName = ThisConfigName & _
ComboFileType.List(ComboFileType.ListIndex, 1) ' Export name
LabelStatus.Caption = " (" & Count + 1 & "/" & CT & ")" & _
" Exporting: " & ExportName ' Show name
FormLibExport.Repaint ' Repaint form
ModelDoc2.ShowConfiguration2 ThisConfigName ' Show config <<<ERRORS HERE ON 2ND PASS>>>
ModelDoc2.GraphicsRedraw2 ' Redraw screen
Retval = ModelDoc2.Extension.SelectByID2("Sketch1", "SKETCH", _
0#, 0#, 0#, False, 0, Nothing, swSelectOptionDefault) ' Select sketch
ModelDoc2.SaveAs4 DocPath & ExportName, swSaveAsCurrentVersion, _
swSaveAsOptions_Silent, Errors, Warnings ' Create file
'Open the new file with the Correct Configuration
Set ModelDocCopy = swApp.OpenDoc6(ExportName, swDocPART, _
swOpenDocOptions_Silent, ThisConfigName, Errors, Warnings)
' Delete design table
ModelDocCopy.DeleteDesignTable

'Get all the configurations names into an array
ConfigNames = ModelDocCopy.GetConfigurationNames

'Delete each configuration except the one that is the active one
For nCountCopy = 0 To UBound(ConfigNames)
If ConfigNames(nCountCopy) <> ConfigNames(nCount) Then
ModelDocCopy.DeleteConfiguration2 (ConfigNames(nCountCopy))
ModelDocCopy.DeleteConfiguration2 ("Default")
End If
Next

'Save and close the modelcopy
ModelDocCopy.SaveSilent
swApp.CloseDoc ModelDocCopy.GetPathName
Set ModelDocCopy = Nothing
Count = Count + 1
CheckBoxFilter.Enabled = True
EXType = "configurations"
End If
Next ' Get next cfg

Any help would be greatly appreciated.
- Mark
 
Replies continue below

Recommended for you

There are two things I see if the code you've posted is actually what you're using. First of all, you probably shouldn't use the variable name "ModelDoc2", since that's the name of a type of variable. That can really screw you up.

Secondly, when you do the SaveAs4 operation it doesn't look like you're using the option "swSaveAsOptions_Copy". The result is that the original file is actually closed and the copy becomes the active document. I'm not sure what happens to the ModelDoc2 object at that point. It will either point to the new document (in which case "ModelDoc2" and "ModelDocCopy" both refer to the same object) or it goes out of scope (in other words, it becomes disconnected from its client) and doesn't exist anymore. In any case, it's out of scope after you close ModelDocCopy. You can add swSaveAsOptions_Copy to swSaveAsOptions_Silent (addition operation) since that argument is a bitmask.
 
Not sure if the following helps, but it may give something to build on. It saves each config as active to a separate file but does not delete the inactive configs.

Configuration Macro
thread559-147091

[cheers]
 
Thanks handleman,

works nicely now. I knew I was losing the file somewhere, bit completly missed the Save As - copy thing. (so obvious when someone point it out.)

Thanks again.
- Mark
 
Status
Not open for further replies.
Back
Top