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.folderSet 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 ThenobjMessage.Unread = False
objMessage.UpdateSet objFolder = objSession.InfoStores("DifferentDataFile").RootFolder.Folders("FolderWithinDataFile")
objMessage.MoveTo objFolder.FolderID, objFolder.StoreIDSet 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

CDO Folder.moveTo function
rchatto
Ok, first thing first:
To loop through all the messages,
Instead of using:
if you are moving the messages or deleting them, you should use:
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
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 SecuritySecurityManager.DisableCDOWarnings =
True 'We use a session object of MAPI ComponentSession = CreateObject("MAPI.Session")
Session.Logon("Outlook", "",
False, False)Session.MAPIOBJECT = Session.MAPIOBJECT
'Choose the folderFolder =
CObj(Session.Inbox) 'Get Mapi NamespaceOutLookApp = 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 MoveNextstrname = Trim(Message.Sender.Name)
'Name of Sender If Mid(Message.Sender.Address, 1, 3) = "/o=" Or Mid(Message.Sender.Address, 1, 3) = "/O=" Thenstremail = Trim(Mid(Message.Sender.Address, 65))
stremail = stremail & "@sheltonstate.edu"
' Email Address Elsestremail = Trim(Message.Sender.Address)
' Email Address End Ifstrsubject = Trim(Message.Subject)
'Subjectstrbody = Trim(Message.Text)
'Message/Bodystrbody = strbody.Trim(" ")
items(0) = Message.Text
rtime = Trim(Message.TimeReceived)
'Time and Date Email recieved 'Gets the movetofolder name locationOutBox = 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.Folderfld = 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 windowConsole.WriteLine(strname & " " & stremail & _
" " & strsubject & " " & strbody & " " & rtime & " " & rdate)
'Get IssueID Call LoadIssueID() 'Generate Helpdesk Issue Call GenerateHelpDeskIssue() 'Generate IssueID Call IncrementIssueID() Next 'Clear UpSession =
NothingSession.Logoff()
Message =
Nothingitems =
Nothingrtime =
Nothingrdate =
Nothingstrname =
Nothingstremail =
Nothingstrsubject =
Nothingstrbody =
Nothing 'Enable SecuritySecurityManager.DisableCDOWarnings =
False 'Close DBcnSheltonDB.Close()
End Sub