VBA Primer - Part 1
I have had numerous requests from people asking for a tutorial on Visual Basic for Applications. Well here it is. This tutorial will take you from the very basics of opening the Visual Basic Editor to building a simple application. I must stress though, that Visual Basic is a language with thousands of built in functions and numerous custom modules and controls available. I strongly recommend that you invest in a good Visual Basic book as I cannot possibly take you through the whole VB language. Also, remember that AutoCAD Visual Basic for Applications is a subset of the Visual Basic language with it's own build in functions and controls. Anyway, enough warbling for now, let's get started with our application.
This application called, Change Layer, will allow you to change the layer of any entity within your drawing. It will display a dialogue box with a list box listing all of the available layers in your drawing. After you have selected a layer to change to, it will prompt you to select the entities you would like to change to that layer and then change them. Sound's good hey? Here we go…
Fire up AutoCAD and choosefrom the pull-down menu (you may need to download and install the VB Editor). The Visual Basic Editor should appear and will look like this:
Don't worry about the various windows that appear in the editor. We will go through each one as we progress through this tutorial.
Now choosefrom the pull down menu bar. The following should appear :
This is the form that we will use to create our dialogue box from. Now look in the top left hand corner of the screen. There you will find the Project dialogue. This basically lists all the Forms, Modules, etc. that are part of your VBA project. Currently, there is just one form called "UserForm1".
As you can see, because we haven't named our project yet, AutoCAD gives our project the name Global1. Before we go any further let's save our project and give it a name. Selectfrom the menu bar and name the project Chlayer.dvb. Also you will be able to see the form that we created listed under Forms and given the default name of UserForm1. All forms, modules and controls that you insert into your project will be given a default name. (You can, if you wish change it later but, in this exercise we will only work with the default names.)
Now, let's have a look at the Properties dialogue box. This you will find in the lower left-hand corner of the Editor and should look like this:
This dialogue lists all the properties that an object has. The dialogue should show all the properties available for the form, UserForm1 that you inserted. If it doesn't, click on the form to select it.
The first property listed is (Name) - UserForm1. This is the property that you would change if you wanted to change the name of the form. Please remember, that this is the name that you use to programatically refer to this particular form. If you change this name once you have written code referring to this form, you will have to manually change all the instances of that name in your code.
Let's change the caption of the form. Select the caption property and change it from UserForm1 to Change Layer. Your form should now look like this :
Notice how the caption has changed. In Part 2 we will have a look at adding controls to our form. See you there…