📄 ch29.htm
字号:
<html>
<head>
<title>Chapter 29 -- Writing TAPI-Assisted Applications</title>
<meta NAME="GENERATOR" CONTENT="Microsoft FrontPage 3.0">
</head>
<body TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<!-- Spidersoft WebZIP Ad Banner Insert -->
<!-- End of Spidersoft WebZIP Ad Banner Insert-->
<h1><font COLOR="#FF0000">Chapter 29</font></h1>
<h1><b><font SIZE="5" COLOR="#FF0000">Writing TAPI-Assisted Applications</font></b> </h1>
<hr WIDTH="100%">
<h3 ALIGN="CENTER"><font SIZE="+2" COLOR="#000000">CONTENTS<a NAME="CONTENTS"></a> </font></h3>
<ul>
<li><a HREF="#TheAssistedTelephonyAPICalls">The Assisted Telephony API Calls</a> <ul>
<li><a HREF="#TestingtheAssistedTAPIFunctions">Testing the Assisted TAPI Functions</a> </li>
</ul>
</li>
<li><a HREF="#CreatingtheQikDialApplicationUsingE">Creating the QikDial Application Using
Excel 95</a> </li>
<li><a HREF="#WritingtheTeleBookApplicationUsingV">Writing the TeleBook Application Using
Visual Basic 4.0</a> <ul>
<li><a HREF="#CreatingtheTeleBookClassModule">Creating the TeleBook Class Module</a> </li>
<li><a HREF="#CodingtheTeleBookMainForm">Coding the TeleBook Main Form</a> </li>
<li><a HREF="#CodingtheTeleBookPhoneEntryForm">Coding the TeleBook Phone Entry Form</a> </li>
<li><a HREF="#CodingthePhoneLogFormforTeleBook">Coding the Phone Log Form for TeleBook</a> </li>
<li><a HREF="#CodingtheSupportRoutinesfortheTele">Coding the Support Routines for the
TeleBook Application</a> </li>
<li><a HREF="#RunningtheTeleBookTAPIApplication">Running the TeleBook TAPI Application</a> </li>
</ul>
</li>
<li><a HREF="#Summary">Summary</a> </li>
</ul>
<hr>
<p><font COLOR="#000000">Now that you have a good idea of how the TAPI system is designed,
it's time to start writing some TAPI programs! This chapter covers the simplest form of
TAPI-the outbound voice-phone call. When you complete this chapter you'll know how to add
phone-dialing capabilities to Excel spreadsheets (or any other VBA-compatible system) and
you'll build a complete Visual Basic 4.0 online phone book that can store names and
numbers, place calls, and log your call history into an Access database for tracking and
reporting purposes.</font> </p>
<h2><a NAME="TheAssistedTelephonyAPICalls"><font SIZE="5" COLOR="#FF0000">The Assisted
Telephony API Calls</font></a></h2>
<p>Before jumping into the coding routines for Assisted TAPI, it is a good idea to review
and test out the two API calls that you'll use throughout the chapter. The two functions
you need to work with in order to complete Assisted TAPI calls are:
<ul>
<li><tt><font FACE="Courier">tapiGetLocationInfo</font></tt>-Returns the current country and
city (area) codes set in the <tt><font FACE="Courier">TELEPHON.INI</font></tt> file. </li>
<li><tt><font FACE="Courier">tapiRequestMakeCall</font></tt>-Initiates a voice-phone call by
passing the dialing address (phone number) and other optional parameters including the
dialing application to use, the name of the person you are calling, and a comment about
the nature of the call. </li>
</ul>
<p>The TAPI system uses the <tt><font FACE="Courier">tapiGetLocationInfo</font></tt> to
determine the current country/area code settings when attempting to dial a phone number
passed in the <tt><font FACE="Courier">tapiRequestMakeCall</font></tt> function. For
example, if the code supplied in the call request includes an area code, TAPI will check
to see if it matches the current area code. If the two codes match, TAPI will not dial the
area code since it is understood that it is not needed to successfully place a call. This
means that you can store complete area code information with all your phone numbers in
your address book. Just like you, TAPI is smart enough to skip the area code when it's
appropriate. </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Tip</b></td>
</tr>
<tr>
<td><blockquote>
<p>Although TAPI's ability to strip out area codes is handy, it can cause minor problems.
In several areas of the world, phone companies allow local calls across area
codes-especially in locations where both parties live close to one another along the
"dividing line." In these cases, TAPI will notice the area code difference and
attempt to place the call using the area code. This usually results in error tones from
the phone exchange. If you are in an area where this occurs, you'll need to leave out area
codes from the affected address entries.</p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p><tt><font FACE="Courier">tapiGetLocationInfo</font></tt> is a read-only function. You
cannot use it to set the current country or area code. However, you can use this function
to return the country/area code string to be used as a default in building new entries in
an online address book. You'll see how this works later in the chapter. </p>
<h3><a NAME="TestingtheAssistedTAPIFunctions">Testing the Assisted TAPI Functions</a></h3>
<p>For now, let's put together a short project that illustrates the two Assisted TAPI
functions you'll use throughout the chapter. You'll need a modem connected to your pc and
a telephone handset connected to the same line as the modem. Figure 29.1 shows how the
equipment should be connected for all Assisted TAPI calls. </p>
<p><a HREF="f29-1.gif"><b>Figure 29.1 : </b><i>Modem, pc, and phone connections for
Assisted TAPI </i></a></p>
<p>To test the Assisted TAPI functions, start a new Visual Basic 4.0 project and place
buttons on the form. Set the caption of <tt><font FACE="Courier">Command1</font></tt> to <tt><font
FACE="Courier">Get Info</font></tt> and the caption of <tt><font FACE="Courier">Command2</font></tt>
to <tt><font FACE="Courier">Place Call</font></tt>. Next add a code module to the project
(select <tt><font FACE="Courier">Insert </font></tt>|<tt><font FACE="Courier"> Module</font></tt>
from the main menu) and add the code shown in Listing 29.1 to the declaration section of
the module. </p>
<hr>
<blockquote>
<b><p>Listing 29.1. Declaring the TAPI functions.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Option Explicit<br>
<br>
'<br>
' declare assisted tapi functions<br>
'<br>
#If Win32 Then<br>
Declare Function tapiRequestMakeCall Lib "TAPI32.DLL"
(ByVal lpszDestAddress As <br>
<font FACE="ZAPFDINGBATS">Â</font>String, ByVal lpszAppName As String, ByVal
lpszCalledParty As String, ByVal <font FACE="ZAPFDINGBATS">Â</font>lpszComment As
String) As Long<br>
Declare Function tapiGetLocationInfo Lib "TAPI32.DLL"
(ByVal lpszCountryCode As <font FACE="ZAPFDINGBATS">Â</font>String, ByVal
lpszCityCode As String) As Long<br>
#Else<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>
Declare Function tapiGetLocationInfo Lib "TAPI.DLL"
(ByVal lpszCountryCode As <font FACE="ZAPFDINGBATS">Â</font>String, ByVal
lpszCityCode As String) As Long<br>
#End If</font></tt> </p>
</blockquote>
<hr>
<p>These are the two Assisted TAPI functions in their 16-bit and 32-bit form. If you are
only working in one environment, you can remove the extra code. But, if you plan to use
this application code in more than one environment, leave the two sets of declares in the
file. </p>
<p>Now you need to add code behind the two buttons. Listing 29.2 shows the code for the <br>
<tt><font FACE="Courier">Command1</font></tt> (<tt><font FACE="Courier">Get Info</font></tt>)
button. Add this code to the <tt><font FACE="Courier">Command1_Click</font></tt> event. </p>
<hr>
<blockquote>
<b><p>Listing 29.2. Adding code for the <tt><font FACE="Courier">Get Info</font></tt>
button.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Private Sub Command1_Click()<br>
'<br>
Dim lTapi As Long<br>
Dim cCountry As String * 1<br>
Dim cCity As String * 3<br>
'<br>
lTapi = tapiGetLocationInfo(cCountry, cCity)<br>
MsgBox Mid(cCountry, 1, 1) + "-(" + Mid(cCity, 1, 3) +
")", 0, "Location Info" <br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<p>Pressing this button causes Visual Basic to display a message box showing the current
country and city code (area code) stored in the TELEPHON.INI file/registry.Notice the
variable declaration sets string sizes for the <tt><font FACE="Courier">cCountry</font></tt>
and <tt><font FACE="Courier">cCity</font></tt> variables. This is needed in order to make
sure the <tt><font FACE="Courier">tapiGetLocationInfo</font></tt> function returns clean
data. You also need to make sure you trim the returned variables. TAPI will return these
two variables as zero-terminated strings (the last character is a zero-<tt><font
FACE="Courier">Chr(0)</font></tt>). Zero characters are unprintable in Visual Basic and
can produce unexpected results. It's always a good idea to clean your string upon return
from API calls. </p>
<p>The code in Listing 29.3 shows how to place a call using the <tt><font FACE="Courier">tapiRequestMakeCall</font></tt>
function. Add this code to the <tt><font FACE="Courier">Command2_Click</font></tt> event. </p>
<hr>
<blockquote>
<b><p>Listing 29.3. Adding code for the <tt><font FACE="Courier">Place Call</font></tt>
button.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Private Sub Command2_Click()<br>
'<br>
Dim cAddress As String<br>
Dim lTapi As Long<br>
'<br>
cAddress = InputBox("Enter Phone Number to Call:",
"Place Call")<br>
cAddress = Trim(cAddress)<br>
'<br>
If Len(cAddress) <> 0 Then<br>
lTapi = tapiRequestMakeCall(cAddress,
"", "", "")<br>
If lTapi <> 0 Then<br>
MsgBox "Error
placing call!", vbCritical, "TAPI Error Code [" + <font FACE="ZAPFDINGBATS">Â</font>CStr(lTapi)
+ "]"<br>
End If<br>
End If<br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<p>Only the first parameter (the phone number) is required for the <tt><font
FACE="Courier">tapiRequestMakeCall</font></tt> function. The other parameters are optional
(dialing application, called party, and comment). You'll use those variables in the Visual
Basic 4.0 project at the end of the chapter. </p>
<p>Save the form as <tt><font FACE="Courier">TAPI01.FRM</font></tt>, the module as <tt><font
FACE="Courier">TAPI01.BAS</font></tt>, and the project as <tt><font FACE="Courier">TAPI01.VBP</font></tt>.
Now run the project. When you click on the <tt><font FACE="Courier">Get_Info</font></tt>
button, you'll see your country and city code. When you click on the <tt><font
FACE="Courier">Place_Call</font></tt> button, you'll be asked to enter a phone number to
dial. Visual Basic will hand the number to the TAPI DLL, which will call the default
dialer application (<tt><font FACE="Courier">DIALER.EXE</font></tt>) which will then
process the call. You'll hear the phone dialing and see the dialog asking you to pick up
the phone and begin speaking. </p>
<p>Those are the basics of Assisted TAPI calls. Now you can use this knowledge to add
dialing capabilities to an Excel spreadsheet. </p>
<h2><a NAME="CreatingtheQikDialApplicationUsingE"><font SIZE="5" COLOR="#FF0000">Creating
the </font><tt><font SIZE="5" COLOR="#FF0000" FACE="Courier">QikDial</font></tt><font
SIZE="5" COLOR="#FF0000"> Application Using Excel 95</font></a></h2>
<p>It's really quite easy to add outbound dialing to any Excel spreadsheet. Since you only
need one API call (<tt><font FACE="Courier">tapiRequestMakeCall</font></tt>), you have
very little code to deal with. All you need is a single declare statement to cover the API
call and one subroutine to handle the details of gathering the phone number from the user
and calling the API function. </p>
<p>For this chapter, you'll create a very simple phone book using Excel. The example here
will allow users to create a two-column table within a worksheet that contains a name in
one column and a phone number in the next column. Users can highlight a name and then
press an on-screen command button that will then place the call for them. </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>The example illustrated here was done using Excel 95, but the same general idea can be
handled in Excel 5.0.</p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>Start Excel and/or open a new workbook. Since you'll be doing a bit of coding, be sure
that the Visual Basic toolbar and the Forms toolbar are visible. If not, select <tt><font
FACE="Courier">View </font></tt>|<tt><font FACE="Courier"> Toolbars</font></tt> and then
place a check mark next to <tt><font FACE="Courier">Visual Basic</font></tt> and <tt><font
FACE="Courier">Forms</font></tt>. Figure 29.2 shows you what your Excel spreadsheet should
look like. </p>
<p>The first thing you need to do is add the TAPI function declaration. To do this you
must first add a code module to the project. Select <tt><font FACE="Courier">Insert </font></tt>|<tt><font
FACE="Courier"> Macro </font></tt>|<tt><font FACE="Courier"> Module</font></tt> from the
main menu or click on the <tt><font FACE="Courier">Insert Module</font></tt> icon in the
Visual Basic toolbar. Once the module has been added to the project, rename the tab to <tt><font
FACE="Courier">VBA Code</font></tt>. Now insert the code in Listing 29.4. </p>
<p><a HREF="f29-2.gif"><b>Figure 29.2 : </b><i>Adding the Visual Basic and forms tools to
Excel </i></a></p>
<hr>
<blockquote>
<b><p>Listing 29.4. Adding the API declare to an Excel module.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>'<br>
' declare assisted-tapi function<br>
'<br>
Declare Function tapiRequestMakeCall Lib "TAPI32.DLL" (ByVal lpszDestAddress As
String, ByVal lpszAppName As String, ByVal lpszCalledParty As String, ByVal lpszComment As
String) As Long</font></tt> </p>
</blockquote>
<hr>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Warning</b></td>
</tr>
<tr>
<td><blockquote>
<p>This example uses the 32-bit version of the API call. This code will not run under
16-bit Windows environments (Window 3.11 or Windows for Workgroups). If you are using
Excel in a 16-bit environment, modify the declare statement to reference <tt><font
FACE="Courier">TAPI.DLL</font></tt> rather than <tt><font FACE="Courier">TAPI32.DLL</font></tt>.
</p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>Now you need to add a small subroutine that will determine the cell selected by the
user, locate the associated phone number and then place the call using the TAPI function
declared in Listing 29.4. Listing 29.5 shows the code needed for this routine. Place this
code in the same module that contains the API declare. </p>
<hr>
<blockquote>
<b><p>Listing 29.5. Adding the subroutine that makes the call.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>'<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -