×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Multiple Files in Macro

Multiple Files in Macro

Multiple Files in Macro

(OP)
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

RE: Multiple Files in Macro

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.

RE: Multiple Files in Macro

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

RE: Multiple Files in Macro

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources