Working with Code Window
As you become proficient with VBA, you spend a lot of time with working in the Code window. Macros that you record are stored in a module, and you can type a VBA code directly into a VBA module.Minimize and maximize Code windows
If you have several projects open, the VBE may have lots of Code window at any given time. Below figure shows an example of this.Code windows are much like your files opened in your CAD program. You can minimize them, maximize them, resize them, and hide them and so on. Most people find it much easier to maximize the code window that they are working on. Doing so lets you see more code and keeps you from getting distracted.
To maximize the Code window, click the maximize button in its title bar or just double-click the title bar of Code window to maximize it. To restore a window to its original size, click the restore button. When the Code window is maximized, its title bar is not visible so you will find the restore below the VBE title bar.
Sometimes you want to have two or more Code windows visible. For example, you want to compare the code in the two modules or copy the code from one module to another. You can arrange the windows manually, or use the "Window ⇨ Tile Horizontally" or "Window ⇨ Tile Vertically" command to arrange them automatically.
You can quickly switch among code window by pressing Ctrl + TAB. If you repeat that key combination, you keep cycling through all the open code windows. Pressing Ctrl + Shift + TAB cycles through the windows in reverse order.
Minimizing a window gets it out of the way. You can also click the window close button in a Code window’s title bar to close the window completely. (Closing a window just hides it; you won’t lose anything.) To open it again, just double-click the appropriate object in the Project window. Working with these Code windows sounds difficult than it really is.
Creating a Module
In general, a VBA module can hold three types of code:- Declaration: One or more information statement that you provide to VBA. For example, you can declare the data type for variables you plan to use, or set some other module-wide options.
- Sub-procedures: A set of programming instructions that performs some actions.
- Function procedures: A set of programming instructions that returns a single value.
How you organize a VBA module is totally up to you. Some people prefer to keep all their VBA code in a single module, others (like me) like to split up the code into several different modules or even classes. It is a personal choice like arranging furniture.
Inserting VBA code into a Module
An empty VBA module is like a fake food you see in the advertisements. It looks good but it does not really do much. Before you can do anything meaningful, you must have some VBA code in the VBA module. You can insert VBA code into VBA module in three ways:- Insert code directly into the code window.
- Use the macro recorder to record your actions and convert them into VBA code.
- Copy the code from one module and paste it into another module.
Entering code directly into a module
Sometimes the best route is the most direct one. Entering the code directly involves, typing the code via your keyboard. Entering and editing a text in a VBA module works as you might expect. You can select, copy, cut, paste and do other things as you do in other word processing software.Use the TAB key to indent some of the lines to make your code easier to read. This is not necessary, but it is a good habit to acquire. As you go through you will understand why indenting code lines is helpful.
A single line of VBA code can be as long as you like. However, you may want to use the “line-continuation” character to break up the long line of code. To continue a single line of code (also known as a statement) from one line to next, end the first line with a space followed by an underscore (_). Then continue the statement in the next line. Below is given the example of a single statement split into two lines:
Example
set swPart = swApp.NewDocument(swApp.GetUserPreferenceStringValue _
(swUserPreferenceStringValue_e.swDefaultTemplatePart),0,0,0)
(If you are wondering what above statement does, then the answer is above statement open a new part with default part template in SolidWorks. This code is not inserted using macro recorder, instead, I write it manually to find a default part template and use that template to open a new part.)
The engineers who designed VBE knew that people like us would be making mistakes. Therefore, the VBE has multiple levels of Undo and Redo. If you deleted a statement that you should not have, use the Undo button on the toolbar (or press Ctrl+Z) until the statement shows up. After undoing, you can use the Redo button to perform the changes you have undone. This redo/undo stuff is much like you use in other software. Until you use it, you cannot understand.
Ready to enter some live code, try the following steps:
- Go to your VBE.
- Double-click your module if it is not opened.
- Go to COde Window.
- Type the following code into Code window:
- Make sure the cursor is located anywhere within the text you typed, and then press F5 to execute the procedure.
Example
Sub GuessName()
Msg = “Is this a CAD Software?”
Ans = MsgBox(Msg, vbYesNo)
If Ans = vbNo Then MsgBox “Oh, that’s fine.”
If Ans = vbYes Then MsgBox “You must be joking!”
End Sub
When you enter the code listed in step 4, you might notice that the VBE makes some adjustments to the text you enter. For example, after you type the sub-statement, the VBE automatically insert the End Sub statement, and if omit the space before or after an equal sign, the VBE insert the space for you. Also, the VBE changes the color and capitalization of some text. This is all perfectly normal. It is just VBE’s way of keeping things neat and readable.
If you followed the previous steps, you just wrote a VBA Sub procedure, also known as a macro. When you press F5, VBE executes the code and follows the instructions. In other words, VBE evaluates each statement and does what you told it to do.
This simple macro uses the following concepts:
- Defining a Sub procedure (the first line).
- Assigning values to variables (Msg and Ans).
- Using a built-in VBA function (MsgBox).
- Using built-in VBA constants (vbYesNo, vbNo, and vbYes).
- Using an If-Then construct (twice).
- Ending a Sub procedure (the last line)
Using the Macro recorder
Another way you can get code into a VBA module is by recording your actions, using the inbuilt macro recorder.By the way, there is absolutely no way you can record the “GuessName” procedure shown in the preceding section. You can record only things that you can do directly in your CAD application. Displaying a message box is not an application's normal repertoire. The macro recorder is useful, but in many cases, you’ll probably need to enter at least some code manually.
We have already seen how macros are recorded. So there is no need for us to go twice for the same thing. If you want to see how it is done go to Open VBA in Solidworks topic.
No comments:
Post a Comment