9 Replies Latest reply on Apr 8, 2014 12:55 AM by Yong Ning

    Select and Edit a general table

    Tom Fosler
      I am writing a macro that will populate a general table from anexcel document. I would like to program in the option via anIf/Then statement to either edit an existing table in the drawingor insert a new one. I can insert a new one with no problem, but Ican't seem to edit an existing one. Any suggestions? Here issimilar code that works for a Revision Table:

      Dim RevTable As Object
      If Not Part.GetCurrentSheet.RevisionTable Is Nothing Then
      Set RevTable = Part.GetCurrentSheet.RevisionTable
      Else
      Set RevTable = Part.GetCurrentSheet.InsertRevisionTable(True, 0, 0,4, _
      "W:BLOCKSSolidWorks TemplatesOtherREVISIONTABLE.sldrevtbt")
      End If

      Thanks!
        • Select and Edit a general table
          SolidAir
          Tom,

          The code worked fine for me. Are you getting any error messages? What version of SolidWorks are you using? I tested in SW2006 SP4.0

          SA
            • Select and Edit a general table
              Tom Fosler
              SolidAir, thanks for responding (in both places). My SW version is2007 SP2.1. Anyway, the issue isn't about the If/Then working for aRevision Table, but for a General Table. The revision table code Isubmitted here is correct; this was a template for when I want tomake a similar event happen with a General Table. Is this handledthorugh the generaltablefeature::getannotations event? SolidAir, doyou have any thoughts on this, to give me some"SolidAir-ity"? (Sorry, I couldn't resist.)

              Cheers!
                • Select and Edit a general table
                  Tom Fosler
                  BTW, the current code I am using for the General Table is thus:

                  Dim genTable As Object
                  'Possible If...Then statement
                  'Set genTable = ??
                  'Else statement
                  Set genTable = Part.InsertTableAnnotation2(True, 0, 0, 4, _
                  "[filepath]PAINT CHART.sldtbt", e, 0)
                  'Above line currently works for insertion
                  'End If

                  This would be similar to the above RevTable If/Then statement.
                    • Select and Edit a general table
                      SolidAir
                      Tom,

                      Sorry I missed the part about the general table.

                      From my research, I do not believe you can use a simple if-then statement. There is only one call that gets a general table (unless you pre-select it in the feature manager tree) and that is TableAnnotation.GeneralTableFeature. But in order to use that call you must first get the TableAnnotation object. Take a look at API example: Get Table Annotation and Contents Example (VB). It gives an example of how to traverse a drawing's views for tables. After you find a table you will have to use the call TableAnnotation.Type to determine what type of table was found. When the type equals swTableAnnotation_General you can then use the call TableAnnotation.GeneralTableFeature.

                      SA
                        • Select and Edit a general table
                          Tom Fosler
                          SolidAir, I will examine that procedure more closely and see if I can figure it out. When I do, I'll post the snippet in this thread. Danke!

                          Tom
                            • Select and Edit a general table
                              Tom Fosler
                              SolidAir, thanks for the insights, I was able to find a solution for the general table insertion. Not only will it choose to either edit an existing table or insert a new one, the table will resize its number of rows based on the number of cells entered in the excel file.

                              Sub Macro4()
                              Dim genTable As Object
                              Set swApp = CreateObject("SldWorks.Application")
                              Set Model = swApp.ActiveDoc
                              Set Draw = Model
                              Set View = Draw.GetFirstView
                              Set Table = View.GetFirstTableAnnotation

                              Dim pRng As Range
                              Set pRng = Range("B25", "B28")

                              If Application.WorksheetFunction.CountA(pRng) = 0 Then
                              MsgBox "No painted Models entered."
                              Exit Sub
                              End If

                              e = Application.WorksheetFunction.CountA(pRng) + 1

                              If Not Table Is Nothing Then
                              Do Until Table.Type = 0
                              Table.GetNext
                              Loop
                              Set genTable = Table
                              Else
                              Set genTable = Model.InsertTableAnnotation2(True, 0, 0, 4, _
                              "PAINT CHART.sldtbt", e, 0)
                              End If
                              If Not genTable Is Nothing Then
                              genTable.BorderLineWeight = 0
                              genTable.GridLineWeight = 0
                              End If

                              g = genTable.RowCount
                              h = Abs(g - e)

                              If g > e Then
                              For i = 0 To h - 1
                              retval = genTable.DeleteRow(0)
                              Next i
                              ElseIf g < e Then
                              For i = 0 To h - 1
                              retval = genTable.InsertRow(1, i)
                              Next i
                              End If

                              For f = 0 To e - 2
                              genTable.Text(f, 0) = Range("B" & (25 + f))
                              genTable.Text(f, 1) = Range("D" & (25 + f))
                              genTable.Text(f, 2) = Range("J" & (25 + f))
                              genTable.Text(f, 3) = Range("M" & (25 + f))
                              Next f

                              End Sub

                              As those of you who are more well-versed in VBA can tell, this is kind of a newbie coding. Any suggestions on making it tighter? I'm trying to use as little code as possible, while trying to maintain as many robust options as possible. If you see anything that may help, please reply. Otherwise, enjoy the free code!
                                • Select and Edit a general table
                                  Hi Tom
                                  First of all, always declare your variable with the type (try to don't use the object type), with this when you type the name of the variable you instantly have acces to all the method and property of this type of object. For forcing you to declare your variable, add at the top of your modules the Option Explicit statement.

                                  Tom Fosler wrote:

                                   

                                  Sub Macro4()

                                  Dim genTable As Object

                                  Set swApp = CreateObject("SldWorks.Application")

                                  Set Model = swApp.ActiveDoc

                                  Set Draw = Model

                                  Set View = Draw.GetFirstView

                                  Set Table = View.GetFirstTableAnnotation



                                  Dim pRng As Range

                                  Set pRng = Range("B25", "B28")



                                  If Application.WorksheetFunction.CountA(pRng) = 0 Then

                                  MsgBox "No painted Models entered."

                                  Exit Sub

                                  End If



                                  e = Application.WorksheetFunction.CountA(pRng) + 1



                                  If Not Table Is Nothing Then

                                  Do Until Table.Type = 0

                                  Second thing try to use the enumeration here Table.Type=0 what is 0 , I think it's swTableAnnotation_General, but when you will look into your code in some years, it will take a moment before to understand what it means.

                      • Re: Select and Edit a general table
                        Yong Ning

                        Private Sub del()

                          Dim SwApp As SldWorks.SldWorks, SwDraw As DrawingDoc

                          Dim SwView As View, SwTable As TableAnnotation

                          Set SwApp = Application.SldWorks

                          Set SwDraw = SwApp.ActiveDoc

                          Set SwView = SwDraw.GetFirstView

                          Debug.Print SwView.Name

                         

                         

                          Set SwTable = SwView.GetFirstTableAnnotation

                          Dim SwAnn As Annotation

                          Debug.Print SwTable.Title

                          Set SwAnn = SwTable.GetAnnotation

                          Debug.Print SwAnn.Layer

                         

                         

                          SwAnn.Layer = "Format"

                          Stop

                            For i = 0 To SwTable.RowCount - 1

                               

                                For j = 0 To SwTable.ColumnCount - 1

                                    Set swTextFormat = SwTable.GetCellTextFormat(i, j)

                                    Debug.Print "Old: " & swTextFormat.CharHeight

                                    With swTextFormat

                                       .CharHeight = 0.003

                                       .WidthFactor = 0.6

                                    End With

                         

                                    Debug.Print "New: " & swTextFormat.CharHeight

                                    SwTable.SetCellTextFormat i, j, False, swTextFormat

                                Next j

                                Debug.Print SwTable.GetRowHeight(i)

                                SwTable.SetRowHeight i, 0.001, 0

                                'Stop

                                Debug.Print SwTable.GetRowHeight(i)

                            Next i

                        End Sub