4 Replies Latest reply on Jul 12, 2018 9:33 AM by Josh Brady

    How can I get this macro to work on multiple files

    Matthew Grabiec

      Hello,

       

      Currently I am trying to edit my macro to make it work on all the drawings in a specific folder. I've read up on as many discussions I could find but I just can't seem to figure it out. I'll put the working code for one at a time and the current code I came up that is failing to work on multiple files in a folder. Any examples or reworks of the code required for it to work would be extremely helpful.

       

      The macro that works for one at a time:

      Dim swApp As Object

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

       

      Sub main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

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

          Set swApp = _

      Application.SldWorks

       

          Set Part = swApp.ActiveDoc

          'swCustPropMgr.Set "Revision Letter", "1"

          swCustPropMgr.Set "Assembly #", "104500"

          swCustPropMgr.Set "Checked Date", "11JUL18"

          swCustPropMgr.Set "Check By", ""

          swCustPropMgr.Set "Effective Date", "05MAY06"

          swCustPropMgr.Set "EDR Approval Date", "25APR06"

          swCustPropMgr.Set "EDR Originated By", ""

          swCustPropMgr.Set "EDR #", "06-060"

          'swCustPropMgr.Set "Revision Description", "ORIGINAL ISSUE"

       

      boolstatus = Part.Extension.SelectByID2("Sheet1", "SHEET", 0.158120395359656, 1.18392865868883E-02, 0, False, 0, Nothing, 0)

      Part.EditTemplate

      Part.EditSketch

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("Sheet Formats1", "SHEET", 7.44302394941151E-03, 0.264739687978772, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SketchBoxSelect("-0.151307", "0.048840", "0.000000", "0.261962", "-0.205311", "0.000000")

      Part.SetLineColor 0

      boolstatus = Part.Extension.SelectByID2("Sheet Formats1", "SHEET", 0.333181241811392, 1.37149385131569E-02, 0, False, 0, Nothing, 0)

      Part.ViewZoomtofit2

      Part.EditSheet

      Part.EditSketch

      Part.ClearSelection2 True

      Part.ViewZoomtofit2

       

      End Sub

       

      Multiple files in a folder:

      Dim swApp As Object

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

       

      Sub main()

          Set swApp = Application.SldWorks

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

          Set swApp = _

          Application.SldWorks

          Set Part = swApp.ActiveDoc

          Path = "C:\Users\grabiem\SWIM_work\EDR-06-060_104500\Drawings\"

          sFileName = Dir(Path & "*.sldasm")

          Do Until sFileName = ""

          Set swModel = swApp.OpenDoc6(Path + sFileName, swDocASSEMBLY, swOpenDocOptions_Silent, "", nErrors, nWarnings)

          'swCustPropMgr.Set "Revision Letter", "1"

          swCustPropMgr.Set "Assembly #", "104500"

          swCustPropMgr.Set "Checked Date", "11JUL18"

          swCustPropMgr.Set "Check By", ""

          swCustPropMgr.Set "Effective Date", "05MAY06"

          swCustPropMgr.Set "EDR Approval Date", "25APR06"

          swCustPropMgr.Set "EDR Originated By", ""

          swCustPropMgr.Set "EDR #", "06-060"

          'swCustPropMgr.Set "Revision Description", "ORIGINAL ISSUE"

          boolstatus = Part.Extension.SelectByID2("Sheet1", "SHEET", 0.158120395359656, 1.18392865868883E-02, 0, False, 0, Nothing, 0)

          Part.EditTemplate

          Part.EditSketch

          Part.ClearSelection2 True

          boolstatus = Part.Extension.SelectByID2("Sheet Formats1", "SHEET", 7.44302394941151E-03, 0.264739687978772, 0, False, 0, Nothing, 0)

          boolstatus = Part.Extension.SketchBoxSelect("-0.151307", "0.048840", "0.000000", "0.261962", "-0.205311", "0.000000")

          Part.SetLineColor 0

          boolstatus = Part.Extension.SelectByID2("Sheet Formats1", "SHEET", 0.333181241811392, 1.37149385131569E-02, 0, False, 0, Nothing, 0)

          Part.ViewZoomtofit2

          Part.EditSheet

          Part.EditSketch

          Part.ClearSelection2 True

          Part.ViewZoomtofit2

          swApp.CloseDoc swModel.GetTitle

      Set swModel = Nothing

      sFileName = Dir

      Loop

       

      End Sub

       

       

      Thanks,

      Matt

        • Re: How can I get this macro to work on multiple files
          Deepak Gupta

          You can set it to work with #TASK to run in batch mode on multiple files.

          • Re: How can I get this macro to work on multiple files
            Josh Brady

            You need to understand what every line of code does before you can try to make it loop through files.

             

            Every document (modeldoc2) has its own custom property manager.  Whenever you switch to a different file (with OpenDoc6) you need to get a new custom property manager from that document.

             

            When you have "Set Part = swapp.activedoc" you are getting the current model document.  That doesn't mean that Part always refers to the active document.  It means that "Part" always refers to whatever document was active when you set it.  If you want Part to refer to a new/different document, you have to set it = to that new document.

             

            What kind of documents are you trying to open?  Your Dir statement is only looking for assemblies, but it seems that your code is wanting to work with drawings.

              • Re: How can I get this macro to work on multiple files
                Matthew Grabiec

                So what would I change the "Set Part = swapp.activedoc" to in order to make it work with the next file? And how would I get a new custom property manager from the new document? Not really sure what you mean because I don't know much about this stuff.

                 

                I changed a bit of the code and I'm working with this now. I believe this fixes the problem with looking for only assemblies correct? I get a compile error still when I attempt to run the code though.

                 

                Dim swApp As Object

                Dim Part As Object

                Dim boolstatus As Boolean

                Dim longstatus As Long, longwarnings As Long

                Sub main()

                    Set swApp = Application.SldWorks

                    Path = "C:\Users\grabiem\SWIM_work\EDR-06-060_104500\Drawings\"

                    sFileName = Dir(Path & "*.slddrw")

                    Do Until sFileName = ""

                    Set swModel = swApp.OpenDoc6(Path + sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)

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

                    'swCustPropMgr.Set "Revision Letter", "1"

                    swCustPropMgr.Set "Assembly #", "104500"

                    swCustPropMgr.Set "Checked Date", "11JUL18"

                    swCustPropMgr.Set "Check By", " "

                    swCustPropMgr.Set "Effective Date", "05MAY06"

                    swCustPropMgr.Set "EDR Approval Date", "25APR06"

                    swCustPropMgr.Set "EDR Originated By", " "

                    swCustPropMgr.Set "EDR #", "06-060"

                    'swCustPropMgr.Set "Revision Description", "ORIGINAL ISSUE"

                Set Part = swApp.ActiveDoc

                    boolstatus = Part.Extension.SelectByID2("Sheet1", "SHEET", 0.158120395359656, 1.18392865868883E-02, 0, False, 0, Nothing, 0)

                    Part.EditTemplate

                    Part.EditSketch

                    Part.ClearSelection2 True

                    boolstatus = Part.Extension.SelectByID2("Sheet Formats1", "SHEET", 7.44302394941151E-03, 0.264739687978772, 0, False, 0, Nothing, 0)

                    boolstatus = Part.Extension.SketchBoxSelect("-0.151307", "0.048840", "0.000000", "0.261962", "-0.205311", "0.000000")

                    Part.SetLineColor 0

                    boolstatus = Part.Extension.SelectByID2("Sheet Formats1", "SHEET", 0.333181241811392, 1.37149385131569E-02, 0, False, 0, Nothing, 0)

                    Part.ViewZoomtofit2

                    Part.EditSheet

                    Part.EditSketch

                    Part.ClearSelection2 True

                    Part.ViewZoomtofit2

                    swApp.CloseDoc swModel.GetTitle

                Set swModel = Nothing

                sFileName = Dir

                Loop

                 

                End Sub

                  • Re: How can I get this macro to work on multiple files
                    Josh Brady

                    Matthew Grabiec wrote:

                     

                    So what would I change the "Set Part = swapp.activedoc" to in order to make it work with the next file? And how would I get a new custom property manager from the new document? Not really sure what you mean because I don't know much about this stuff.

                     

                     

                    In your first post you had "Set Part..." outside the loop.  In your second post you moved it inside the loop.  That was correct.

                    You also moved "Set swPropMan...." inside the loop.  That was also correct.

                    One possible issue is that OpenDoc6 does not necessarily make the opened document active.  Please read the comments on the API help documentation for OpenDoc6.

                     

                    In your code, Part and swModel are both supposed to refer to the same document.  You don't actually need Part.  Just swModel.  Get rid of "Set Part...", and then everywhere else you can change Part to swModel.

                     

                    I don't see any Save action going on here... I can't remember if swApp.CloseDoc throws up a save warning or if it just closes silent.  If so, nothing this macro does will actually be saved.

                     

                    What is your compile error?  It should tell you which line is the issue.