A picture of a keypad key that says Help!

VBA – Tutorial 1

A picture of Mustang Sally, my dog and friend.

Learning How to insert an external block onscreen using a VBA form.


If you are using Autocad 2010 then you should have downloaded and installed the VBA updater from Autodesk.

I wanted to wait until I had some screenshots to show, but evidently, everyone who is familiar at all with VBA for Autocad knows what the screen looks like in the VBA environment.  It is fairly plain and straightforward.  However, when I can get everything setup, I will throw a screenshot in when I feel it is necessary.  Besides, most of this is classic VB6 and, outside of the slightly different IDE of VB6, the code is the same.  If you are new to coding then I reccomend that you read the VB6 tutorials that are located from my Home page|VB6 Tutorials Home.

So what are we going to do in our first VBA lesson?  I thought maybe we would start with a userform and a button control.  I think that one of the most often asked questions that I see on the forums is how to select onscreen from a VBA form control.  We will insert an external block using the form code, which is also very popular chatter on the forums.  I don't think I have seen the method that I use specifically though.  I believe that my way is very efficient and to the point. 

Let's get started!

First start a new macro by selecting from the menu Tools|Macro|Visual Basic Editor.

In the editor environment right click on the "Thisdrawing" element on the left side and select Insert|userform.  If the Toolbox isn't visible after you insert the form, go to the menu View|Toolbox.  The Toolbox should show up so that you can click on a button control and drop it on the form.

Now that we have a form with a button control, click on the button control once and the properties window (usually on the lower left side) will reveal all of the properies for the button.  Click in the Label property and type "Insert An External Block".  You may have to resize the button for all of the text to fit in.

Next, start coding the button by double-clicking directly on the button.  This will open the code window for the button click event.  The code that we write is what is going to occur when someone clicks this button (in other words).

We are going to declare a couple of variables to use in this procedure.

Dim startPnt As Variant
Dim prompt1 As String

We need the Variant type for the start point to get the onscreen coordinates of where the block will be placed on the screen.  The prompt variable is what will appear on the command line when we are placing our block, so it is just the String type.

Next we need to get the block file that we want to place in our drawing.  This file needs to be created the usual way in which you would create a block template file.  If you don't know how to do that, you ARE an Autocad newbie.  There are plenty of sites online for researching how to do stuff like that, but maybe, one day I will cover some of the basics like that in a tutorial.

As most of you should know, "ME" always refers to the containing form.  We need to hide the form to get at the active drawing screen.The vbCRLF is a built in function in VBA and it provides linefeed/carriage return.  It works well for string expressions where you need this.  We need it in the beginning to tell Autocad to proceed with the command.  The "Getpoint" method allows you to pick a point on the screen, which stores the coordinates in our variable "startPnt".  When we use the "Inserblock" method, as you can see, the block is inserted at the stored coordinate via our variable.  The rest of the stuff in the method is where to get the file and where to locate the block in question, in this case, we are using defaults, which work fine.  Be sure to replace all of the "YOUR" stuff with actual location information. Afterwards, we activate the drawing to allow the block to insert.  Last, we show the form again.

Press F5 to run the macro, click on the point where you want your block. After the form shows itself again, at this point, you can close the macro and go back to your drawing, and, voila!, there is your block placed right where you clicked.

The original drawing items may not have been a block, but it is in this drawing.  If you type "block" on the command line, you will see the name of your drawing in the block list, as a block.  You can treat this as you do any other block that  you use.  If you have setup attributes in your block drawing, you will be able to edit them in the normal way.  I don't know exactly what YOU would use this for, but I use it to place my title blocks.  My form also has other stuff on it for utility purposes, such as text boxes to fill in the attributes for my title blocks, a print all sheets button and a fill in the BOM block with BOM information from Excel.  Makes my job easier and faster.

Be sure you save the VBA file with a name that means something to you when you exit Autocad.  Autocad will prompt you to save the file automatically.  Autocad will save it with the "dvb" extension (unless you are using a much earlier version of Autocad than 2007.  As I said earlier, I am using 2010.)  You can have Autocad automatically load the file when you start Autocad.  Email me if you want ot know how to do this.

I hope this was a good start to VBA!  Send me an email from my Contact link above if you have questions or would like to see some other tutorial for my next page project.

A logo of Ardent Realty Group.

A picture of a young girl using an abacus.