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

📄 ch8.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<html>

<head>
<title>Chapter 8 -- The OLE Messaging Library</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 8</font></h1>

<h1><b><font SIZE="5" COLOR="#FF0000">The OLE Messaging Library</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="#Introduction">Introduction</a> </li>
  <li><a HREF="#TheSessionObject">The Session Object</a> <ul>
      <li><a HREF="#TheSessionObjectMethods">The Session Object Methods</a> </li>
      <li><a HREF="#TheSessionObjectProperties">The Session Object Properties</a> </li>
    </ul>
  </li>
  <li><a HREF="#TheInfoStoreObjectsandCollections">The InfoStore Objects and Collections</a> <ul>
      <li><a HREF="#TheInfoStoresCollection">The InfoStores Collection</a> </li>
      <li><a HREF="#TheInfoStoreObject">The InfoStore Object</a> </li>
    </ul>
  </li>
  <li><a HREF="#TheFolderObjectsandCollections">The Folder Objects and Collections</a> <ul>
      <li><a HREF="#TheFoldersCollectionObject">The Folders Collection Object</a> </li>
      <li><a HREF="#TheFolderObject">The Folder Object</a> </li>
      <li><a HREF="#TheInboxandOutBoxFolders">The Inbox and OutBox Folders</a> </li>
    </ul>
  </li>
  <li><a HREF="#TheMessageObjectsandCollections">The Message Objects and Collections</a> <ul>
      <li><a HREF="#TheMessagesCollectionObject">The Messages Collection Object</a> </li>
      <li><a HREF="#TheMessageObject">The Message Object</a> </li>
    </ul>
  </li>
  <li><a HREF="#TheRecipientObjectsandCollectionsan">The Recipient Objects and Collections and 
    the Address Object</a> <ul>
      <li><a HREF="#TheRecipientsCollectionObject">The Recipients Collection Object</a> </li>
      <li><a HREF="#TheRecipientObject">The Recipient Object</a> </li>
      <li><a HREF="#TheAddressEntryObject">The AddressEntry Object</a> </li>
    </ul>
  </li>
  <li><a HREF="#TheAttachmentObjectsandCollections">The Attachment Objects and Collections</a> 
    <ul>
      <li><a HREF="#TheAttachmentsCollectionObject">The Attachments Collection Object</a> </li>
      <li><a HREF="#TheAttachmentObject">The Attachment Object</a> </li>
    </ul>
  </li>
  <li><a HREF="#Summary">Summary</a> </li>
</ul>

<hr>

<h2><a NAME="Introduction"><b><font SIZE="5" COLOR="#FF0000">Introduction</font></b></a> </h2>

<p>One of the new features of Microsoft Exchange is the creation of the <i>OLE Messaging 
library</i>. This set of OLE objects, properties, and methods allows any VBA-compliant 
development tool to gain access to MAPI services and incorporate them into desktop 
applications. This chapter shows you how the OLE Messaging library works and how you can 
use the OLE objects to create MAPI-enabled programs. </p>

<p>This chapter provides an overview of all the OLE Messaging library objects and gives 
examples of their use. You'll learn about the following objects: 

<ul>
  <li><font COLOR="#000000">The </font><tt><font FACE="Courier">Session</font></tt> object </li>
  <li><font COLOR="#000000">The </font><tt><font FACE="Courier">InfoStore</font></tt> objects 
    and collections </li>
  <li><font COLOR="#000000">The </font><tt><font FACE="Courier">Folder</font></tt> objects and 
    collections </li>
  <li><font COLOR="#000000">The </font><tt><font FACE="Courier">Message</font></tt> objects 
    and collections </li>
  <li><font COLOR="#000000">The </font><tt><font FACE="Courier">Recipient</font></tt> objects 
    and collections </li>
  <li><font COLOR="#000000">The </font><tt><font FACE="Courier">Address</font></tt> objects </li>
  <li><font COLOR="#000000">The </font><tt><font FACE="Courier">Attachment</font></tt> objects 
    and collections </li>
