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

📄 ch36.htm

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

<hr>

<p>Two short MAPI routines are <tt><font FACE="Courier">MAPIAddrBook</font></tt> and <tt><font
FACE="Courier">MAPIDeleteMsg</font></tt>. These two routines give the user access to the 
MAPI address book and delete a selected message, respectively. Add these two subroutines 
to your project from Listings 36.4 and 36.5. </p>

<hr>

<blockquote>
  <b><p>Listing 36.4. 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;On Error GoTo LocalErr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objSession.AddressBook<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  LocalErr:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MsgBox ErrMsg(Err), vbCritical, &quot;MAPIAddrBook&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>The <tt><font FACE="Courier">MAPIDeleteMsg</font></tt> routine will remove a selected 
message object from the collection. Add the code from Listing 36.5 to your project. </p>

<hr>

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

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub MAPIDeleteMsg(cMsgID As String) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' delete selected message<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo LocalErr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim iAns As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objMsg = objSession.GetMessage(cMsgID) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;iAns = MsgBox(objMsg.Subject, vbExclamation + vbYesNo, 
  &quot;Delete Talk Mail <font FACE="ZAPFDINGBATS">&Acirc;</font>Message&quot;)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If iAns = vbYes Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objMsg.Delete <br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  LocalErr:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MsgBox ErrMsg(Err), vbCritical, &quot;MAPIDeleteMsg&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>Next you need to add two routines that call the <tt><font FACE="Courier">tmRead</font></tt> 
and <tt><font FACE="Courier">tmNew</font></tt> dialog boxes to read or create new 
messages. These two routines are almost identical. The only difference is the name of the 
form that is launched. Add the <tt><font FACE="Courier">MAPINewMsg</font></tt> subroutine 
and enter the code form Listing 36.6. </p>

<hr>

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

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub MAPINewMsg()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' allow user to compose a message<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmTMView.CMDialog1.Filter = &quot;WaveForm File 
  (*.wav)|*.wav&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmTMNew.MMControl1.DeviceType = &quot;WaveAudio&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmTMNew.Show<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmTMView.Hide<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>Note that this routine (and the <tt><font FACE="Courier">MAPIReadMsg</font></tt> 
routine) both just hide the main form rather than unloading it. This is necessary because 
the project has some values that must be shared between forms. Now add the <tt><font
FACE="Courier">MAPIReadMsg</font></tt> routine shown in Listing 36.7. </p>

<hr>

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

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub MAPIReadMsg(cMsgID As String) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' allow user to view a message<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmTMView.CMDialog1.Filter = &quot;WaveForm File 
  (*.wav)|*.wav&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmTMRead.MMControl1.DeviceType = &quot;WaveAudio&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmTMRead.Show<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmTMView.Hide<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>The next routine is the largest one in the module. The <tt><font FACE="Courier">MAPISendMsg</font></tt> 
routine is the one that builds the MAPI message object and makes sure it is sent off to 
the recipient(s). Add the <tt><font FACE="Courier">MAPISendMsg</font></tt> subroutine to 
the project and enter the code from Listing 36.8. </p>

<hr>

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

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub MAPISendMsg(objRecipList As Object, cSubject As 
  String, cBody As String, <font FACE="ZAPFDINGBATS">&Acirc;</font>cWavFile As String)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' construct a message and send it off <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo LocalErr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim x As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' build new message<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objMsg = objOutBox.Messages.Add<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.Type = &quot;IPM.Note.TalkMail&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.Subject = cSubject<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.Text = &quot; &quot; &amp; cBody <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' add wav file as attachment<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If Len(cWavFile) &lt;&gt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set objAttach = objMsg.Attachments.Add<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objAttach.Name = &quot;Talk Mail.WAV&quot; 
  'cWavFile<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objAttach.position = 0<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objAttach.Type = mapiFileData<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objAttach.Source = cWavFile<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objAttach.ReadFromFile cWavFile<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' load recipient(s)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If objRecipList Is Nothing Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;No Recipients!&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For x = 1 To objRecipList.Count <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set objRecip = 
  objMsg.Recipients.Add<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Name = 
  objRecipList.Item(x).Name<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next x<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' send it out quietly<br>
  &nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.Update ' update all the collections <br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.Send showdialog:=False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' let everyone know<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Message Sent&quot;, vbInformation, &quot;Talk 
  Mail&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  LocalErr:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MsgBox ErrMsg(Err), vbCritical, &quot;MAPISendMsg&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>There are four main tasks performed by this routine. First, the message body is 
assembled. Note the use of <tt><font FACE="Courier">IPM.Note.TalkMail</font></tt> in the <tt><font
FACE="Courier">Type</font></tt> property. You'll use this value as a search criterion when 
you refresh the list of available Talk Mail messages. </p>

<p>Next, the recorded WAV audio file is added to the package as an attachment. There are a 
couple of important points to make here. First, it is important that you use 
&quot;.WAV&quot; as the suffix of the <tt><font FACE="Courier">Name</font></tt> property 
of the attachment object. This will make sure you can click the object, and Windows will 
call up the audio player automatically. Also, when you are attaching data to the message 
(not just linking, but attaching), you need to invoke the <tt><font FACE="Courier">ReadFromFile</font></tt> 
method to actually load the selected file into the message package. Unlike the OCX 
controls, OLE does not do this for you. </p>

<p>Third, you need to add the recipients from the collection returned by the address book 
into the collection for the message. This requires iterating through the source object and 
using the results to insert into the message's receipt object. </p>

<p>Lastly, the routine updates all its child objects and quietly sends the MAPI package to 
the MAPI spooler for delivery. </p>

<p>There are three general helper functions that need to be added to the <tt><font
FACE="Courier">LibTalkMail</font></tt> module. The first one is the <tt><font
FACE="Courier">FindSubject</font></tt> function. This is used to return a unique MAPI 
message ID using the UDT you built at the start of this section. Add the <tt><font
FACE="Courier">FindSubject</font></tt> function and enter the code shown in Listing 36.9. </p>

<hr>

<blockquote>
  <b><p>Listing 36.9. Adding the <tt><font FACE="Courier">FindSubject</font></tt> function.<br>
  <br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Function FindSubject(cSubject As String) As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' takes subject line, returns MsgID<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim x As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cRtn As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cRtn = &quot;&quot; ' start empty<br>
  &nbsp;&nbsp;&nbsp;&nbsp;For x = 1 To iMsgCount<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If UCase(cSubject) = 
  UCase(uMsgP(x).Subject) Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cRtn = uMsgP(x).ID<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit For<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Next x<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;FindSubject = cRtn<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Function</font></tt> </p>
</blockquote>

<hr>

<p>Another very valuable helper function is the <tt><font FACE="Courier">GetRecipients</font></tt> 
function. This routine accepts a collection of recipients and extracts the display name 
into a single string for producing an onscreen list of the message recipients. After 
adding the new <tt><font FACE="Courier">GetRecipients</font></tt> function to the project, 
enter the code shown in Listing 36.10. </p>

<hr>

<blockquote>
  <b><p>Listing 36.10. Adding the new <tt><font FACE="Courier">GetRecipients</font></tt> 
  function.<br>
  <br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Function GetRecipients(objRecipColl As Object) As 
  String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' pull qualified recipients from object collection<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cRtn As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim x As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cRtn = &quot;&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If objRecipColl Is Nothing Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GoTo LeaveHere <br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;For x = 1 To objRecipColl.Count<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set objRecip = objRecipColl.Item(x)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cRtn = cRtn &amp; objRecip.Name &amp; 
  &quot;;&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Next x<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If Len(cRtn) &gt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cRtn = Left(cRtn, Len(cRtn) - 1)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  LeaveHere:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;GetRecipients = cRtn<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Function</font></tt> </p>
</blockquote>

<hr>

<p>The last helper function you need to add to the <tt><font FACE="Courier">LibTalkMail</font></tt> 
module is the <tt><font FACE="Courier">ErrMsg</font></tt> function. This routine uses the 
new Visual Basic 4.0 <tt><font FACE="Courier">Err</font></tt> object to build an 
informative message concerning the name, type, and originator of the message. Add the code 
in Listing 36.11 to your project. </p>

<hr>

<blockquote>
  <b><p>Listing 36.11. Add the <tt><font FACE="Courier">ErrMsg</font></tt> function.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Function ErrMsg(ErrObj As Object) As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' return formatted message<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cMsg As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cMsg = &quot;ErrCode:&quot; &amp; Chr(9) &amp; CStr(ErrObj.Number) 
  &amp; Chr(13)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cMsg = cMsg &amp; &quot;ErrMsg:&quot; &amp; Chr(9) &amp; 
  ErrObj.Description &amp; Chr(13)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cMsg = cMsg &amp; &quot;Source:&quot; &amp; Chr(9) &amp; 
  ErrObj.Source<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;ErrMsg = cMsg<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Function</font></tt> </p>
</blockquote>

⌨️ 快捷键说明

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