Here's a modified version of the code that works.
The issue in your code is that when you create a new TableFormat object most
if it is unitialized, indicating those properties have not been set.
Because of this the TextStyle property of the TableFormat object will not
return a TextStyle. That's why it's failing. Instead, you need to use an
existing text style, or create a new one and use it to assign to the
TableFormat object. The code below does this.
Public Sub CreateCustomTable()
' 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
' Set the column titles
Dim oTitles(1 To 3) As String
oTitles(1) = "Part Number"
oTitles(2) = "Quantity"
oTitles(3) = "Material"
' Set the contents of the custom table (contents are set row-wise)
Dim oContents(1 To 9) As String
oContents(1) = "1"
oContents(2) = "1"
oContents(3) = "Brass"
oContents(4) = "2"
oContents(5) = "2"
oContents(6) = "Aluminium"
oContents(7) = "3"
oContents(8) = "1"
oContents(9) = "Steel"
' Set the column widths (defaults to the column title width if not
specified)
Dim oColumnWidths(1 To 3) As Double
oColumnWidths(1) = 2.5
oColumnWidths(2) = 2.5
oColumnWidths(3) = 4
Dim oRowHeights(1 To 3) As Double
oRowHeights(1) = 0.25
oRowHeights(2) = 0.25
oRowHeights(3) = 0.25
' Create the custom table
Dim oCustomTable As CustomTable
Set oCustomTable = oSheet.CustomTables.Add("My Table",
ThisApplication.TransientGeometry.CreatePoint2d(15, 15), _
3, 3, oTitles, oContents,
oColumnWidths, oRowHeights)
' Change the 3rd column to be left justified.
oCustomTable.Columns.Item(3).ValueHorizontalJustification =
kAlignTextLeft
' Create a new text style by copying the current one used for the text
box.
Dim oTextStyle As TextStyle
Set oTextStyle = oCustomTable.DataTextStyle.Copy("sCopy of " &
oCustomTable.DataTextStyle.Name)
' Set the font size for the new style
oTextStyle.FontSize = 2
' Create a table format object
Dim oFormat As TableFormat
Set oFormat = oSheet.CustomTables.CreateTableFormat
' Set some of the properties of the table format.
oFormat.OutsideLineWeight = 0.05
oFormat.TextStyle = oTextStyle
' Modify the table formats
oCustomTable.OverrideFormat = oFormat
End Sub
--
Brian Ekins
Autodesk Inventor API