Quantcast
Channel: Manufacturing DevBlog
Viewing all articles
Browse latest Browse all 532

delete and add rows to existing CustomTable

$
0
0

By Xiaodong Liang

Question:

Given there is a drawing with at least one custom table,and the existing table is not empty. Can I delete the existing rows and add the new data I need?

Solution:

Each Row object of CustomTable.Rows provides Delete method that can remove this row. And CustomTable.Rows also provides Add method to add a new row. So the first way is to delete all old rows one by one, and add the new row one by one. e.g. this is a test drawing with a custom table as below.

Download Testdrawing-2016

image

The code below will delete all old rows and add new rows.

Sub modifyTable_Way1()
 
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
   
    ' Set a reference to the active sheet.
    Dim oSheet As Sheet
    Set oSheet = oDrawDoc.ActiveSheet
   
    Dim oTB As CustomTable
    Set oTB = oSheet.CustomTables(1)
   
    'delete the old rows
    Dim oRow As Row
    For Each oRow In oTB.Rows   
        oRow.Delete       
    Next
   
    'assume the new data is available
    Dim oContents(1 To 3) As String
    oContents(1) = "1 - new"
    oContents(2) = "1 - new"
    oContents(3) = "Brass- new"     
    Call oTB.Rows.Add(0, False, oContents)    
    
    oContents(1) = "2 - new"
    oContents(2) = "2 - new"
    oContents(3) = "Aluminium- new"      
    Call oTB.Rows.Add(0, False, oContents)
    
    oContents(1) = "3 - new"
    oContents(2) = "3 - new"
    oContents(3) = "Steel- new"      
    Call oTB.Rows.Add(0, False, oContents)
    
End Sub

 

After the code, the table becomes:

image

 

The second way is to make a note on the columns definitions of the CustomTable, call CustomTable.Delete to delete the table, add the table again with the new data.

Sub modifyTable_Way2()


' Set a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
   
    ' Set a reference to the active sheet.
    Dim oSheet As Sheet
    Set oSheet = oDrawDoc.ActiveSheet
   
    Dim oTB As CustomTable
    Set oTB = oSheet.CustomTables(1)
   
    Dim oTitles() As String
    ReDim Preserve oTitles(1 To oTB.Columns.Count)
    Dim i As Integer
   
    For i = 1 To oTB.Columns.Count
        oTitles(i) = oTB.Columns(i).Title
    Next

    
   
     ' Set the contents of the custom table (contents are set row-wise)
    Dim oContents(1 To 9) As String
    oContents(1) = "1 - New"
    oContents(2) = "1- New"
    oContents(3) = "Brass- New"
    oContents(4) = "2- New"
    oContents(5) = "2- New"
    oContents(6) = "Aluminium- New"
    oContents(7) = "3- New"
    oContents(8) = "1- New"
    oContents(9) = "Steel- New"
   
      ' Create new custom table
    Dim oTB_New As CustomTable
    Set oTB_New = oSheet.CustomTables.Add

                       (oTB.Title, oTB.Position, _
                       oTB.Columns.Count,               oTB.Rows.Count, oTitles, oContents)               
   
    'delete the old table
    oTB.Delete
    
End Sub


Viewing all articles
Browse latest Browse all 532