Moving Outlook.Items from Outlook.MAPIFolder

C

C.Shamis

I am using VB 2005, VSTO 2005 SE BETA, Outlook 2003 SP2.

I am trying to iterate through all the item in an Outlook.MAPIFolder, and
move it to another. The problem is sometimes the assignment fails.

The problem is, the code works for hundreds of items at a time, but it may
fail on the 200th, or the 30th. There is no rhyme or reason I can detect. I
can run the program over and over with the same data-set (a *copy* of my
inbox on our exchange server), and it will fail in different places each
time.

Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objFolderItems As Outlook.Items
Dim objMail As Outlook.MailItem

objApp = CreateObject("Outlook.Application")
objNS = objApp.GetNamespace("MAPI")

objSourceFolder = objNS.GetFolderFromID(My.Settings.SourceID)
objDestFolder = objNS.GetFolderFromID(My.Settings.DestID)

objFolderItems = objSourceFolder.Items

If radMove.Checked Then
For i As Integer = objFolderItems.Count To 1 Step -1
objMail = TryCast(objFolderItems.Item(i), Outlook.MailItem)
If Not objMail Is Nothing Then
objMail.Move(objDestFolder)
End If
Next i
MsgBox(objFolderItems.Count.ToString() + " message(s) could not
be moved.")
End If

Do I need to use a different object method to assign the
objFolderItems.Item(i) into my objMail? TryCast isn't the right thing to
use? Do I need to put a "lock" on the MAPIFolder? Do I need to perform a
"wait" on the object?

--Chris
 
K

Ken Slovak - [MVP - Outlook]

What failure is happening? Are you getting any errors or exceptions?

I would try using a Try...Catch block and checking and outputting any
exceptions that are caught to see what's going on.
 
C

C.Shamis

I get: Unable to cast COM object of type 'System.__ComObject' to interface
type 'Outlook.MailItem'. This operation failed because the QueryInterface
call on the COM component for the interface with IID
'{00063034-0000-0000-C000-000000000046}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).

On this line: objMail = objFolderItems.Item(i)

But I never know when I'm going to get this error, and even if I trap it
out, there's nothing I can do about it. "Re-trying" the cast over and over
eventually (after a few minutes) will eventually suceed, but that isn't how
things are supposed to work.
 
K

Ken Slovak - [MVP - Outlook]

I'd tend to agree with Dmitry in the other thread you started, an overload
of the RPC limit. That would account for things working again after a period
of time, that time is probably after the garbage collector has freed up some
of the RPC channels.
 
C

cshamis0

That was it. Either setting the obj to Nothing, or calling the
GC.Collect() after each iteration did the trick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top