CDO Folder.moveTo function

I'm encountering some strange behaviour when using the MoveTo function... 
here's the code:

Dim objSession As MAPI.Session
Dim objInbox As MAPI.Folder
Dim objMessages As Messages
Dim objMessage As Message
Dim objFolder As MAPI.folder

Set objSession = New MAPI.Session
   
objSession.Logon ProfileName:="profileName", NewSession:=True, showDialog:=False
   
Set objInbox = objSession.Inbox
Set objMessages = objInbox.Messages
Set objMessage = objMessages.GetFirst
   
Do While Not objMessage Is Nothing
           
  If objMessage.Subject = "Certain Subject" And objMessage.Unread = True Then

  objMessage.Unread = False
  objMessage.Update

  Set objFolder = objSession.InfoStores("DifferentDataFile").RootFolder.Folders("FolderWithinDataFile")
           
  objMessage.MoveTo objFolder.FolderID, objFolder.StoreID

  Set objMessage = objInbox.GetNext

Loop


Ideally this should take certain unread messages with a given subject and mark them unread and move them to a folder within a different data file.
Most of it works. The weird thing is it moves it to the Data file's root folder instead of the "FolderWithinDataFile".... 
Even stranger, if I set the MoveTo parameters to the RootFoler.FolderID & StoreID, the message dissapears! 

Any help would be greatly appreciated.
Thanks

-Tyler



Answer this question

CDO Folder.moveTo function

  • rchatto

    Ok, first thing first:

    To loop through all the messages,
    Instead of using:

    For Each Message In Folder.Messages
    Next

    if you are moving the messages or deleting them, you should use:

    Do While objMessage Is Nothing

    Set objMessage = Folder.GetNext

    Loop

    If you use For...Each, you're error-prone when you get to the bottom of the message pile.

    Hmm.. As for the rest of it (your actual problem)... Try "Session.InfoStores("Inbox").RootFolder" and see if that still generates the error... I'm not sure if Inbox is a valid InfoStore... you could always create a new datafile from within outlook.. File -> Add -> DataFile.... use the same name you give it in the InfoStores collection

    You must forgive me as I'm away from work today. At work I've got my VB application all set up.. on my home computer, I seem to not have MAPI How strange.. So I can't thoroughly test anything I give you to see if it actually works.

    I know I encountered the same errors numerous times and it took me forever to get it to work. The first topic is a basically my original code. I had to remove the actual names and stuff to protect privacy... But I believe it should work.

    I'll get back to this on Monday and see what I can dig up to help you


  • Holger Grund

    Hi Tyler,

    Your problem lies here:

    objMessage.MoveTo objFolder.FolderID, objFolder.StoreID

    You should use "objFolder.ID" instead of "objFolder.FolderID", because the latter refers to the folder holding "objFolder", which is indeed the "DiffrentDataFile" folder.

    Regards,

    Goswinus


  • Michael Stephenson UK

    Thank you graciously!

    I must have stared at that code in confusion for upwards of a few hours and couldn't figure it out... I'll try it first thing in the morning tomorrow.

    -Tyler


  • Mustansir - MSFT

    What should be teh ("DifferentDataFile) and the FolderWithinDataFile should it be an example of "Inbox" and "Dell"
  • beezleinc

    Well, if it was inbox, you could just use the "Inbox" property...

    If you have a different DataFile within Outlook, you can access it through the InfoStores collection... The FolderWithinDataFile is a folder within this DataFile....

    For my program, I didn't want all the files I was looping through to go in my inbox... the entire point of the program was to move them away from the Inbox.


  • ysb

    Well I think I am a little confuse tswaters. In my project, what I am doing is reading every email sent to helpdesk from the inbox and generate an issue record in a SQL DB. Now, when that record has been generated, I want to move it to a special folder for storage. Here is my code below.

    Sub ReadHelpDeskMail()

    Dim i As Integer

    Dim Message As MAPI.Message

    Dim objMsg As MAPI.Recipient

    Dim PrSenderEmail, PrBodyEmail

    'Add-in OutLook Security manager

    Dim SecurityManager As New AddinExpress.Outlook.SecurityManager

    'Disable Security

    SecurityManager.DisableCDOWarnings = True

    'We use a session object of MAPI Component

    Session = CreateObject("MAPI.Session")

    Session.Logon("Outlook", "", False, False)

    Session.MAPIOBJECT = Session.MAPIOBJECT

    'Choose the folder

    Folder = CObj(Session.Inbox)

    'Get Mapi Namespace

    OutLookApp = CreateObject("Outlook.Application")

    OLNSP = OutLookApp.GetNamespace("MAPI")

    OLNSP.Logon()

    Console.WriteLine("Reading Help Desk Exchange Email")

    For Each Message In Folder.Messages

    ' On Error Resume Next 'GoTo MoveNext

    strname = Trim(Message.Sender.Name) 'Name of Sender

    If Mid(Message.Sender.Address, 1, 3) = "/o=" Or Mid(Message.Sender.Address, 1, 3) = "/O=" Then

    stremail = Trim(Mid(Message.Sender.Address, 65))

    stremail = stremail & "@sheltonstate.edu" ' Email Address

    Else

    stremail = Trim(Message.Sender.Address) ' Email Address

    End If

    strsubject = Trim(Message.Subject) 'Subject

    strbody = Trim(Message.Text) 'Message/Body

    strbody = strbody.Trim(" ")

    items(0) = Message.Text

    rtime = Trim(Message.TimeReceived) 'Time and Date Email recieved

    'Gets the movetofolder name location

    OutBox = CType(Folder.Folders(1).Name, Object) 'This acually gets the folder name

    Message.MoveTo(OutBox) 'error occurs here when try to move

    Dim fld As MAPI.Folder

    fld = Session.InfoStores("Inbox").RootFolder.Folders("Dell") 'error ocurs here sating Mapi_E_Not_Found

    Message.MoveTo(fld.ID)

    'Message.MoveTo(fld, fld.StoreID)

    'Message.Delete()

    'Output to window

    Console.WriteLine(strname & " " & stremail & _

    " " & strsubject & " " & strbody & " " & rtime & " " & rdate)

    'Get IssueID

    Call LoadIssueID()

    'Generate Helpdesk Issue

    Call GenerateHelpDeskIssue()

    'Generate IssueID

    Call IncrementIssueID()

    Next

    'Clear Up

    Session = Nothing

    Session.Logoff()

    Message = Nothing

    items = Nothing

    rtime = Nothing

    rdate = Nothing

    strname = Nothing

    stremail = Nothing

    strsubject = Nothing

    strbody = Nothing

    'Enable Security

    SecurityManager.DisableCDOWarnings = False

    'Close DB

    cnSheltonDB.Close()

    End Sub


  • CDO Folder.moveTo function