Using Visual Basic for Applications
In the following exercises, you will use Visual Basic for Applications with Word, and with Excel.
* To use VBA with Microsoft Word
- Start Microsoft Word and open the VBA.doc file in WA\Practice\Ch04 directory.
- If prompted, click Enable Macros .
- From the View menu, point to Toolbars and then click Visual Basic .
- From the toolbar, click Design Mode .
- Place the insertion point at the bottom of the document.
- From the Control toolbox, click the Command Button icon.
- Right-click the Command button and click Properties .
- Change the Caption property of the Command button to Calculate .
- Close the Properties dialog.
- Double-click the Command button to open the VBA code window.
- In the click event of CommandButton1 , type the following code to calculate a monthly payment based on loan information.
On Error GoTo ErrorHandler
If APR > 1 Then APR = APR / 100 ' Ensure proper form.
If chkEOM = True Then
PayType = 0
Else
PayType = 1
End If
lblMOpayment = Format(Pmt(APR / 12, TotPmts, -PVal, 0, PayType), "currency")
Exit Sub
ErrorHandler:
lblMOpayment = 0
- Close the Visual Basic Editor.
- Switch to the Word Document.
- From the toolbar, click Exit Design Mode .
At this point, you can enter in values for each field.
- Enter in values for each field and click Calculate .
- Save changes to the document and exit Word.
* To use VBA with Microsoft Excel
- Start Microsoft Excel.
- From the View menu, select Toolbars , then click Visual Basic .
- From the Visual Basic toolbar, click Design Mode .
- From the Visual Basic toolbar, click Control Toolbox .
- Add a command button control to the Excel spreadsheet.
- Double-click on CommandButton1 to open the VBA editor.
- In the Click event of the command button, type the following code:
Cells.Range("A1") = "Hello World!"
- Click Exit Design Mode on the Visual Basic toolbar.
- Click CommandButton1 in Microsoft Excel.
"Hello World!" will be displayed in the first cell of the spreadsheet.
Lesson Summary
Visual Studio 97 is a comprehensive set of visual development tools for creating various types of applications and components . Many of the tools perform similar functions. Select the appropriate tool to use based upon your desired deliverables. The tools in the suite include:
- Visual Basic
- Visual C++
- Visual InterDev
- Visual J++
- Visual FoxPro
Microsoft Office is a suite of applications for developing business solutions. The applications include:
- Word
- Excel
- Powerpoint
- Outlook
- Access
Visual Basic for Applications (VBA) is a new version of Visual Basic included in Office that allows you to create customized Office solutions.