2 Replies Latest reply on Jan 26, 2018 6:31 AM by Aaron Torberg

    Closing a document within a while loop?

    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

       

                  Wend

                 

              If UserForm1.CloseDoc.Value = False Then

             

                  Swapp.ActivateDoc (OrriginalDoc)

                 

              End If

                     

          Else

         

              Call PrintActive

       

          End If

       

          UserForm1.Hide

          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!

        • Re: Closing a document within a while loop?
          Jacob Corder

          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

                         Dim NextDoc as modeldoc2

                         Set nextDoc = SwModel.GetNext

                          If UserForm1.CloseDoc.Value = True Then

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

                                                         Swapp.CloseDoc (CurrentDoc)

                                

                              End If

                          End If

                          swmodel = NextDoc  

                         

           

                      Wend

                    

                  If UserForm1.CloseDoc.Value = False Then

                

                      Swapp.ActivateDoc (OrriginalDoc)

                    

                  End If

                        

              Else

            

                  Call PrintActive

           

              End If

           

              UserForm1.Hide

              MsgBox "Print Complete!"

           

          End Sub

           

          ok so what is happening is you are closing the doc which separates it from the COM Boundary, meaning you dont care really it just means you cant destroy something before you try to use it again. 

           

          adding the code in bold should fix your issue..  you are getting the next document from a dead document currently this will get it before you kill the document.