⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch8.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 5 页
字号:
FACE="Courier">objSession</font></tt> variable to <tt><font FACE="Courier">Nothing</font></tt>. 
This is done to clear Visual Basic's memory storage and conserve RAM space. </p>

<hr>

<blockquote>
  <b><p>Listing 8.3. Adding the <tt><font FACE="Courier">MAPIEnd</font></tt> routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub MAPIEnd()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' end the current session<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objSession.logoff<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objSession = Nothing<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>Save the form as <tt><font FACE="Courier">OML.FRM</font></tt> and the project as <tt><font
FACE="Courier">OML.VBP</font></tt>. You can now run the project and log into and out of a 
MAPI session. You won't see much, but it works! </p>

<h4><b>Accessing the MAPI Address Book</b></h4>

<p>You will often need to give your users access to the MAPI Address Book. This is 
achieved with the <tt><font FACE="Courier">AddressBook</font></tt> method of the <tt><font
FACE="Courier">Session</font></tt> object. Access to the address book gives users the 
ability to look up names in the address book; add, edit, and delete names from the book; 
and select one or more addresses as recipients of a message. </p>

<p>Add another command button to the array (<tt><font FACE="Courier">Edit </font></tt><font
SIZE="1">|</font><tt><font FACE="Courier"> Copy</font></tt>, <tt><font FACE="Courier">Edit 
</font></tt><font SIZE="1">|</font><tt><font FACE="Courier"> Paste</font></tt>) and modify 
the <tt><font FACE="Courier">Command1_Click</font></tt> event to match the code in Listing 
8.4. This will call up the MAPI address book. </p>

<hr>

<blockquote>
  <b><p>Listing 8.4. Modifying the <tt><font FACE="Courier">Command1_Click</font></tt> event 
  to call the address book.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Private Sub Command1_Click(Index As Integer) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' handle user selections<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Select Case Index<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case 0 ' mapi start<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAPIStart <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case 1 ' mapi end<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAPIEnd <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case 2 ' call address book<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAPIAddrBook <br>
  &nbsp;&nbsp;&nbsp;&nbsp;End Select<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>Now add the <tt><font FACE="Courier">MAPIAddrBook</font></tt> routine to the form and 
enter the code in Listing 8.5. </p>

<hr>

<blockquote>
  <b><p>Listing 8.5. Adding the <tt><font FACE="Courier">MAPIAddrBook</font></tt> routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub MAPIAddrBook()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' call the address book<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objSession.AddressBook<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>Save and run the project. After clicking the MAPI <tt><font FACE="Courier">Start</font></tt> 
button, press the <tt><font FACE="Courier">Address Book</font></tt> button. Your screen 
should look like the one in Figure 8.1. </p>

<p><a HREF="f8-1.gif"><b>Figure 8.1 : </b><i>Displaying the address book.</i></a> </p>

<p>You can set several parameters when you call the <tt><font FACE="Courier">Address Book</font></tt> 
method. For example, you can set the title of the address book using the <tt><font
FACE="Courier">Title</font></tt> property. You can also control the number and caption of 
the recipient selection buttons that appear on the address book. You can even set the 
address book dialog so that the user can review the addresses but cannot select one. </p>

<p>Listing 8.6 shows you how to modify the code in the <tt><font FACE="Courier">MAPIAddrBook</font></tt> 
routine to set the title and remove all recipient selection buttons. </p>

<hr>

<blockquote>
  <b><p>Listing 8.6. Controlling the appearance of the MAPI address book.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub MAPIAddrBook()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' call the address book<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objSession.AddressBook recipLists:=0, Title:=&quot;For Viewing 
  Only&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>When you run the project, your address book will look something like the one in Figure 
8.2. </p>

<p><a HREF="f8-2.gif"><b>Figure 8.2 : </b><i>Modified Address Book dialog box</a>.</i> </p>

<h4><b>Automating the Session Logon</b></h4>

<p>You can also control the <tt><font FACE="Courier">Logon</font></tt> method behavior by 
passing selected parameters. The most common use for this is automatically logging a user 
into MAPI without the use of the <tt><font FACE="Courier">Logon</font></tt> dialog box. To 
do this you need to pass the user profile and password and set the <tt><font
FACE="Courier">ShowDialog</font></tt> flag to false. </p>

<p>Listing 8.7 shows you how to modify the <tt><font FACE="Courier">MAPIStart</font></tt> 
routine to perform an automatic logon. You should change the <tt><font FACE="Courier">ProfileName</font></tt> 
parameter to match your personal Microsoft Exchange logon. </p>

<hr>

<blockquote>
  <b><p>Listing 8.8. Creating an automatic MAPI session logon.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub MAPIStart()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' start a mapi session<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo MAPIStartErr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objSession = CreateObject(&quot;MAPI.Session&quot;) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;objSession.logon ProfileName:=&quot;MCA&quot;, ShowDialog:=False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  MAPIStartErr:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MsgBox Error$, vbCritical, &quot;MAPIStartErr [&quot; &amp; 
  CStr(Err) &amp; &quot;]&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<h3><b><a NAME="TheSessionObjectProperties">The <tt><font SIZE="4" FACE="Courier">Session</font></tt><font
