Add Text In AutoCAD Using Excel & VBA

Share this

March 13, 2014

Add Text In AutoCAD Using Excel VBA


The previous week I published a VBA code for drawing circles in AutoCAD using data from an Excel workbook. Since I received several email requests from various blog readers, I decided to continue the subject of AutoCAD automation from Excel. So, below you will find a VBA code that will help you “send” text strings from Excel to a specific location in an AutoCAD drawing. The code might be extremely useful for filling the information in a drawing legend (watch the demonstration video for example).

The code is based on the AddText method. According to AutoCAD VBA help, the structure of this method is the following:

RetVal = object.AddText(TextString, InsertionPoint, Height)

RetVal: Text object. The newly created Text object.
Object: ModelSpace Collection, PaperSpace Collection, Block – the objects this method applies to.
TextString: String; input-only. The actual text to be displayed.
InsertionPoint: Variant (three-element array of doubles); input-only. The 3D WCS coordinates on the drawing where the text is placed.
Height: Double; input-only. The height of the text. It must be a positive number.

The sample workbook that you will find in the Downloads section below requires three main user inputs (as the AddText method), as well as four optional: the coordinates of the insertion point (in X, Y, Z) – in other words, where you want the newly created text to be inserted, the text height (like font size) and the text message that should be displayed in the drawing. Then, by clicking the “Add Text” button the text is inserted either in the active drawing (if AutoCAD is already lunched), or in a newly created drawing. The optional parameters give additional functionality, so you can adjust the alignment, the rotation angle, the width factor and the color of the text to be inserted.


VBA code to add text in AutoCAD from Excel

Similar to the circle case, almost half of the code is used to initialize the AutoCAD object, as well as the active/new drawing object.

Option Explicit
Sub AddText()
    'This macro adds text in AutoCAD using data - insertion point, text height and text message - from Excel.
    'Moreover, it provides some optional parameters, so that the user can adjust the alignment, the rotation angle,
    'the width factor and the color of the text to be inserted (using the Red, Green and Blue parameters).
    'The code uses late binding, so no reference to external AutoCAD (type) library is required.
    'It goes without saying that AutoCAD must be installed at your computer before running this code.
    'Written By:    Christos Samaras
    'Date:          07/03/2014
    'Last Update:   16/03/2015
    'E-mail:        [email protected]
    'Declaring the necessary variables.
    Dim acadApp                 As Object
    Dim acadDoc                 As Object
    Dim acadText                As Object
    Dim acadColor               As Object
    Dim LastRow                 As Long
    Dim i                       As Long
    Dim InsertionPoint(0 To 2)  As Double
    Dim ZeroPoint(0 To 2)       As Double
    'Activate the coordinates sheet and find the last row.
    With Sheets("Coordinates")
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    'Check if there are coordinates for at least one text message.
    If LastRow < 2 Then
        MsgBox "There are no coordinates for the insertion point!", vbCritical, "Insertion Point Error"
        Exit Sub
    End If
    'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
    'Check (again) if there is an AutoCAD object.
    If acadApp Is Nothing Then
        MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
        Exit Sub
    End If
    On Error GoTo 0
    'If there is no active drawing create a new one.
    On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add
    End If
    On Error GoTo 0
    'Check if the active space is paper space and change it to model space.
    If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
        acadDoc.ActiveSpace = 1     '1 = acModelSpace in early binding
    End If
    'Set the AcCmColor object (here acadColor) which represents colors.
    Set acadColor = acadApp.GetInterfaceObject("AutoCAD.AcCmColor." & Left(acadApp.Version, 2))
    'The point at the beginning of 3 axes.
    ZeroPoint(0) = 0
    ZeroPoint(1) = 0
    ZeroPoint(2) = 0
    'Loop through all the rows and add the corresponding text in AutoCAD.
    With Sheets("Coordinates")
        For i = 2 To LastRow
            'If the height and the message are not empty, add the text.
            If IsEmpty(.Range("D" & i)) = False And IsEmpty(.Range("E" & i)) = False Then
                'Set the insertion point.
                InsertionPoint(0) = .Range("A" & i).Value
                InsertionPoint(1) = .Range("B" & i).Value
                InsertionPoint(2) = .Range("C" & i).Value
                'Add the text.
                Set acadText = acadDoc.ModelSpace.AddText(.Range("E" & i), InsertionPoint, .Range("D" & i))
                'Align the text based on alignment selection. The default is left.
                If IsEmpty(.Range("F" & i)) = False Then
                    If UCase(.Range("F" & i)) = "CENTER" Then
                        acadText.Alignment = 1
                        acadText.Move ZeroPoint, InsertionPoint
                    End If
                    If UCase(.Range("F" & i)) = "RIGHT" Then
                        acadText.Alignment = 2
                        acadText.Move ZeroPoint, InsertionPoint
                    End If
                End If
                'If the rotation angle is not empty, rotate the text.
                '0.0174532925 is used to convert from degrees to radians.
                If IsEmpty(.Range("G" & i)) = False Then acadText.Rotate InsertionPoint, .Range("G" & i).Value * 0.0174532925
                'If the width factor is not empty, apply the width factor.
                If IsEmpty(.Range("H" & i)) = False Then acadText.ScaleFactor = .Range("H" & i)
                'Use the sheet data to change the color on each line of text (if the corresponding cells are not empty).
                If IsEmpty(.Range("I" & i)) = False And IsEmpty(.Range("J" & i)) = False And IsEmpty(.Range("K" & i)) = False Then
                    Call acadColor.SetRGB(.Range("I" & i), .Range("J" & i), .Range("K" & i))
                    'Change the color of text.
                    acadText.TrueColor = acadColor
                End If
            End If
        Next i
    End With
    'Zoom in to the drawing area.
    'Release the objects.
    Set acadText = Nothing
    Set acadDoc = Nothing
    Set acadApp = Nothing
    'Inform the user about the process.
    MsgBox "The text was successfully added in AutoCAD!", vbInformation, "Finished"
End Sub 

Note that if you have AutoCAD 2010 or a newer version, you will have to download and install the VBA module, otherwise the code will probably fail.

All links were copied from Autodesk‘s website.

Demonstration video

The short video below demonstrates the result of the above VBA code. Seven lines of text are inserted in the legend of this sample drawing. Each line has a different color, scale factor, and alignment, while the rotation angle for all lines was set to zero.




The file can be opened with Excel 2007 or newer. Please enable macros before using it.


Read also

Drawing Circles In AutoCAD Using Excel & VBA
Insert Blocks In AutoCAD Using Excel & VBA
Drawing Points In AutoCAD Using Excel & VBA
Send AutoCAD Commands From Excel & VBA

Page last modified: October 1, 2021

Christos Samaras

Hi, I am Christos, a Mechanical Engineer by profession (Ph.D.) and a Software Developer by obsession (10+ years of experience)! I founded this site back in 2011 intending to provide solutions to various engineering and programming problems.

  • FirstVBAtoCAD says:


    Can you help me?

    ๊Use VBA Create Ordinate Dimention Every station in CAD

    In picture

    Left DATA of Corssection road way

    (a,1) offset wcs (b,1) Station

    (a,2) offset centerline(roadway) (b,2) elevation

    sorry for my English

  • Sorry my friend, but I cannot understand what you are trying to do.
    I suppose that on columns you have coordinates, but still I don’t know what you want to achieve…

  • Hi, CH,

    If you put the alignment left or leave it empty it should add the text with the correct z point.
    I hope it helps!

    Best Regards,

  • Jayagurunathan S says:

    Hello Christos, I m jayaguru from india. Thanks for sharing the XLS and your site also looks good … Its great learning for me… Can u help me to improve my idea ? The idea is to insert blocks with attributes in autocad by using excel VBA

  • Hi, Jayagurunathan,

    The functionality to edit attributes is something that I have postponed for quite a while (I am working on it).
    I will release it at some point but as a commercial workbook/add-in).
    It’s not as straightforward as this code.

    Best Regards,

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    Add Content Block