CAD programming: Using macros and VBA


20 October 2021

By: Andrew G. Roe

Macros and VBA provide an easy way for users to get familiar with programming.

Many CAD programs (and others for that matter) save you a considerable amount of time by using macros to automate repetitive tasks. In this column, we will focus on MicroStation PowerDraft with the aim of providing a clear example. If you are using another CAD program, it may offer a similar macro recording tool that allows you to record multiple steps and play them back to complete the tasks in one step, all without typing any code. Read this column for an overview of the macros, then check the specifics of your CAD package. For more general details on VBA, read here.

Macros are one of the many tools available if you want to customize or program in a CAD environment. (See CAD Programming: Introduction for 2021 for an overview of other options.) You don’t need to know programming to use macros, but they can be a great introduction to programming. In fact, MicroStation allows you to convert a recorded macro into a Visual Basic for Applications (VBA) project for further editing and customization. More on that later.

For starters, let’s say you want to automate the insertion of text into your drawings. It could be a particular note or tag that you use over and over again. Instead of typing the note each time or copying and pasting the text, you can create a simple macro to accomplish the task with the following steps.

Record macro

  1. With MicroStation running and a blank drawing open, click the Utilities tab to display the Macro window.

  2. Click on Save to start recording your macro.

  3. Click it Annotate tab and select the Place text tool.

  4. In the Text editor window, type some text, for example “This is a note for my drawing”.

  5. Click a location in the drawing to place the text.

  6. Right click to exit the command.

  7. Click it Utilities tab again and click Stop to stop recording.

  8. When prompted to record the macro, enter a name, such as “TextMacro” and click to safeguard.

Congratulations! You recorded a macro. You can now read it to see what you have done.

Play the macro

  1. Delete the text you placed earlier when recording the macro.

  2. In the Utilities tab, click To play.

  3. Display the text placed in the drawing.

If this is your first macro, you might be wondering what you’ve accomplished. You simply ran a MicroStation command from a macro instead of the normal menu command. The text is always placed in the same place, which does not add much value yet. You can increase the value by customizing the macro so that you can select a location. To do this, use a MicroStation feature that allows you to promote a macro script to a VBA project for further editing and customization.

Promote the macro

  1. In the Utilities tab, click the pencil icon next to your macro’s name.

  2. In the window that appears, click on the Promote icon to promote your macro in VBA.

  3. In the Utilities tab, click the VBA Manager icon.

  4. In the VBA projects window, select your macro and make sure the Charge the box is checked.

  5. Click it Pencil to modify the VBA code of your macro.

  6. In the VBA window, find your macro by expanding the modules listed on the left side of the window.

  7. Double click on the macro module to view the code in the VBA window.

Under BmrTextMacro ()
Dim startPoint As Point3d
Dim point As Point3d, point2 As Point3d
Dim lngTemp as long
Dim oMessage As CadInputMessage
‘Send a keyin which can be a command string
CadInputQueue.SendKeyin “TEXTEDITOR PLACE”
CadInputQueue.SendKeyin “TEXTEDITOR PLAYCOMMAND INSERT_TEXT” “This is a note for my drawing.” “”
‘Coordinates are in principal units
startPoint.X = 5.85984028573692
startPoint.Y = 1.8561289704424
startPoint.Z = 0 #
‘Send a data point to the current command
point.X = startPoint.X
point.Y = startPoint.Y
point.Z = startPoint.Z
Point CadInputQueue.SendDataPoint, 1
‘Send a reset to the current command

End of the submarine

If that sounds like gibberish, don’t despair. You don’t really have to figure everything out just yet – just the part you want to customize.

Customize the macro

To make the macro more useful, let’s explore how to let the user determine the location of the text.

  1. In the VBA window, locate the three lines beginning with the term “starting point…These lines define the location of the text at the coordinates you selected when recording the macro.

  2. Insert an apostrophe () at the beginning of these lines. This converts those lines to comments which are not executed.

  3. Add a line just below the commented lines to prompt the user to select a point. (Your coordinate values ​​may differ from this example, depending on the location you chose.
    ‘startPoint.X = 5.85984028573692
    ‘startPoint.Y = 1.8561289704424
    ‘startPoint.Z = 0 #
    startPoint = CadInputQueue.GetInput (msdCadInputTypeDataPoint) .point

  4. Click it To play in the VBA window and notice how the program allows you to select a location for the text.

  5. Click it to safeguard icon to save your work in a VBA program.

Here are some brief highlights of the key lines of the macro. If you’ve been programming this should be familiar territory. Otherwise, take a deep breath for some VBA programming basics.

Lines starting with “Weak…”Are dimension statements that declare variables to be used in the macro, such as the startPoint variable which determines the location of the text. The term “CadQueueEntry …”Refers to an object in the MicroStation application programming interface (API) that can be manipulated with various methods and properties.

The methods used in this macro include “SendKeyin”, Which sends a command as if you were typing it on the keyboard, and“SendDataPoint”, Which sends information about the location identified by the variable called point.

Where to go next

If you are interested in learning more about macros and VBA programming in the MicroStation environment, Bentley has online resources. To get started, see: Developers and Programming – Bentley Communities. You can also try recording macros using other MicroStation commands. In general, most MicroStation tasks can be saved as macros and further customized with VBA code.

General information about VBA is widespread on the Internet and in many books. Because VBA was originally introduced by Microsoft for automating tasks in Microsoft Office, many of the concepts discussed in this article apply to automating tasks in Microsoft Word, Excel, and Access, as well as ‘AutoCAD and other VBA compatible products. See the following information on the Microsoft website: Office Visual Basic for Applications (VBA) Reference | Microsoft Docs and Find Help on Using the Visual Basic Editor (

Also note that VBA has some limitations and is not intended for large and complex programs. He also faces a somewhat uncertain future with some software vendors, but has managed to survive for many years after rumors of his demise first surfaced. For more serious programmers, Visual Studio.NET offers more versatility and more robust functionality, and also appears to have broad support for the foreseeable future.

For more in-depth MicroStation programming, Bentley’s MicroStation Development Libraries (MDL) provide additional power. APIs are available in native (C ++ and C) and managed (.NET) environments, providing access to the MicroStation object model and DGN design data.

In future articles, I will continue to explore other facets of CAD programming, including AutoCAD and MicroStation examples. If you want to suggest a topic, please send me an email.


Leave a Reply

Your email address will not be published.