SIZE="4"> Object Properties</font></a></b></h3>

<p>Now let's look at some of the <tt><font FACE="Courier">Session</font></tt> object 
properties. After reviewing the properties, you can add another button to the form to 
display the properties of your MAPI session. Table 8.2 has a list of the <tt><font
FACE="Courier">Session</font></tt> object properties, their type, and a short description.<br>
</p>

<p align="center"><b>Table 8.2. <tt><font FACE="Courier">Session</font></tt> object 
properties.</b> </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><i>Properties</i></td>
    <td WIDTH="181"><i>Type</i> </td>
    <td WIDTH="243"><i>Description</i></td>
  </tr>
  <tr>
    <td WIDTH="166"><tt><font FACE="Courier">Application</font></tt> </td>
    <td WIDTH="181">String</td>
    <td WIDTH="243">Name of the library. Always set to <tt><font FACE="Courier">OLE/Messaging</font></tt>. 
    </td>
  </tr>
  <tr>
    <td WIDTH="166"><tt><font FACE="Courier">Class</font></tt></td>
    <td WIDTH="181">Long</td>
    <td WIDTH="243">Internal identifying code for all MAPI objects. Always set to <tt><font
    FACE="Courier">0</font></tt> for <tt><font FACE="Courier">Session</font></tt> objects. </td>
  </tr>
  <tr>
    <td WIDTH="166"><tt><font FACE="Courier">CurrentUser</font></tt> </td>
    <td WIDTH="181"><tt><font FACE="Courier">AddressEntry</font></tt> object </td>
    <td WIDTH="243"><tt><font FACE="Courier">Address</font></tt> object of current user (see 
    &quot;<tt><font FACE="Courier">Address</font></tt> Objects&quot; later in this chapter). </td>
  </tr>
  <tr>
    <td WIDTH="166"><tt><font FACE="Courier">Inbox</font></tt></td>
    <td WIDTH="181"><tt><font FACE="Courier">Folder</font></tt> object </td>
    <td WIDTH="243"><tt><font FACE="Courier">Folder</font></tt> object where all new unread 
    messages are placed (see &quot;<tt><font FACE="Courier">Folder</font></tt> Objects&quot; 
    later in this chapter). </td>
  </tr>
  <tr>
    <td WIDTH="166"><tt><font FACE="Courier">InfoStores</font></tt> </td>
    <td WIDTH="181"><tt><font FACE="Courier">InfoStores</font></tt> object </td>
    <td WIDTH="243"><tt><font FACE="Courier">InfoStores</font></tt> object collection for this 
    session (see &quot;<tt><font FACE="Courier">InfoStore</font></tt> <tt><font FACE="Courier">Objects</font></tt>&quot; 
    later in this chapter). </td>
  </tr>
  <tr>
    <td WIDTH="166"><tt><font FACE="Courier">Name</font></tt></td>
    <td WIDTH="181">String</td>
    <td WIDTH="243">Session name. Always set to the current profile name on Microsoft Exchange 
    system. </td>
  </tr>
  <tr>
    <td WIDTH="166"><tt><font FACE="Courier">OperatingSystem</font></tt> </td>
    <td WIDTH="181">String</td>
    <td WIDTH="243">Name of operating system in use. </td>
  </tr>
  <tr>
    <td WIDTH="166"><tt><font FACE="Courier">Outbox</font></tt> </td>
    <td WIDTH="181"><tt><font FACE="Courier">Folder</font></tt> object </td>
    <td WIDTH="243"><tt><font FACE="Courier">Folder</font></tt> object where all outgoing 
    messages are placed (see &quot;<tt><font FACE="Courier">Folder</font></tt> Objects&quot; 
    later in this chapter). </td>
  </tr>
  <tr>
    <td WIDTH="166"><tt><font FACE="Courier">Version</font></tt> </td>
    <td WIDTH="181">String</td>
    <td WIDTH="243">Version number of OLE Messaging library. Current version is <tt><font
    FACE="Courier">1.00</font></tt>. </td>
  </tr>
</table>
</center></div>

<p>The <tt><font FACE="Courier">Session</font></tt> object has several properties, many of 
them objects themselves. Using these object properties allows you to gain access to more 
complex data than standard strings or numbers. You'll inspect the object properties later 
in the chapter. </p>

<p>Now add another button to the control array (use <tt><font FACE="Courier">Edit </font></tt><font
SIZE="1">|</font><tt><font FACE="Courier"> Copy</font></tt>, <tt><font FACE="Courier">Edit 
</font></tt><font SIZE="1">|</font> <tt><font FACE="Courier">Paste</font></tt>), set its 
Caption to &quot;MapiProps&quot; and modify the code in the <tt><font FACE="Courier">Command1_Click</font></tt> 
event to look like the code in Listing 8.9. </p>

<hr>

<blockquote>
  <b><p>Listing 8.9. Updated <tt><font FACE="Courier">Command1_Click</font></tt> event.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Private Sub Command1_Click(Index As Integer) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' handle user selections<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Select Case Index<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case 0 ' mapi start<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAPIStart <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case 1 ' mapi end<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAPIEnd <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case 2 ' call address book<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MAPIAddrBook <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case 3 ' session properties<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -