21-11-2019, 05:38 AM
AutoCAD Drawing automation techniques using VBA (Visual Basic for Applications)
AutoCAD VBA Introduction
VBA Stands for Visual Basic for Applications, where program snippets used to control the applications instead of commands/controls. You can use VBA Editor to add new codes, modify existing codes. All codes are written in familiar programming language VB and since this website deals with VBA for AutoCAD, Excel and Solidworks, it is recommended that you refer VB code tutorials (there are many, available online) before using this website.
VBA Editor comes with AutoCAD 2007 version as preinstalled, if you are using later versions; you should install VBA editor before beginning the projects.
To open VBA Editor, go to Tools -> Macro -> Visual Basic Editor as shown below
Or simply press ALT + F11
![[Image: 1.JPG]](https://1.bp.blogspot.com/-v7AkLR1RhFo/XdYiQHIIFwI/AAAAAAAAAkY/oQHs0ZNS5cwVTOljZ0DxxALp8qZTATanACLcBGAsYHQ/s1600/1.JPG)
The newly opened window should be something like this,
![[Image: 2.JPG]](https://1.bp.blogspot.com/-vMKMOYzisis/XdYiQELa1HI/AAAAAAAAAkg/rOB2ALI7M7wUd_-wYYRfOpEbcnrRNnklgCLcBGAsYHQ/s1600/2.JPG)
To begin with your first vba macro, go to Insert -> Module
![[Image: 3.JPG]](https://1.bp.blogspot.com/-s7OKEvW3HqM/XdYiQIRj37I/AAAAAAAAAkc/WE73ap1N_Vc6AkRtIFR-7Bx_EBugfFCOQCLcBGAsYHQ/s1600/3.JPG)
Then type Sub module1 () and press Enter (module1 is the name of your new module, it can be anything like drawline, yourname etc without any spaces)
As you can see that “End Sub” has been appeared just below Sub module1 (), thus defining the boundary of your AutoCAD macro.
Now, you can start typing your codes between module name and end sub, ie, Sub module1 () and End Sub, as shown below
Sub module1()
'your codes goes here
'your codes goes here
'your codes goes here
End Sub
AutoCAD VBA Introduction
VBA Stands for Visual Basic for Applications, where program snippets used to control the applications instead of commands/controls. You can use VBA Editor to add new codes, modify existing codes. All codes are written in familiar programming language VB and since this website deals with VBA for AutoCAD, Excel and Solidworks, it is recommended that you refer VB code tutorials (there are many, available online) before using this website.
VBA Editor comes with AutoCAD 2007 version as preinstalled, if you are using later versions; you should install VBA editor before beginning the projects.
To open VBA Editor, go to Tools -> Macro -> Visual Basic Editor as shown below
Or simply press ALT + F11
The newly opened window should be something like this,
To begin with your first vba macro, go to Insert -> Module
Then type Sub module1 () and press Enter (module1 is the name of your new module, it can be anything like drawline, yourname etc without any spaces)
As you can see that “End Sub” has been appeared just below Sub module1 (), thus defining the boundary of your AutoCAD macro.
Now, you can start typing your codes between module name and end sub, ie, Sub module1 () and End Sub, as shown below
Sub module1()
'your codes goes here
'your codes goes here
'your codes goes here
End Sub