6 Replies Latest reply on Dec 19, 2017 10:07 AM by Nicholas Haner

    How do I exclude char for renaming sections and details with a macro?

    Nicholas Haner

      I found the script below on another question about renaming sections and detail. What I am trying to do, without success, is to modify it to exclude different characters I,O,Q,S,X & Z. Thought it would be as simple as an

      If curChar = 73 Then

           curChar + 2

           End

      End Is

      but no such luck. Any help would be appreciated. Is there a way to add an exclusion list of some kind?

       

      Const A_CHAR As Integer = 65

      Const Z_CHAR As Integer = 90

       

      Dim swApp As SldWorks.SldWorks

      Dim swDraw As SldWorks.DrawingDoc

      Dim swSheet As SldWorks.Sheet

       

      Dim curChar As Integer

       

      Sub main()

         

          Set swApp = Application.SldWorks

       

          Set swDraw = swApp.ActiveDoc

         

          If swDraw Is Nothing Then

              MsgBox "Please open drawing document"

              End

          End If

             

          Dim vSheetNames As Variant

          vSheetNames = swDraw.GetSheetNames

         

          Dim sheetInd As Integer

          curChar = A_CHAR

                 

          For sheetInd = 0 To UBound(vSheetNames)

             

              swDraw.ActivateSheet vSheetNames(sheetInd)

              Set swSheet = swDraw.Sheet(vSheetNames(sheetInd))

       

              Dim vViews As Variant

              vViews = swSheet.GetViews

             

              Dim i As Integer

         

              For i = 0 To UBound(vViews)

                 

                  Dim swView As SldWorks.View

                 

                  Set swView = vViews(i)

                 

                  Select Case swView.Type

                      Case swDrawingViewTypes_e.swDrawingSectionView

                          Dim swSectionView As SldWorks.DrSection

                          Set swSectionView = swView.GetSection

                          Debug.Print swSectionView.GetLabel

                          swSectionView.SetLabel2 GetNextName

                      Case swDrawingViewTypes_e.swDrawingDetailView

                          Dim swDetailedView As SldWorks.DetailCircle

                          Set swDetailedView = swView.GetDetail

                          Debug.Print swDetailedView.GetLabel

                          swDetailedView.SetLabel GetNextName

                  End Select

                 

              Next

         

          Next

         

          swDraw.ForceRebuild

         

      End Sub

       

      Function GetNextName() As String

          If curChar > Z_CHAR Then

              MsgBox ("Overflow")

              End

          End If

          GetNextName = Chr(curChar) & curText

          curChar = curChar + 1

      End Function

        • Re: How do I exclude char for renaming sections and details with a macro?
          Josh Brady

          You can use a Select Case syntax where you specify multiple values for the case:

          Copied from Select...Case Statement (Visual Basic) | Microsoft Docs :

          You can use multiple expressions or ranges in each Case clause. For example, the following line is valid.

          Case 1 To 4, 7 To 9, 11, 13, Is > maxNumber

            • Re: How do I exclude char for renaming sections and details with a macro?
              Nicholas Haner

              AWESOME... Thank you.

              For anyone else looking for it.....

              Const A_CHAR As Integer = 65

              Const Z_CHAR As Integer = 90

               

              Dim swApp As SldWorks.SldWorks

              Dim swDraw As SldWorks.DrawingDoc

              Dim swSheet As SldWorks.Sheet

              Dim curChar As Integer

               

              Sub main()

                

                  Set swApp = Application.SldWorks

               

                  Set swDraw = swApp.ActiveDoc

                

                  If swDraw Is Nothing Then

                      MsgBox "Please open drawing document"

                      End

                  End If

                    

                  Dim vSheetNames As Variant

                  vSheetNames = swDraw.GetSheetNames

                

                  Dim sheetInd As Integer

                  curChar = A_CHAR

                        

                  For sheetInd = 0 To UBound(vSheetNames)

                    

                      swDraw.ActivateSheet vSheetNames(sheetInd)

                      Set swSheet = swDraw.Sheet(vSheetNames(sheetInd))

               

                      Dim vViews As Variant

                      vViews = swSheet.GetViews

                    

                      Dim i As Integer

                

                      For i = 0 To UBound(vViews)

                        

                          Dim swView As SldWorks.View

                        

                          Set swView = vViews(i)

                        

                          Select Case swView.Type

                              Case swDrawingViewTypes_e.swDrawingSectionView

                                  Dim swSectionView As SldWorks.DrSection

                                  Set swSectionView = swView.GetSection

                                  Debug.Print swSectionView.GetLabel

                                  swSectionView.SetLabel2 GetNextName

                              Case swDrawingViewTypes_e.swDrawingDetailView

                                  Dim swDetailedView As SldWorks.DetailCircle

                                  Set swDetailedView = swView.GetDetail

                                  Debug.Print swDetailedView.GetLabel

                                  swDetailedView.SetLabel GetNextName

                          End Select

                        

                      Next

                

                  Next

                

                  swDraw.ForceRebuild

                

              End Sub

               

              Function GetNextName() As String

                 

                  If curChar > Z_CHAR Then

                      MsgBox ("Overflow")

                      End

                  End If

               

                  GetNextName = Chr(curChar) & curText

                 

                  curChar = curChar + 1

               

              Select Case curChar

                  Case 73, 79, 81, 83, 88, 90

                  curChar = curChar + 2

              End Select

               

              End Function

            • Re: How do I exclude char for renaming sections and details with a macro?
              Nicholas Haner

              This is working well so far in testing. The only thing it doesn't do well is handle past "Z". i.e. AA, AB, AC. Any ideas?

                • Re: How do I exclude char for renaming sections and details with a macro?
                  Elmar Klammer

                  Hi Nicholas,

                   

                  See if that version can help you...

                   

                  Function GetNextName() As String
                  Dim I As Integer

                      If curChar > Z_CHAR Then
                      'If curChar > 69 Then 'letter Z is reached
                          GetNextName = "A" & Chr(A_CHAR + curChar - 69)
                          curChar = curChar + 1
                          Debug.Print "NextName - " & GetNextName
                          Exit Function
                          End
                      End If
                         
                      GetNextName = Chr(curChar) & curtext
                     
                      Debug.Print "curChart - " & curChar

                      curChar = curChar + 1

                  End Function

                    • Re: How do I exclude char for renaming sections and details with a macro?
                      Nicholas Haner

                      Had to tweek the numbers just a little. yours jumped from "Y" to "AW"

                       

                      The below goes from "Y" to "AA" and excludes I,O,Q,S,X, and Z. I've tested this on a drawing with sections out to "CY-CY" and it works nicely. Thank you all for your help.

                       

                      End Function

                      Function GetNextName() As String
                      Dim I As Integer

                      If curChar > Z_CHAR Then
                      'If curChar > 90 Then 'letter Z is reached
                      GetNextName = "A" & Chr(A_CHAR + curChar - 90)
                      curChar = curChar + 1
                      Debug.Print "NextName - " & GetNextName
                      Exit Function
                      End
                      End If

                      GetNextName = Chr(curChar) & curtext

                      Debug.Print "curChart - " & curChar

                      curChar = curChar + 1

                       

                      Select Case curChar

                          Case 73, 79, 81, 83, 88, 90

                          curChar = curChar + 1

                      End Select

                       

                      End Function