</ul>

<p>You'll also learn how these objects interact with each other and how to use them to 
perform several advanced MAPI tasks, including: 

<ul>
  <li><font COLOR="#000000">Moving messages from one folder to another</font> </li>
  <li><font COLOR="#000000">Renaming existing folders</font> </li>
  <li><font COLOR="#000000">Modifying and deleting address entries</font> </li>
  <li><font COLOR="#000000">Switching between message stores</font> </li>
</ul>

<p>When you complete this chapter, you'll understand the OLE Messaging library objects and 
how to use them to build MAPI-compliant e-mail applications with any VBA-compatible 
development tool. </p>

<h2><b><a NAME="TheSessionObject"><font SIZE="5" COLOR="#FF0000">The </font><tt><font
SIZE="5" COLOR="#FF0000" FACE="Courier">Session</font></tt><font SIZE="5" COLOR="#FF0000"> 
Object</font></a></b></h2>

<p>The <tt><font FACE="Courier">Session</font></tt> object of the OLE Messaging library is 
the top-most object in the hierarchy. You must create an instance of a MAPI <tt><font
FACE="Courier">Session</font></tt> object before you can gain access to any other aspects 
of the MAPI system. The <tt><font FACE="Courier">Session</font></tt> object has several 
properties (including subordinate objects) and a handful of methods that you can invoke. </p>

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

<p>Table 8.1 shows the <tt><font FACE="Courier">Session</font></tt> Object Methods along 
with a list of parameters and short descriptions. <br>
</p>

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

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><i>Methods</i></td>
    <td WIDTH="252"><i>Parameters</i> </td>
    <td WIDTH="114"><i>Description</i></td>
  </tr>
  <tr>
    <td WIDTH="127"><tt><font FACE="Courier">AddressBook</font></tt> </td>
    <td WIDTH="252">(opt) title as String, <br>
    (opt) <tt><font FACE="Courier">oneAddress</font></tt> as Boolean,<br>
    (opt) <tt><font FACE="Courier">forceResolution</font></tt> as Boolean,<br>
    (opt) <tt><font FACE="Courier">recipLists</font></tt> as long,<br>
    (opt) <tt><font FACE="Courier">toLabel</font></tt> as String,<br>
    (opt) <tt><font FACE="Courier">ccLabel</font></tt> as String,<br>
    (opt) <tt><font FACE="Courier">bccLabel</font></tt> as String,<br>
    (opt) <tt><font FACE="Courier">parentWindow</font></tt> as Long </td>
    <td WIDTH="114">Access to session addressbook.</td>
  </tr>
  <tr>
    <td WIDTH="127"><tt><font FACE="Courier">GetAddressEntry</font></tt> </td>
    <td WIDTH="252"><tt><font FACE="Courier">entryID </font></tt>as String </td>
    <td WIDTH="114">Direct access to a single entry in the address book. </td>
  </tr>
  <tr>
    <td WIDTH="127"><tt><font FACE="Courier">GetInfoStore</font></tt> </td>
    <td WIDTH="252"><tt><font FACE="Courier">storeID</font></tt> as String </td>
    <td WIDTH="114">Direct access to one of the Message storage objects. </td>
  </tr>
  <tr>
    <td WIDTH="127"><tt><font FACE="Courier">GetFolder</font></tt> </td>
    <td WIDTH="252"><tt><font FACE="Courier">folderID</font></tt> as String, <br>
    <tt><font FACE="Courier">storeID</font></tt> as String </td>
    <td WIDTH="114">Direct access to a single folder object in the folders collection. </td>
  </tr>
  <tr>
    <td WIDTH="127"><tt><font FACE="Courier">GetMessage</font></tt> </td>
    <td WIDTH="252"><tt><font FACE="Courier">messageID</font></tt> as String, <br>
    <tt><font FACE="Courier">storeID</font></tt> as String </td>
    <td WIDTH="114">Direct access to a single message in the Messages collection. </td>
  </tr>
  <tr>
    <td WIDTH="127"><tt><font FACE="Courier">Logoff</font></tt> </td>
    <td WIDTH="252">(none)</td>
    <td WIDTH="114">End the current MAPI Session. </td>
  </tr>
  <tr>
    <td WIDTH="127"><tt><font FACE="Courier">Logon</font></tt></td>
    <td WIDTH="252">(opt) <tt><font FACE="Courier">profileName</font></tt> as String, <br>
    (opt) <tt><font FACE="Courier">profilePassword</font></tt> as String,<br>
    (opt) <tt><font FACE="Courier">showDialog</font></tt> as Boolean,<br>
    (opt) <tt><font FACE="Courier">newSession</font></tt> as Boolean,<br>
    (opt) <tt><font FACE="Courier">parentWindow</font></tt> as Long </td>
    <td WIDTH="114">Start new MAPI Session.</td>
  </tr>
