AnsweredAssumed Answered

Closing a document within a while loop?

Question asked by Aaron Torberg on Jan 25, 2018
Latest reply on Jan 26, 2018 by Aaron Torberg

Sorry guys, I am pretty new to macros - please go easy on me.  I thought this change would be simple, but I am having trouble.  I have a macro that cycles through the open documents and looks for drawing files, then sets the print options and prints all of the open drawings.  I wanted to have a option on the user form for closing the documents after they are printed.  I get an error though and I think it has to do with the fact that I am closing a document that the while loop relies on.  Here is my code:


Public Sub PrintButton()


    If UserForm1.PrintAllOpen.Value = True Then


        Dim OrriginalDoc As String

        OrriginalDoc = swModel.GetTitle


        Set swModel = Swapp.GetFirstDocument


            While Not swModel Is Nothing


                If swModel.Visible = 1 And swModel.GetType = 3 Then

                    Swapp.ActivateDoc (swModel.GetTitle)

                    Call PrintActive


                    Dim CurrentDoc As String

                    CurrentDoc = swModel.GetTitle


                End If


                If UserForm1.CloseDoc.Value = True Then

                    If swModel.Visible = 1 And swModel.GetType = 3 Then


                        Swapp.CloseDoc (CurrentDoc)


                    End If

                End If


                Set swModel = swModel.GetNext




        If UserForm1.CloseDoc.Value = False Then


            Swapp.ActivateDoc (OrriginalDoc)


        End If




        Call PrintActive


    End If



    MsgBox "Print Complete!"


End Sub



The error I get is:


"Run-time error '-(numbers):


Automation Error

The object invoked has disconnected from its clients."


Can someone show me the code required to close all open drawing files?  Or even just point me in the correct direction?  Thanks!