5 Replies Latest reply on Apr 19, 2016 6:43 PM by Viktor Bovzdarenko

    Run macro on all open drawings

    Anton Miller

      Is there a way to run a macro on all open drawings?

       

      so lets say I open up 20 different drawings (could be different file locations), click the macro button and have it cycle through them all?  I tried to record this function and it always used the file name.  I'm assuming there is a way to do like what you would for a batch folder run macro, but I don't know how.

       

      I wish to have the macro save each open drawing as a pdf file name being a custom property.  macro below.

       

      'File Save As PDF

      Dim swApp           As SldWorks.SldWorks
      Dim swModel         As SldWorks.ModelDoc2
      Dim swDraw          As SldWorks.DrawingDoc
      Dim swCustProp      As CustomPropertyManager
      Dim valOut          As String
      Dim resolvedValOut  As String
      Dim Filepath        As String
      Dim FileName        As String

      Sub main()

      Set swApp = Application.SldWorks
      Set swDraw = swApp.ActiveDoc

      If (swDraw Is Nothing) Or (swDraw.GetType <> swDocDRAWING) Then

      swApp.SendMsgToUser ("To be used for drawings only, Open a drawing first and then TRY AGAIN!")

      Exit Sub

      End If

      Set swView = swDraw.GetFirstView
      Set swView = swView.GetNextView
      Set swModel = swView.ReferencedDocument
      Set Sheet = swApp.ActiveDoc.GetCurrentSheet

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

      swCustProp.Get2 "dwgnumber1", valOut, resolvedValOut 'Change the custom property name here

      Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))

      FileName = Left(swDraw.GetTitle, Len(swDraw.GetTitle) - 9)

      swDraw.SaveAs (Filepath + resolvedValOut + ".PDF") 'Change the custom property text here
        
      End Sub

        • Re: Run macro on all open drawings
          Viktor Bovzdarenko

          Hi Anton

          you can get all open documents using: Set swModel = swApp.GetFirstDocument; Set swModel = swModel.GetNext

          example: 2014 SOLIDWORKS API Help - Get Names of Open Documents Example (VBA)

           

          Also you can use a macro from #TASK library.

          It has an option to combine the name of resulting file from several variables. In your situation it can be just specified by you custom property.

           

          Regards

          Viktor

            • Re: Run macro on all open drawings
              Anton Miller

              '-------------------------------------

              '

              ' Preconditions: SolidWorks application is running.

              '

              ' Postconditions: None

              '

              '-------------------------------------

              Option Explicit

              Public Enum swDocumentTypes_e

                  swDocNONE  = 0       '  Used to be TYPE_NONE

                  swDocPART  = 1       '  Used to be TYPE_PART

                  swDocASSEMBLY  = 2   '  Used to be TYPE_ASSEMBLY

                  swDocDRAWING  = 3    '  Used to be TYPE_DRAWING

               

              End Enum

              Sub main()

                  Dim  swApp                       As  SldWorks.SldWorks

                  Dim  swModel                     As  SldWorks.ModelDoc2

                  Dim  swModelTitle                As  SldWorks.ModelDoc2

                  Dim  swModelPath                 As  SldWorks.ModelDoc2

                  Dim  cTitle                      As  New Collection

                  Dim  vTitle                      As  Variant

                  Dim  cPath                       As  New Collection

                  Dim  vPath                       As  Variant

                 

                  Set  swApp = Application.SldWorks

                  Set  swModel = swApp.GetFirstDocument

                 

                  While  Not swModel Is Nothing

                      '  Assembly components are opened, but are not visible

                      '  until opened by the user

                      Debug.Print  swModel.GetPathName & "    ["  & swModel.Visible & "]"

                      Debug.Print  "  "  & swModel.GetTitle & "  [" & swModel.GetType  & "]"

                     

                      '  The document name contains a filename extension

                      '  if the document has been already saved

                      '  and is subject to Microsoft Explorer setting;

                      '  the document name does not contain a

                      '  filename extension if the document has not been saved;

                      ' ModelDoc2::GetPathName is blank until the file is saved

                      cTitle.Add  swModel.GetTitle

                     

                      cPath.Add  swModel.GetPathName

                 

                      Set  swModel = swModel.GetNext

                  Wend

                  Debug.Print  "--------------------------"

                 

                  For  Each vTitle In cTitle

                      Set  swModelTitle = swApp.GetOpenDocument(vTitle):  Debug.Assert Not swModelTitle Is Nothing

                 

                      Debug.Print  swModelTitle.GetPathName &  "  ["  & swModelTitle.Visible &  "]"

                      Debug.Print  "  "  & swModelTitle.GetTitle &  " [" & swModelTitle.GetType  & "]"

                  Next  vTitle

                  Debug.Print  "--------------------------"

                 

                  For  Each vPath In cPath

                      Set  swModelPath = swApp.GetOpenDocument(vPath)

                      If  Not swModelPath Is Nothing Then

                          Debug.Print  swModelPath.GetPathName &  "  ["  & swModelPath.Visible &  "]"

                          Debug.Print  "  "  & swModelPath.GetTitle &  " [" & swModelPath.GetType  & "]"

                      End  If

                  Next  vPath

                  Debug.Print  "--------------------------"

              End Sub

              '-------------------------------------

               

               

               

               

               

               

              I am lost in this...  where would I put my macro, im assuming right before the "wend"  but it isnt working.

               

              fails here

              Capture.PNG