AnsweredAssumed Answered

How to end a loop in a macro?

Question asked by Matt Jones on Jul 22, 2015
Latest reply on Jul 22, 2015 by 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

Outcomes