8 Replies Latest reply on Apr 16, 2015 10:03 AM by Alex Graff

    Run Macro in Task Scheduler

    Alex Graff

      Hello,

      I just started writing macros yesterday so I am very new to this. By sifting through various forums and many discussions on this forum I managed to create a macro to do what I need. With a drawing open it deletes the current revision table and inserts a new revision table in the proper location. It isn't quite perfect because I was having trouble with the proper anchor location but somehow it is working. I now need a way to run this in task scheduler so it will open, change the revision table and save all the drawings in a given directory. Any help with this would be great. I will post what I currently have. Like I said it is bits and pieces that I put together that I found so I can't take much credit for the work done. I am not familiar with the namespace and arguments everything uses so it might be a little messy still.

       

      I believe I need to take what I have and update it and save it as a .swb file. I know I need to define a string and then have the string be $$$my_file_path$$$ so I can input the desired directory in the parameters in task managers custom task. I am just getting to the point where I am getting lost with only minimal programming experience and only a day in SolidWorks API. The code below is what I have working as a macro on an open file. I am getting lost when it comes to opening changing and saving multiple files in a folder.

       

      It took me 6 hours to get only as far as I am so I figured it was time to reach out for help. If I cant get this working I'm stuck doing all several hundred drawings by hand, which won't be as bad as it was now that I at least have a macro... but still a lengthy process.

       

      Thanks in advance!

       

      Const TABLE_TEMPLATE As String = "H:\SolidWorks\Revision Table Templates\MARIS REVISION.sldrevtbt"

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swDraw As SldWorks.DrawingDoc

      Dim swSheet As SldWorks.Sheet

      Dim vViews As Variant

      Dim swView As SldWorks.View

      Dim swTableAnn As SldWorks.TableAnnotation

      Dim swAnn As SldWorks.Annotation

      Dim swRevTableAnn As SldWorks.RevisionTableAnnotation

       

       

       

       

       

      Sub main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swDraw = swModel

          Set swSheet = swDraw.GetCurrentSheet

       

          'Delete existing revision table

          Set swView = swDraw.GetFirstView

          Do While Not swView Is Nothing

              Set swTableAnn = swView.GetFirstTableAnnotation

              While Not swTableAnn Is Nothing

                  If swTableAnn.Type = swTableAnnotation_RevisionBlock Then

                      Set swAnn = swTableAnn.GetAnnotation

                      swAnn.Select3 False, Nothing

                      swModel.Extension.DeleteSelection2 Empty

                      Exit Do

                  End If

                  Set swTableAnn = swTableAnn.GetNext

              Wend

              Set swView = swView.GetNextView

          Loop

       

          'Add new revision table

          Set swRevTableAnn = swSheet.InsertRevisionTable(True, Empty, Empty, _

              swBOMConfigurationAnchor_TopRight, TABLE_TEMPLATE)

      End Sub

        • Re: Run Macro in Task Scheduler
          Keith Rice

          Check out this blog post: Schedule Macros Using the Task Scheduler

           

          I should note, however, that the only reason to use the Task Scheduler with macros is that you can schedule them to run at a certain time. If your goal is to simply have the macro process every part in a folder, use the VBA Dir() function.

           

          For example, here is how to open all parts in a specified folder:

           

          Dim swApp As SldWorks.SldWorks
          Dim swDoc As ModelDoc2
          Dim fileerror As Long
          Dim filewarning As Long
          
          Const folder As String = "C:\SolidWorks Training Files\Essentials\Lesson01\Case Study\"
          Dim files As Variant
              
          Sub main()
              Set swApp = Application.SldWorks
              files = Dir(folder & "*.sldprt", vbNormal)
              Do While files <> ""
                  swApp.OpenDoc6 folder & files, swDocPART, 0, "", fileerror, filewarning
                  files = Dir
              Loop
          End Sub

           

          Keith

          SolidWorks API Training

            • Re: Run Macro in Task Scheduler
              Alex Graff

              Thank you for your response. I posted before taking a few days off not anticipating such a quick response. My goal is to write it to run in task scheduler. I have over 400 drawings to fix revision tables on so my hope is to set it to run after I leave work one day so I don't feel like I am wasting a day sitting and watching paint dry. Some of the drawings are for very large assemblies and can take a while to open. I will look at that link you posted and see if I can pick my way through how to get things working.

                • Re: Run Macro in Task Scheduler
                  Keith Rice

                  If you're going to run it write when you leave, then I still wouldn't use the Task Scheduler. Just write the macro as I described using the Dir() function, and start it as you leave. The Task Scheduler is adding an extra "layer" to your program so I would avoid it if possible. Nevertheless, you know what you need so best of luck as you move forward.

                   

                  Keith

                  SolidWorks API Tutorials

                    • Re: Run Macro in Task Scheduler
                      Alex Graff

                      If using a regular marco is simpler I will go that route. I am very new to writing for SolidWorks so any difficulty I can remove I will try. Currently I am running into a problem where I get stuck in a loop and the program gets stuck deleting and re-adding revision blocks. I think I've currently made things messier with unneeded declarations while trying to sort out what everything is. I am more than likely over complicating things.

                  • Re: Run Macro in Task Scheduler
                    Alex Graff

                    Hello Again,

                     

                    After a few weeks doing other work I am back at writing this macro. I am still struggling quite a bit with it. I have my basic marco to edit my revision tables. I am still trying to figure out how to run it on all drawing files in a folder. I need to open them, run my existing macro, save the change and then close the drawing and move to the next one.  Any help with any of these next steps would be great.

                     

                    Thanks,

                    Alex

                  • Re: Run Macro in Task Scheduler
                    Alex Graff

                    I tried it before and had no results. Then I realized that i forgot the \ at the end of my folder path... that one error had me hung up. Thank you for the help. Now I am on to the code to save and close each drawing after updating