📄 ch29.htm
字号:
' call number in active cell<br>
'<br>
Sub CallBtn()<br>
Dim x As Long ' for return<br>
Dim cPhone As String<br>
Dim cName As String<br>
'<br>
cName = ActiveCell<br>
cPhone = ActiveCell.Offset(0, 1)<br>
'<br>
x = tapiRequestMakeCall(cPhone, "", cName, "")<br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<p>You'll notice that this routine passes empty strings for the second and fourth
parameters of the API call. These are optional parameters and are not needed for our
example. You'll use these extra parameters in the Visual Basic 4.0 example later in this
chapter. Now all you need to do is lay out the worksheet page to contain the name/phone
number pairs. Figure 29.3 shows one way to lay out the form. </p>
<p><a HREF="f29-3.gif"><b>Figure 29.3 : </b><i>Laying out the QikDial worksheet </i></a></p>
<p>Notice that this worksheet contains a command button. Use the <tt><font FACE="Courier">Create
Button</font></tt> icon from the Forms toolbar to add the button to the worksheet. When
you are prompted to enter the macro associated with this button, be sure to enter the name
of the subroutine shown in Listing 29.5 (<tt><font FACE="Courier">CallBtn</font></tt>). It
doesn't matter where you place things on the form. Just be sure to arrange the Name and
Phone columns next to each other. The <tt><font FACE="Courier">CallBtn</font></tt>
subroutine will only work if the two columns are arranged side-by-side in the proper
order. This example also has some friendly instructions to help the first-time user. </p>
<p>Save the file as <tt><font FACE="Courier">QikDial.xls</font></tt> and then highlight a
name and press the <tt><font FACE="Courier">Call</font></tt> button. You should hear the
modem in your pc dialing the number and see a dialog box on your screen that looks like
the one in Figure 29.4. </p>
<p><a HREF="f29-4.gif"><b>Figure 29.4 : </b><i>Testing the QikDial spreadsheet </i></a></p>
<p>You now have a short set of code that can be added to any VBA-compatible program,
including Microsoft Project and Microsoft Access. In the next section, you'll create a
complete application in Visual Basic 4.0 that performs basically the same function. </p>
<h2><a NAME="WritingtheTeleBookApplicationUsingV"><font SIZE="5" COLOR="#FF0000">Writing
the </font><tt><font SIZE="5" COLOR="#FF0000" FACE="Courier">TeleBook</font></tt><font
SIZE="5" COLOR="#FF0000"> Application Using Visual Basic 4.0</font></a></h2>
<p><tt><font FACE="Courier">TeleBook</font></tt> is a sample telephone dialing application
that can be used to place voice calls using the modem attached to the pc. Once the phone
number has been dialed, a dialog box will appear telling the user to pick up the handset
and start talking. The user can then click on another button to hang up the call upon
completion. </p>
<p>This project has three forms, one class module, and one basic code module. The main
form contains a list box showing the list of all the people in the phone book and a set of
pushbuttons that mimic the keys on a standard single-line phone. A set of command buttons
appears at the bottom of the form to allow users to add, edit, delete, find, and dial
numbers selected from the phone list. Figure 29.5 shows how the form will look when it is
completed. </p>
<p><a HREF="f29-5.gif"><b>Figure 29.5 : </b><i>Laying out the TeleBook main form </i></a></p>
<p>The second form in the project is the phone list entry form. This form appears when the
user selects the add or edit buttons on the main form. This is where the user can add or
edit <tt><font FACE="Courier">TeleBook</font></tt> entries (see Figure 29.6). </p>
<p><a HREF="f29-6.gif"><b>Figure 29.6 : </b><i>Laying out the phone list entry form </i></a></p>
<p>The last form shows a log of all calls placed using the <tt><font FACE="Courier">TeleBook</font></tt>
application. <tt><font FACE="Courier">TeleBook</font></tt> saves all call data in a
Microsoft Access database for later use. The call history data can be shared with other
programs that are capable of reading Microsoft Access files, including Excel, Microsoft
Query, and several reporting tools. Figure 29.7 shows the <tt><font FACE="Courier">Call
Log</font></tt> form. </p>
<p><a HREF="f29-7.gif"><b>Figure 29.7 : </b><i>Laying out the Call Log form.</i></a> </p>
<p>Before laying out the forms, you first need to create the basic code module that
contains the TAPI declare statement and the support routines. You also need a class module
to encapsulate the API call into an easy-to-use method that allows four property settings.
</p>
<h3><a NAME="CreatingtheTeleBookClassModule">Creating the <tt><font SIZE="4"
FACE="Courier">TeleBook</font></tt><font SIZE="4"> Class Module</font></a></h3>
<p>Start a new VB4 project and insert a new module. Add the code shown in Listing 29.6.
This is the only Telephony API you need for the <tt><font FACE="Courier">TeleBook</font></tt>
application. </p>
<hr>
<blockquote>
<b><p>Listing 29.6. Adding the Telephony API call.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Option Explicit<br>
<br>
<br>
'<br>
' declare tapi-assist API<br>
'<br>
#If Win16 Then<br>
Declare Function tapiRequestMakeCall Lib "TAPI.DLL"
(ByVal lpszDestAddress As <font FACE="ZAPFDINGBATS">Â</font>String, ByVal
lpszAppName As String, ByVal lpszCalledParty As String, ByVal <font FACE="ZAPFDINGBATS">Â</font>lpszComment
As String) As Long<br>
#Else<br>
Declare Function tapiRequestMakeCall Lib "TAPI32.DLL"
(ByVal lpszDestAddress As <font FACE="ZAPFDINGBATS">Â</font>String, ByVal
lpszAppName As String, ByVal lpszCalledParty As String, ByVal <font FACE="ZAPFDINGBATS">Â</font>lpszComment
As String) As Long<br>
#End If</font></tt> </p>
</blockquote>
<hr>
<p>Notice that the code in Listing 29.3 uses the <tt><font FACE="Courier">#If...#Else...#End
If</font></tt> compiler directive. This is done to create a code piece that will compile
properly for both 16-bit and 32-bit environments. </p>
<p>Before continuing, save this module as <tt><font FACE="Courier">TBOOK0.BAS</font></tt>
and save the project as <tt><font FACE="Courier">TELEBOOK.VBP</font></tt>. </p>
<p>Now that the API declare is done, you are ready to build a class module that will
encapsulate the API. By writing a class module as a "wrapper" for the API,
you'll make your program easier to read and much easier to update in the future-especially
if the syntax of the API declare changes in the future. </p>
<p>Insert a class module into your VB project, set its <tt><font FACE="Courier">Name</font></tt>
property to <tt><font FACE="Courier">clsPhoneCall</font></tt>, and add the code shown in
listing 29.7. </p>
<hr>
<blockquote>
<b><p>Listing 29.7. Coding the <tt><font FACE="Courier">clsPhoneCall</font></tt> class
module.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Option Explicit<br>
<br>
Dim cDestAddress As String<br>
Dim cAppName As String<br>
Dim cCalledParty As String<br>
Dim cComment As String</font></tt> </p>
</blockquote>
<hr>
<p>The code in Listing 29.7 declares four variables used within the class module to keep
track of the property values set using the <tt><font FACE="Courier">Property Get</font></tt>
and <tt><font FACE="Courier">Property Let</font></tt> routines. Next you need to add these
routines. </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Warning</b></td>
</tr>
<tr>
<td><blockquote>
<p>Be sure to use the <tt><font FACE="Courier">Insert </font></tt>|<tt><font
FACE="Courier"> Property</font></tt> menu option from the Visual Basic main menu. If you
simply type <tt><font FACE="Courier">Property Get</font></tt> and <tt><font FACE="Courier">Property
Let</font></tt> in the module, it will not work properly. </p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>Listing 29.8 shows all four sets of <tt><font FACE="Courier">Property Get</font></tt>/<tt><font
FACE="Courier">Property Let</font></tt> statement pairs. Use this listing to build your
class module. </p>
<hr>
<blockquote>
<b><p>Listing 29.8. Coding the <tt><font FACE="Courier">clsPhoneCall</font></tt> property
routines.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Public Property Get DestAddress()<br>
DestAddress = cDestAddress<br>
End Property<br>
<br>
Public Property Let DestAddress(vNewValue)<br>
cDestAddress = vNewValue<br>
End Property<br>
<br>
Public Property Get AppName()<br>
AppName = cAppName<br>
End Property<br>
<br>
Public Property Let AppName(vNewValue)<br>
cAppName = vNewValue<br>
End Property<br>
<br>
Public Property Get CalledParty()<br>
CalledParty = cCalledParty<br>
End Property<br>
<br>
Public Property Let CalledParty(vNewValue)<br>
cCalledParty = vNewValue<br>
End Property<br>
<br>
Public Property Get Comment()<br>
Comment = cComment<br>
End Property<br>
<br>
Public Property Let Comment(vNewValue)<br>
cComment = vNewValue<br>
End Property</font></tt> </p>
</blockquote>
<hr>
<p>The last routine you need to add to the class module is a function called <tt><font
FACE="Courier">RequestMakeCall</font></tt>. Since this function is built within a Visual
Basic class module, it will act as a VB method. You'll call this method in the main
program (see Listing 29.9). </p>
<hr>
<blockquote>
<b><p>Listing 29.9. Adding the <tt><font FACE="Courier">RequestMakeCall</font></tt>
function to the class module.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Public Function RequestMakeCall() As Long<br>
RequestMakeCall = tapiRequestMakeCall(cDestAddress, cAppName,
cCalledParty, cComment)<br>
End Function</font></tt> </p>
</blockquote>
<hr>
<p>The function in Listing 29.9 calls the API declared in the <tt><font FACE="Courier">TBOOK0.BAS</font></tt>
module by using the four parameters set with the <tt><font FACE="Courier">Property Get</font></tt>/<tt><font
FACE="Courier">Property Let</font></tt> routines defined in the <tt><font FACE="Courier">clsPhoneCall</font></tt>
class. </p>
<h3><a NAME="CodingtheTeleBookMainForm">Coding the <tt><font SIZE="4" FACE="Courier">TeleBook</font></tt><font
SIZE="4"> Main Form</font></a></h3>
<p>Now it's time to build the onscreen forms for the <tt><font FACE="Courier">TeleBook</font></tt>
application. Use the information in Table 29.1 and Figure 29.5 to build the main form. </p>
<p>There are a few things to be aware of as you build this form. It contains two command
button arrays-<tt><font FACE="Courier">cmdKey(0-11)</font></tt> and <tt><font
FACE="Courier">cmdBtn(0-5)</font></tt>. You can save typing by adding the first button and
then using <tt><font FACE="Courier">Edit </font></tt>|<font FACE="AGaramond Bold"> </font><tt><font
FACE="Courier">Copy</font></tt>, <tt><font FACE="Courier">Edit </font></tt>|<tt><font
FACE="Courier"> Paste</font></tt> from the Visual Basic main menu. </p>
<p>Also, be sure to place the <tt><font FACE="Courier">Frame</font></tt> control on the
form before you place any of the controls that appear within the frame. When you place
controls in the frame, you must click the control from the toolbox and then <i>draw</i>
the control within the frame. </p>
<p>Finally, be sure to set the <tt><font FACE="Courier">Data1.DatabaseName</font></tt>
property to point to the <tt><font FACE="Courier">TELEBOOK.MDB</font></tt> database that
ships with the CD-ROM. It can be found in the <tt><font FACE="Courier">chAP29</font></tt>
directory. </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>If you want to run the project, but don't want to do all the typing, you can find the
source code in the <tt><font FACE="Courier">chAP29</font></tt> directory. Simply load this
project (make sure the data control is set properly) and run it! </p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p align="center"><b>Table 29.1. Building the <tt><font FACE="Courier">TeleBook</font></tt>
main form.</b> </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><i>Control</i></td>
<td WIDTH="138"><i>Property</i> </td>
<td WIDTH="166"><i>Setting</i></td>
</tr>
<tr>
<td WIDTH="167"><tt><font FACE="Courier">Form</font></tt></td>
<td WIDTH="138"><tt><font FACE="Courier">Name</font></tt></td>
<td WIDTH="166">frmTeleBook </td>
</tr>
<tr>
<td WIDTH="167"> </td>
<td WIDTH="138"><tt><font FACE="Courier">Caption</font></tt> </td>
<td WIDTH="166">TeleBook Control</td>
</tr>
<tr>
<td WIDTH="167"> </td>
<td WIDTH="138"><tt><font FACE="Courier">Height</font></tt> </td>
<td WIDTH="166">4515</td>
</tr>
<tr>
<td WIDTH="167"> </td>
<td WIDTH="138"><tt><font FACE="Courier">Left</font></tt> </td>
<td WIDTH="166">1155</td>
</tr>
<tr>
<td WIDTH="167"> </td>
<td WIDTH="138"><tt><font FACE="Courier">MaxButton</font></tt> </td>
<td WIDTH="166">0 - False</td>
</tr>
<tr>
<td WIDTH="167"> </td>
<td WIDTH="138"><tt><font FACE="Courier">Top</font></tt> </td>
<td WIDTH="166">1155</td>
</tr>
<tr>
<td WIDTH="167"> </td>
<td WIDTH="138"><tt><font FACE="Courier">Width</font></tt> </td>
<td WIDTH="166">5370</td>
</tr>
<tr>
<td WIDTH="167"><tt><font FACE="Courier">CommandButton</font></tt> </td>
<td WIDTH="138"><tt><font FACE="Courier">Name</font></tt></td>
<td WIDTH="166">cmdBtn</td>
</tr>
<tr>
<td WIDTH="167"> </td>
<td WIDTH="138"><tt><font FACE="Courier">Caption</font></tt> </td>
<td WIDTH="166">&Add</td>
</tr>
<tr>
<td WIDTH="167"> </td>
<td WIDTH="138"><tt><font FACE="Courier">Font</font></tt> </td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -