HOWTO: Finding a Sent Message using Collaboration Data Objects

ID: Q195569


The information in this article applies to:
  • Collaboration Data Objects (CDO), versions 1.1, 1.2, 1.21


SUMMARY

Using Collaboration Data Objects (CDO) it is possible to find a message created programmatically even after the user has had a chance to change some properties of the message. One way to accomplish this task is by adding a custom field to the message before the user has a chance to make modifications.

The code in the MORE INFORMATION section demonstrates how to add a custom field to a message and then find that same message in the Sent Items folder after the message is sent.


MORE INFORMATION

You may need to use this method when you programmatically fill out some fields of the message object with some default data before showing the message window to the user for further modifications.

For example, you may want to fill out the To and Subject fields of the message before showing it to the user who fills out the Text part of the message later. The problem is, after you show the message dialog box to the user, he/she has the ability to modify your default data. If you want to keep track of the final version of the message programmatically, you need to find the copy of the message that has already been sent.

Copies of sent messages are kept in the Sent Items folder of Microsoft Outlook when you call the Send method of the Message object with the SaveCopy parameter set to True (which is the default).

You can search for the sent message in the Sent Items folder to see the changes made to the message by the user. However, at this point, you do not know anything about the message to filter for it, since the user may have changed all default data (recipients, subject, and so forth). In addition, the entryID of the message is changes when moved to the Sent Items folder.

One possibility is to add a dummy field to the message object before calling the Send method with ShowDialog:=True. Then, you can search the Sent Items folder for the content of the dummy field.

The following code sample demonstrates how to add a dummy field to a message object and set a filter on Sent Items folder based on the content of the dummy field.

Sample Code


   Sub Main()
      Dim objSess As MAPI.Session
      Dim objMsg As Message
      Dim objSentMsg As Message
      Dim objMyField As Field
      Dim objMessColl As Messages
      Dim objMsgFilter As MessageFilter
      Dim objInfoStores As InfoStores
      Dim objRootFolder As Folder
      Dim objFolders As Folders
      Dim objSentItems As Folder
      Dim objOutbox as Folder

      Set objSess = CreateObject("mapi.session")
      objSess.Logon "YourProfileName"  'Put a valid profile name here.
      Set objOutbox = objSess.Outbox
      Set objMsg = objOutbox.Messages.Add
      'Add some default data to your message before showing it to the user.
      With objMsg
         .Text = "This is my text."
         .Subject = "This is my subject."
         .Recipients.Add Name:="YourRecipientAddress"  'Put a valid
                                                     'recipient name here.
         .Recipients.Resolve
      End With

      ' Add a dummy field with unique content to the message object
      ' to identify the message, later on. If guaranteed uniqueness is
      ' important a GUID would be more appropriate but for simplicity we
      ' will use the current "system date and time" for the content of the
      ' field
      Mytime = Now
      Set objMyfield = objMsg.Fields.Add("MyField", 8)
      objMyfield.Value = Mytime
      myfieldID = objMyfield.ID

      ' Save the changes you made to the message.
      objMsg.Update refreshobject:=True

      ' Display the message window.
      objMsg.Send ShowDialog:=True

      ' Get the "Sent Items" Folder
      ' Note: If you use CDO 1.2 or CDO 1.21 library, you can get the "Sent
      ' Items" folder by simply calling GetDefaultFolder method of the
      ' Session object as follows. If this is the case, uncomment the
      ' following line of code and comment out the lines in between ' *\*
      ' and ' */*

      'Set objSentItems = objSess.GetDefaultFolder(3)

      ' *\*
      Set objInfostore = objSess.InfoStores
      CountInfoStores = objInfostore.Count
      FoundMailbox = False
      For i = 1 To CountInfoStores
         If Mid(objInfostore(i), 1, 7) = "Mailbox" Then
            FoundMailbox = True
            Set objRootFolder = objInfostore(i).RootFolder
            Set objFolders = objRootFolder.Folders
            For j = 1 To objfolders.Count
               If objFolders(j).Name = "Sent Items" Then
                  Set objSentItems = objFolders(j)
                  Exit For
               End If
            Next
         End If
         If FoundMailbox = True Then
            Exit For
         End If
      Next
      ' */*

      Set objMessColl = objSentItems.Messages
      Set objMsgFilter = objMessColl.Filter

      ' Setup a filter that passes only messages with the dummy field .
      objMsgFilter.Fields(myfieldID) = Mytime
      For Each objSentMsg In objMessColl
         MsgBox objSentMsg.Subject
         Msgbox objSentMsg.Text
      Next

      Set objSentMsg = Nothing
      Set objMyField = Nothing
      Set objMessColl = Nothing
      Set objMsgFilter = Nothing
      Set objSentItems = Nothing
      Set objOutbox = Nothing

      ' Comment out the following lines if you used the GetDefaultFolder
      ' Method.

      ' *\*
      Set objInfoStore = Nothing
      Set objRootFolder = Nothing
      Set objFolders = Nothing
      ' */*

      objSess.Logoff
      Set objSess = Nothing

    End Sub 

Additional query words:

Keywords : kbCDO110 kbCDO120 kbCDO121 kbMsg kbVBp kbGrpMsg
Version : WINDOWS:1.1,1.2,1.21
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 5, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.