2 Replies Latest reply on Oct 7, 2012 11:18 AM by Steven Wright

    Batch open files and cycle through sheets code problem

    Steven Wright

      I am having some difficulty writing some code to cycle through files of a certain extension, open them, then cycle through the sheets within the file and perform an operation on each sheet. The difficulty I am having is cycling through the sheets properly. I actually have some code to cycle through the files in a directory, and then some code to cycle through the sheets. Each of these codes work fine by themselves. I just cannot seem to combine the code into one macro. Unfortunately I am not well versed in vba so I am sure my error is something very simple. Here is the code:

       

      Option Explicit

      Dim swApp           As SldWorks.SldWorks
      Dim swModel         As SldWorks.ModelDoc2
      Dim swDraw          As SldWorks.DrawingDoc
      Dim sDirectory      As String
      Dim workDrive       As String
      Dim workDir         As String
      Dim swDocType       As String
      Dim Response        As String
      Dim Doc             As Object
      Dim longstatus      As Long
      Dim longwarnings    As Long
      Dim vSheetName      As Variant
      Dim i               As Integer
      Dim DwgView         As Object
      Dim Note            As Object
      Dim Note_Text       As String
      Dim rval            As Boolean
      Dim Silent          As Boolean

       

      Sub main()

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

      'sDirectory = InputBox(Prompt:="What directory do you want to process?", Title:="Process Directory",Default:="C:\Users\swright\Desktop\Macro Project")
      sDirectory = "C:\Users\swright\Desktop\Macro Project"

      workDrive = Left(sDirectory, 1)

      workDir = sDirectory

      ChDrive (workDrive)

      ChDir (workDir)

      swDocType = ".SLDDRW"

      Response = Dir("")

      Do Until Response = ""

      If Right(UCase$(Response), 7) = swDocType Then
       
          Set Doc = swApp.OpenDoc6(Response, 3, 0, "", longstatus, longwarnings)

          Set Doc = swApp.ActiveDoc
         
          ' Get all the sheet names
          vSheetName = swDraw.GetSheetNames
         
          ' Start with the first sheet, iterate through all the sheets
          For i = 0 To UBound(vSheetName)
             
              swDraw.ActivateSheet (vSheetName)
             
              Set DwgView = Doc.GetFirstView()
         
              ' Starting with the sheet, iterate through all views in drawing
              While Not (DwgView Is Nothing)
             
                  ' Get the first note in this view
                  Set Note = DwgView.GetFirstNote()
         
                  ' Repeat until there are no more notes to get
                  While Not (Note Is Nothing)
             
                      Note_Text = UCase(Note.GetText)
             
                      rval = Note.SetText(Note_Text)
             
                      Set Note = Note.GetNext()
                 
                  Wend
             
                  ' Get the next view. Or not.
                  Set DwgView = DwgView.GetNextView()
             
              Wend
         
          Doc.ViewZoomtofit2
         
          Next i
           
          ' Save the drawing
          Doc.Save2 Silent
         
          ' Close the drawing
          swApp.CloseDoc (Response)
           
      End If

      Response = Dir()

      Loop

      End Sub

       

      If anyone can help provide some input to correct the code I would really appreciate it.

       

      Also, if you have any tips to improve any other parts of the code then please by all means let me know. Thank you.

        • Re: Batch open files and cycle through sheets code problem
          Deepak Gupta

          Welcome to SolidWorks forums Steven.

           

          Here is the corrected code:

          Option Explicit

          Dim swApp           As SldWorks.SldWorks

          Dim swModel         As SldWorks.ModelDoc2

          Dim swDraw          As SldWorks.DrawingDoc

          Dim sDirectory      As String

          Dim workDrive       As String

          Dim workDir         As String

          Dim swDocType       As String

          Dim Response        As String

          Dim longstatus      As Long

          Dim longwarnings    As Long

          Dim vSheetName      As Variant

          Dim i               As Integer

          Dim DwgView         As Object

          Dim Note            As Object

          Dim Note_Text       As String

          Dim rval            As Boolean

          Dim Silent          As Boolean

           

          Sub main()

           

           

          Set swApp = Application.SldWorks

           

           

          'sDirectory = InputBox(Prompt:="What directory do you want to process?", Title:="Process Directory",Default:="C:\Users\swright\Desktop\Macro Project")

          sDirectory = "C:\Users\swright\Desktop\Macro Project\" ' \ was missing in the path

           

           

          workDrive = Left(sDirectory, 1)

           

           

          workDir = sDirectory

           

           

          ChDrive (workDrive)

           

           

          ChDir (workDir)

           

           

          swDocType = ".SLDDRW"

           

           

          Response = Dir("")

           

           

          Do Until Response = ""

           

           

          If Right(UCase$(Response), 7) = swDocType Then

           

              Set swDraw = swApp.OpenDoc6(Response, 3, 0, "", longstatus, longwarnings) ' replaced Doc with swDraw everywhere in the code

           

              Set swDraw = swApp.ActiveDoc

           

              ' Get all the sheet names

              vSheetName = swDraw.GetSheetNames

           

              ' Start with the first sheet, iterate through all the sheets

              For i = 0 To UBound(vSheetName)

           

                  swDraw.ActivateSheet vSheetName(i) ' ( i ) was misisng here

           

                  Set DwgView = swDraw.GetFirstView()

           

                  ' Starting with the sheet, iterate through all views in drawing

                  While Not (DwgView Is Nothing)

           

                      ' Get the first note in this view

                      Set Note = DwgView.GetFirstNote()

           

                      ' Repeat until there are no more notes to get

                      While Not (Note Is Nothing)

           

                          Note_Text = UCase(Note.GetText)

           

                          rval = Note.SetText(Note_Text)

           

                          Set Note = Note.GetNext()

           

                      Wend

           

                      ' Get the next view. Or not.

                      Set DwgView = DwgView.GetNextView()

           

                  Wend

           

              swDraw.ViewZoomtofit2

           

              Next i

           

              ' Save the drawing

              swDraw.Save2 Silent

           

              ' Close the drawing

              swApp.CloseDoc (Response)

           

          End If

           

          Response = Dir()

           

          Loop

           

          End Sub

           

          You might also find this post helpful for your future reference.

          • Re: Batch open files and cycle through sheets code problem
            Steven Wright

            Deepak, thank you so much for the response. I had tried some of your suggestions before but could not get them to work as I was stepping through the macro. I did find that if I run through the macro from start to finish (not stepping through it) then it works fine. I am not sure what causes that. Thank you very much though. This saves me a significant amount of time.

             

            Steve