21, Feb, 2019

AutoCAD VBA – Create Table and insert data

autocad insert table vba

AutoCAD VBA – Create Table and add data

ADD TABLE @ X=400,  Y = 300

Call AddTable(400, 300)

Code Begins

‘———————-

Sub AddTable(ByVal x1 As Double, ByVal y1 As Double)

Dim pt(0 To 2) As Double
Dim acTable As AcadTable
pt(0) = x1: pt(1) = y1: pt(2) = 0
Set acTable = ThisDrawing.ActiveLayout.Block.AddTable(pt, 5, 3, 15, 80)

With acTable
.RegenerateTableSuppressed = True
.RecomputeTableBlock False
.TitleSuppressed = False
.HeaderSuppressed = False
Dim i As Double, j As Double
Dim col As New AcadAcCmColor
col.SetRGB 255, 255, 255

‘title
.SetCellTextHeight i, j, 6.4
.SetCellAlignment i, j, acMiddleCenter
col.SetRGB 255, 255, 255
.SetCellBackgroundColor i, j, col
col.SetRGB 255, 255, 255
.SetCellContentColor i, j, col
.SetCellType i, j, acTextCell
.SetText 0, 0, “Table Title”

i = i + 1

‘headers
For j = 0 To .Columns – 1
col.SetRGB 255, 255, 255
‘.SetCellBackgroundColor i, j, col
col.SetRGB 255, 255, 255
.SetCellContentColor i, j, col
.SetCellTextHeight i, j, 5.2
.SetCellAlignment i, j, acMiddleCenter
.SetCellType i, j, acTextCell
.SetText i, j, “Header” & CStr(j + 1)
Next

‘data rows
For i = 2 To .Rows – 1
For j = 0 To .Columns – 1

.SetCellTextHeight i, j, 4.5
.SetCellAlignment i, j, acMiddleCenter
If i Mod 2 = 0 Then
col.SetRGB 255, 255, 255
Else
col.SetRGB 255, 255, 255
End If
‘.SetCellBackgroundColor i, j, col
col.SetRGB 255, 255, 255
.SetCellContentColor i, j, col
.SetCellType i, j, acTextCell
.SetText i, j, “Row” & CStr(i – 1) & ” Column” & CStr(j + 1)

Next j
Next i

.RegenerateTableSuppressed = False
.RecomputeTableBlock True
.Update
ZoomAll

Set col = Nothing

End With

End Sub

Excel AutoCAD VBA sample codes

insert table with VBA in AutoCAD

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>