3 Replies Latest reply on Jul 22, 2015 4:24 AM by Matt Jones

    How to end a loop in a macro?

    Matt Jones

      I have merged two macro's together and now it saves all open as a pdf building the file name from values of the referenced model in the drawing file then closes the document.

       

      I am having trouble ending the loop where the macro runs through the drawing and everything I try I get the following error;

       

      Capture.JPG

       

      I have since deleted anything that trys to end the loop to test the rest of the macro and it works great! It just crashes at the end when there is no open document!

       

      Can someone help me end this loop in the macro?

       

      Your help would be greatly appreciated!!! (see macro below)

       

      _____________________________________________________________________

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swDraw As SldWorks.DrawingDoc

      Dim swCustProp As CustomPropertyManager

      Dim valOut1 As String

      Dim valOut2 As String

      Dim resolvedValOut1 As String

      Dim resolvedValOut2 As String

      Dim Filepath As String

      Dim ConfigName As String

      Dim PartNo As String

      Dim FullName As String

      Dim nFileName As String

       

       

      Sub main()

      Set swApp = Application.SldWorks

       

       

      swDocs = swApp.GetDocuments

           For i = 0 To UBound(swDocs)

                Set Part = swDocs(i)

              

                  Set swDraw = swApp.ActiveDoc

                  Set swView = swDraw.GetFirstView

                  Set swView = swView.GetNextView

                  Set swModel = swView.ReferencedDocument

       

       

                  If (swModel.GetType = swDocPART) Then

       

       

                      Set swModel = swView.ReferencedDocument

                      Set swView = swDraw.GetFirstView

                      Set swView = swView.GetNextView

                      ConfigName = swView.ReferencedConfiguration

                      FullName = swModel.GetTitle

                      PartNo = Left(FullName, Len(FullName) - 7)

                      'Filepath = "C:\Users\m.jones\Desktop\PDF\"

       

       

                      Set swCustProp = swModel.Extension.CustomPropertyManager(ConfigName)

                          swCustProp.Get2 "Description", valOut1, resolvedValOut1

                          swCustProp.Get2 "Revision", valOut2, resolvedValOut2

             

                      nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & PartNo & "-" & ConfigName & "-" & resolvedValOut2 & " " & resolvedValOut1

       

       

                      swDraw.SaveAs3 nFileName & ".PDF", 0, 0

        

                      'MsgBox nFileName & ".PDF" + " Saved as a PDF"

        

                      swApp.QuitDoc swDraw.GetPathName

       

                  ElseIf (swModel.GetType = swDocASSEMBLY) Then

       

       

                      Set swView = swDraw.GetFirstView

                      Set swView = swView.GetNextView

                      Set swModel = swView.ReferencedDocument

                      ConfigName = swView.ReferencedConfiguration

                      FullName = swModel.GetTitle

                      PartNo = Left(FullName, Len(FullName) - 7)

                      'Filepath = "C:\Users\m.jones\Desktop\PDF\"

       

       

                      Set swCustProp = swModel.Extension.CustomPropertyManager("")

                          swCustProp.Get2 "Description", valOut1, resolvedValOut1

                          swCustProp.Get2 "Revision", valOut2, resolvedValOut2

          

                      nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & PartNo & "-" & resolvedValOut2 & " " & resolvedValOut1

       

       

                      swDraw.SaveAs3 nFileName & ".PDF", 0, 0

        

                      'MsgBox nFileName & ".PDF" + " Saved as a PDF"

        

        

                      swApp.QuitDoc swDraw.GetPathName

                  End If

       

       

       

       

          Next i

       

       

       

       

       

       

      End Sub

        • Re: How to end a loop in a macro?
          Ivana Kolin
           Option Explicit
          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Dim swDraw As SldWorks.DrawingDoc
          Dim swCustProp As CustomPropertyManager
          Dim valOut1 As String
          Dim valOut2 As String
          Dim resolvedValOut1 As String
          Dim resolvedValOut2 As String
          
          Dim ConfigName As String
          Dim PartNo As String
          Dim FullName As String
          Dim nFileName As String
          Dim swView As SldWorks.View
          
          
          Sub main()
              Dim swDocs As Variant
              Dim i As Integer
              Set swApp = Application.SldWorks
          
          
          
          
              swDocs = swApp.GetDocuments
              For i = 0 To UBound(swDocs)
                  Set swModel = swDocs(i)
                  If swModel.GetType = swDocDRAWING Then
          
          
                      Set swDraw = swModel
                      Set swView = swDraw.GetFirstView
                      Set swView = swView.GetNextView
                      Set swModel = swView.ReferencedDocument
                      If (swModel.GetType = swDocPART) Then
                          Set swModel = swView.ReferencedDocument
                          Set swView = swDraw.GetFirstView
                          Set swView = swView.GetNextView
                          ConfigName = swView.ReferencedConfiguration
                          FullName = swModel.GetTitle
                          PartNo = Left(FullName, Len(FullName) - 7)
                          'Filepath = "C:\Users\m.jones\Desktop\PDF\"
              
              
                          Set swCustProp = swModel.Extension.CustomPropertyManager(ConfigName)
                          swCustProp.Get2 "Description", valOut1, resolvedValOut1
                          swCustProp.Get2 "Revision", valOut2, resolvedValOut2
              
                          nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & PartNo & "-" & ConfigName & "-" & resolvedValOut2 & " " & resolvedValOut1
              
              
                          swDraw.SaveAs3 nFileName & ".PDF", 0, 0
              
                          'MsgBox nFileName & ".PDF" + " Saved as a PDF"
              
                          swApp.QuitDoc swDraw.GetPathName
              
                      ElseIf (swModel.GetType = swDocASSEMBLY) Then
              
              
                          Set swView = swDraw.GetFirstView
                          Set swView = swView.GetNextView
                          Set swModel = swView.ReferencedDocument
                          ConfigName = swView.ReferencedConfiguration
                          FullName = swModel.GetTitle
                          PartNo = Left(FullName, Len(FullName) - 7)
                          'Filepath = "C:\Users\m.jones\Desktop\PDF\"
              
              
                          Set swCustProp = swModel.Extension.CustomPropertyManager("")
                          swCustProp.Get2 "Description", valOut1, resolvedValOut1
                          swCustProp.Get2 "Revision", valOut2, resolvedValOut2
              
                          nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & PartNo & "-" & resolvedValOut2 & " " & resolvedValOut1
              
              
                          swDraw.SaveAs3 nFileName & ".PDF", 0, 0
              
                          swApp.QuitDoc swDraw.GetPathName
                      End If
                  End If
              Next i
          End Sub
          
          
          
          
          • Re: How to end a loop in a macro?
            Deepak Gupta

            Matt, no hard feeling but I would appreciate if you can just keep one post and don't make several with same contents. Try the following macro:

             

            Dim swApp As SldWorks.SldWorks

            Dim swModel As SldWorks.ModelDoc2

            Dim sModel  As SldWorks.ModelDoc2

            Dim swDraw As SldWorks.DrawingDoc

            Dim swCustProp As CustomPropertyManager

            Dim valOut1 As String

            Dim valOut2 As String

            Dim resolvedValOut1 As String

            Dim resolvedValOut2 As String

            Dim Filepath As String

            Dim PartNo As String

            Dim FullName As String

            Dim nFileName As String

             

            Sub main()

            Set swApp = Application.SldWorks

            Set sModel = swApp.GetFirstDocument

             

             

            While Not sModel Is Nothing

             

            If sModel.GetType = swDocDRAWING Then

                  

                        Set swDraw = sModel

                        Set swView = swDraw.GetFirstView

                        Set swView = swView.GetNextView

                        Set swModel = swView.ReferencedDocument

                       

                        If swModel.GetType = swDocPART Then

                        PartNo = Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)

                                Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)

                                swCustProp.Get2 "Description", valOut1, resolvedValOut1

                                swCustProp.Get2 "Finish", valOut2, resolvedValOut2

                        nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & PartNo & "-" & ConfigName & "-" & resolvedValOut2 & " " & resolvedValOut1

                        swDraw.SaveAs3 nFileName & ".PDF", 0, 0

                            

                        ElseIf (swModel.GetType = swDocASSEMBLY) Then

             

                        PartNo = Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)

                                Set swCustProp = swModel.Extension.CustomPropertyManager("")

                                swCustProp.Get2 "Material", valOut1, resolvedValOut1

                                swCustProp.Get2 "Weight", valOut2, resolvedValOut2

                        nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & PartNo & "-" & ConfigName & "-" & resolvedValOut2 & " " & resolvedValOut1

                        swDraw.SaveAs3 nFileName & ".PDF", 0, 0

                        End If

                       

                        swApp.QuitDoc swDraw.GetPathName

             

            End If

            Set sModel = sModel.GetNext

            Wend

             

            End Sub