4 Replies Latest reply on Nov 18, 2015 12:41 PM by Patrick Malone

    How do I add a counter / repeat / while loop to repeat one step (add multiple lines to a table)??

    Patrick Malone

      This is another users code for adding a line to a table (BOM)

      (Will have to look for the actual post to credit properly..)

       

      At the bottom of the code is this line:

       

      ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

       

      I have simply copied the line the number of lines I want to add to my table.

      Works just fine for my purposes!!

      In desiring to learn more I was thinking that I would like to get the routine to ask me

      how many lines I'd like to add to my table then have it repeat the above line (that one step) that many times...

       

      I assume its a counter or a while loop.

      This code works.

       

      Hoping someone will have some mercy on me..

       

      = = =

       

      Option Explicit

      Option Compare Text

       

      Dim swApp As SldWorks.SldWorks

      Dim Part As ModelDoc2

       

      Sub main()

       

          Dim myTable As TableAnnotation

       

          Dim selMgr As SelectionMgr

       

          Dim i1 As Long

          Dim i2 As Long

          Dim i3 As Long

          Dim i4 As Long

          Dim ret As Boolean

       

          Set swApp = Application.SldWorks

       

          Set Part = swApp.ActiveDoc

       

          If Part Is Nothing Then Exit Sub

       

          If Part.GetType <> 3 Then Exit Sub

       

          Set selMgr = Part.SelectionManager

       

          If selMgr.GetSelectedObjectCount2(-1) <> 1 Then Exit Sub

       

          If selMgr.GetSelectedObjectType3(1, -1) <> swSelANNOTATIONTABLES Then Exit Sub

       

          Set myTable = selMgr.GetSelectedObject6(1, -1)

       

          myTable.GetCellRange i1, i2, i3, i4

       

       

      'Copy following line for the Number of lines you want to add - 5 entries = 5 new lines to the table

      ' ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

       

           ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

          ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

          ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

          ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

          ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

       

      End Sub

       

       

       

      Thanks in advance!

        • Re: How do I add a counter / repeat / while loop to repeat one step (add multiple lines to a table)??
          Jacob Corder

          A Simple For  loop works

           

          So

          Function Add5RowsToTable(byval MyTable as tableAnnotation) as boolean

              Dim i1 As Long

              Dim i2 As Long

              Dim i3 As Long

              Dim i4 As Long

               myTable.GetCellRange i1, i2, i3, i4

                addrow(mytable,5,i1)

           

          end Function

          Function AddRow(byval MyTable as TableAnnotation, byval Count as integer,byval ReferencedIndex as integer) as Boolean

           

               Dim RetBool as boolean

               For i = 1 to Count

                      retbool = mytable.insertRow(swTableItemInsertPosition_Before, referencedIndex)

               next

               return retbool

          end if

          • Re: How do I add a counter / repeat / while loop to repeat one step (add multiple lines to a table)??
            Digvijay Taunk

            For portion

             

            'Copy following line for the Number of lines you want to add - 5 entries = 5 new lines to the table

            ' ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

             

                 ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

                ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

                ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

                ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

                ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

            Loop should be:

            RowCount = InputBox("How Many Rows Would You Like To Add?")

            Dim RetBool as boolean

                 For J = 0 to RowCount-1

                        retbool = mytable.insertRow(J, i1)

                 next

              • Re: How do I add a counter / repeat / while loop to repeat one step (add multiple lines to a table)??
                Patrick Malone

                Awesome! That is what I needed. <THANK YOU!!>

                 

                One thing I don't understand.

                If I enter 3, I get 2 new rows.

                I assume that is because of the " -1"

                 

                I had to add the top line (below) to get an accurate number of rows.

                 

                (My intent was to pick where I wanted the rows added and I wanted them together)

                This adds my first new row before the currently selected row
                then any additional rows are added at the beginning/top of the table.

                ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

                 

                         For J = 0 To RowCount - 1

                            RetBool = myTable.InsertRow(J, i1)

                        Next

                (I have a two part followup question)

                 

                1) How can I get all the new rows to be added in sequence (together before current selected row)

                 

                2) How can I get all the rows to be added at the top of the table?

                 

                ===========HERE IS MY CURRENT CODE=============

                 

                 

                'insert multiple empty rows (above or before a user selected row) into a TABLE in drawing.

                 

                Option Explicit

                 

                Option Compare Text

                 

                 

                Dim swApp As SldWorks.SldWorks

                 

                Dim Part As ModelDoc2

                 

                 

                 

                Sub main()

                 

                    Dim myTable As TableAnnotation

                 

                    Dim selMgr As SelectionMgr

                 

                    Dim i1 As Long

                 

                    Dim i2 As Long

                 

                    Dim i3 As Long

                 

                    Dim i4 As Long

                 

                    Dim ret As Boolean

                   

                '======================

                    Dim RetBool As Boolean

                 

                    Dim RowCount As Integer

                   

                    Dim J As Integer

                '======================

                 

                    Set swApp = Application.SldWorks

                 

                 

                    Set Part = swApp.ActiveDoc

                 

                    If Part Is Nothing Then Exit Sub

                 

                    If Part.GetType <> 3 Then Exit Sub

                 

                    Set selMgr = Part.SelectionManager

                 

                    If selMgr.GetSelectedObjectCount2(-1) <> 1 Then Exit Sub

                 

                    If selMgr.GetSelectedObjectType3(1, -1) <> swSelANNOTATIONTABLES Then Exit Sub

                 

                 

                    Set myTable = selMgr.GetSelectedObject6(1, -1)

                 

                    myTable.GetCellRange i1, i2, i3, i4

                   

                 

                '======================

                RowCount = InputBox("How Many Rows Would You Like To Add?")

                 

                     ret = myTable.InsertRow(swTableItemInsertPosition_Before, i1)

                 

                         For J = 0 To RowCount - 1

                            RetBool = myTable.InsertRow(J, i1)

                        Next

                '======================

                End Sub