</table>
</center></div>

<p>The most used of these methods are the <tt><font FACE="Courier">Logon</font></tt> and <tt><font
FACE="Courier">Logoff</font></tt> methods. You use these to start and end MAPI sessions. 
You will also use the <tt><font FACE="Courier">AddressBook</font></tt> and <tt><font
FACE="Courier">GetInfoStore</font></tt> methods frequently in your programs. </p>

<h4><b>Using the MAPI <tt><font FACE="Courier">Logon</font></tt> and <tt><font
FACE="Courier">Logoff</font></tt> Methods</b></h4>

<p>If you have not done so yet, load Visual Basic 4.0 and start a new project. Place a 
single button on the form. Set its index property to <tt><font FACE="Courier">0</font></tt> 
and its caption to <tt><font FACE="Courier">MAPI &amp;Start</font></tt>. Copy and paste a 
second command button onto the form and set its caption property to <tt><font
FACE="Courier">MAPI &amp;End</font></tt>. Now add the code in Listing 8.1 to the <tt><font
FACE="Courier">Command1_Click</font></tt> event of the buttons. </p>

<hr>

<blockquote>
  <b><p>Listing 8.1. Adding Code to the <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>
  End Select<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>The code in Listing 8.1 calls two subroutines-<tt><font FACE="Courier">MAPIStart</font></tt> 
and <tt><font FACE="Courier">MAPIEnd</font></tt>. They each use a form-level variable 
called <tt><font FACE="Courier">objSession</font></tt>. Add this variable to the general 
declarations section of the form. </p>

<blockquote>
  <tt><font FACE="Courier"><p>Option Explicit<br>
  '<br>
  Dim objSession As Object ' for mapi session</font></tt> </p>
</blockquote>

<p>Now create a new subroutine called <tt><font FACE="Courier">MAPIStart</font></tt> and 
add the code shown in Listing 8.2. This routine initializes the session object and calls 
the MAPI logon dialog box. </p>

<hr>

<blockquote>
  <b><p>Listing 8.2. Adding the <tt><font FACE="Courier">MAPIStart</font></tt> routine.<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<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>

<p>It is important that you formally end each MAPI session you begin. This will ensure 
that you do not have any stray sessions running in the background.<br>
</p>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td WIDTH="574"><b>Warning</b></td>
  </tr>
  <tr>
    <td WIDTH="574"><blockquote>
      <p>By default, the MAPI <tt><font FACE="Courier">Logon</font></tt> method will attempt to 
      connect you to the first available active session currently running on your workstation. 
      For example, if you have started a MAPI session with your e-mail client and you then run 
      this sample code, MAPI will attach this program to the same MAPI session started by the 
      MAPI client. This could give you unexpected results. That is another reason why you should 
      always close your MAPI sessions when you are exiting your programs. </p>
    </blockquote>
    </td>
  </tr>
</table>

<p>Now add a new subroutine to the project called <tt><font FACE="Courier">MAPIEnd</font></tt> 
and add the code shown in Listing 8.3. Notice that this routine sets the <tt><font

⌨️ 快捷键说明

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