HOWTO: Move Messages Using Active Messaging MoveTo Method

Last reviewed: November 21, 1997
Article ID: Q172740
The information in this article applies to:
  • Collaboration Data Objects (CDO), version 1.1
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Standard and Professional Editions, 32-bit only, for Windows, version 4.0

SUMMARY

Using the Active Messaging version 1.1 Object Library you can move a message to another folder without first copying and then deleting it. This new functionality is provided by the new MoveTo method.

This article describes how to use the MoveTo method.

MORE INFORMATION

The sample code in this article requires a reference to the Microsoft Active Messaging 1.1 Object Library (Olemsg32.dll). If you are missing this file, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q171440
   TITLE     : INFO: Where to Acquire the Active Messaging Libraries

This article requires that you set up the appropriate folders to move a message to. The sample code assumes that messages have a subject line starting with either "East" or "West".

The syntax of the MoveTo method is as follows:

   Set objMovedMessage = objMessage.MoveTo(folderID [, storeID ] )

where
  • objMovedMessage : On successful return, contains the moved Message object.
  • objMessage : Required. This Message object.
  • folderID : Required. String. The unique identifier of the destination Folder object in which this message is to appear.
  • storeID : Optional. String. The unique identifier of the InfoStore object in which the message is to appear, if different from this current InfoStore.

The FolderID is a string created when a folder is created and it never changes. If you know the FolderID, you can directly code it into your application. If you do not know the FolderID, but know the folder name and where it is located, you can find the FolderID by using the ID property of the folder object. This example assumes that you do not know the FolderID, but that you do know the name of the folder you want to move a message to.

Sample Code

  1. Copy the following code to a Module:

          Option Explicit
    

          Public objSession As MAPI.Session
          Public strEast As String
          Public strWest As String
    

          Sub Main()
             Dim objInboxFolder As Folder
             Dim objInboxFolders As Folders
             Dim f As Integer
             Screen.MousePointer = vbHourglass
             Set objSession = CreateObject("MAPI.Session")
             objSession.Logon ("YourSessionIDHere")
    
             Set objInboxFolder = objSession.Inbox
             Set objInboxFolders = objInboxFolder.Folders
    
             'Set strMoveToFolderID equal to the ID property of the
             'folder you want to move the message to.  This will
             'be used later
             With objInboxFolders
                For f = 1 To .Count
                   Select Case .Item(f).Name
                      Case "East"
                         strEast = .Item(f).ID
                      Case "West"
                         strWest = .Item(f).ID
                   End Select
                Next f
             End With 'objInboxFolders
             Load Form1
             Form1.Show
             Screen.MousePointer = vbNormal
          End Sub
    
    

  2. Copy the following code to a Form:

          Option Explicit
    

          Private Sub cmdMoveMail_Click()
    
             If Not MoveMessageToInboxSubfolder Then
                MsgBox "A move operation failed"
             End If
          End Sub
    
          Public Function MoveMessageToInboxSubfolder() As Boolean
             Dim objInboxFolder As Folder
             Dim objInMessages As Messages
             Dim objOneMessage As Message
             Dim objMoveMessage As Message
             Dim objInboxFolders As Folders
             Dim objMoveToFolder As Folder
             Dim objMsgFilter As MessageFilter
             Dim i As Integer  'Attachment Counter
             Dim f As Integer  'Folder counter
             Dim strMoveToFolderID As String
    
             On Error GoTo ErrorHandler
    
             Screen.MousePointer = vbHourglass
             Set objInboxFolder = objSession.Inbox
             Set objInboxFolders = objInboxFolder.Folders
             Set objInMessages = objInboxFolder.Messages
             Set objMsgFilter = objInMessages.Filter  'Set the Message Filter
                                                     'object
             'Filter for unread messages
             objMsgFilter.Unread = True
    
             'Loop through all the messages in the objInMessages
             For i = 1 To objInMessages.Count
                Set objOneMessage = objInMessages.Item(i)
                'Look for a subject that we want to move to another folder
                Select Case Left(UCase(objOneMessage.Subject), 4)
                   Case "EAST"
                      strMoveToFolderID = strEast
                   Case "WEST"
                      strMoveToFolderID = strWest
                   Case Else
                      strMoveToFolderID = ""
                End Select
    
                'This If statement does the actual move
                If strMoveToFolderID <> "" Then
                   Set objMoveMessage = objOneMessage.MoveTo(strMoveToFolderID)
                   objMoveMessage.Update
                   'Need to decrement "i" because objInMessages.Count
                   'has decremented by 1.  You need to decrement "i"
                   'to keep the loops synchronized
                   i = i - 1
                End If
    
                'Test to see of you've examined all the messages
                If i = objInMessages.Count Then
                   Screen.MousePointer = vbNormal
                   MoveMessageToInboxSubfolder = True
                   Exit Function
                End If
             Next i
             MoveMessageToInboxSubfolder = True
    
             Screen.MousePointer = vbNormal
             Set objInboxFolder = Nothing
             Set objInMessages = Nothing
             Set objOneMessage = Nothing
             Set objMoveMessage = Nothing
             Set objInboxFolders = Nothing
             Set objMoveToFolder = Nothing
             Exit Function
          ErrorHandler:
             MoveMessageToInboxSubfolder = False
             Exit Function
          End Function
    
    

  3. Add the required sub folders to the Inbox, set your project to start up on Sub Main, and run the above example.

REFERENCES

For more information, please see the "MoveTo Method" topics in the July 1997 "Microsoft Developer Network Library." This is the path to that topic:

    SDK Documentation
      /Platform SDK
        /Database and Messaging Services
          /Active Messaging
            /Reference
              /Message Object
                /MoveTo Method

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)


Additional query words: move
Keywords : ActMsg
Version : WINDOWS:1.1,4.0,5.0
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.