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.
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:
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()
(oTB.Title, oTB.Position, _ |