My first VBA application in AutoCAD
Print This Post
![]() |
Author | Özgür Özkurt |
| Profession | Mechanical Engineer | |
| Software Programmer | ||
| Location | Istanbul, TURKEY | |
AutoCAD 13 & higher versions
There is no doubt that the most important difference of Autocad from any other CAD software is its various tools of customization and automation. Some macro programmes can be written and managed in Autocad to ease the routine works. Although VBA (Visual Basic Application) has the whole control, loop etc. mechanisms of Basic Language, it lacks some options compared to the other versions of Visual Basic. The one who knows even a little about Basic Language and/or uses one of Visual Basic IDE can get used to VBA for Autocad very easily.
VBA.IDE that is integrated in Autocad can be opened by clicking the ‘Visual Basic Editor’ in macro line in Tools menu. If ‘Toolbox’ does not exist, just select it form the ‘View’ menu.

Figure.1
The Toolbox Menu icludes the controls the interfaces of the macros that you prepare.

Figure.2
A new form will be added when you apply the steps seen in Figure.2. The desired control is selected by clicking in the “Toolbox Menu” and it is placed by clicking the appropriate button in the form.
Let us attach a Command Button, two Text Box and a Label to our form by the method shown in Figure.1.
![]()
Figure.3
Change the box as ‘Sum‘ that corresponds to ‘Caption’ box in the ‘Properties’ window by selecting the CommandButton of the form we have added. Set the ‘TextAlign’ property in the same window as it is in the figure below.
![]()
Figure.4

Figure.5
We have just prepared the form that we will use. Next step is assigning an event to the Button.
Double click the button in the form. The code page and the lines below should be opened.
Private Sub CommandButton1_Click()
'''your code here
End Sub
Add the
Label1.Caption = Val(TextBox1.Text) + Val(TextBox2.Text)
Code between the lines above. Now we can run the macro. It can be done by selecting the ‘Run Sub/UserForm’ in the Run Menu.

Figure.6
The sum of the numbers are printed automatically in the label control when you write some numbers into the two Text Box and click the button.
Label1.Caption
expression means the caption property of the label control that we have just changed in the Properties window.
TextBox1.Text
expression means the Text property of the TextBox1. By means of the Val() function, the content is changed into a numerical value.
The same process is done for the TextBox2. They are summed and transferred to the caption property of the + label control.
VBA Mathematical Operations and Process Priorities
^ Exponential operator.
* Multiplication operator.
/ Real division operator. (Ex:5/2=2.5)
\ Integer division operator. (Ex:5\2=2)
Mod Mod operator. (Ex:5 Mod 2)
+ Summation operator.
- Subtraction operator.
We gave general information about the usage of VBA in Autocad and explained how a VBA macro is run. Besides, some basic properties of Visual Basic Language was studied. But this is not the limit of the usage of VBA for Autocad and OLE (ActiveX, COM). We will study this subject in more detail in coming lessons.
Regards.
Read in other languages: TR
Technorati Tags: AutoCAD, VBA for AutoCAD, Daily AutoCAD
Similar Posts
Rate this article:


November 27th, 2007 12:02
Microsft has said that it will no longer continue developing VBA - if you are starting a language it’s a good idea to look at VB.Net or C#.Net because they have a brighter future.
http://www.3DCADMax.com
January 3rd, 2008 16:49
I work with a company that uses metric data, but wants it displayed imperical. We have to do the conversion from imperical to metric. I would like a script that will do this for me as I am entering the imperical number on the command line.
Thanks
March 25th, 2008 12:52
I just learnt ACAD… i have some programming knowledge but i dont know how to start programming in ACAD… what should i keep in mind and what should i practice on…..???
please do give me some tips
Regards
Imran
May 31st, 2008 16:35
Ghassan mansour
I just learnt ACAD… i have some programming knowledge but i dont know how to start programming in ACAD… what should i keep in mind and what should i practice on…..???
please do give me some tips
Regards
ghassan mansour
June 24th, 2008 12:40
Yeah it is fine, but how it is going to be useful for autocad.
I need to do programming for autocad using vb6.0 and i need code for creation of autocad files