Now that you have learned the basics of programming in AutoCAD VBA, let's try creating a simple “Hello World” exercise. In this exercise you will create a new AutoCAD drawing, add a line of text to that drawing, then save the drawing, all from VBA.
To create the “Hello World” text object
Command: VBAIDE
ThisDrawing.Application.Documents.Add
Dim insPoint(0 To 2) As Double 'Declare insertion point
Dim textHeight As Double 'Declare text height
Dim textStr As String 'Declare text string
Dim textObj As AcadText 'Declare text object
insPoint(0) = 2 'Set insertion point x coordinate
insPoint(1) = 4 'Set insertion point y coordinate
insPoint(2) = 0 'Set insertion point z coordinate
textHeight = 1 'Set text height to 1.0
textStr = "Hello World!" 'Set the text string
'Create the Text object
Set textObj = ThisDrawing.ModelSpace.AddText _
(textStr, insPoint, textHeight)
ThisDrawing.SaveAs("Hello.dwg")
When the program finishes running, bring the AutoCAD application to the front. You should see your text “Hello World!” visible in your drawing. The drawing name should be Hello.dwg.