COMException: "Can't move the items." on MailItem.Move

J

Jim Carpenter

I am writing a COM Add-In for Outlook 2002 using C#:

try
{
Outlook.NameSpace ns = applicationObject.GetNamespace("MAPI");
Outlook.MAPIFolder inbox =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.MAPIFolder deleted =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems);
Outlook.MailItem item = (Outlook.MailItem) inbox.Items[0];
item = item.Move(deleted);
}
catch (Exception e)
{
Debug.WriteLine("Exception: " + e.Message);
}

When I try and move an item from the Inbox to the Deleted Items
folder, the code works correctly. If I then move the item from the
Deleted Items folder back to the Inbox using the GUI and run the code
again, it fails with a COMException: "Can't move the items." on
MailItem.Move.

It seems like the GUI is locking the MailItem and preventing the
Add-In from accessing it.

Any help would be greatly appreciated?

Thanks,

Jim Carpenter
SRI International
 
D

Dmitry Streblechenko

That error is by design - it is > 0, meaning it is a non-critical error and
can be safely ignored. .Net runtime (Delphi does that too) expects S_OK (=
0) and raises an exception if it is anything else. The only workaround is to
explicitly call IDispatch.GetIDsOfNames("Move", ...) and IDispatch.Invoke()
handling any returned errors in your code. Easy to do in C++/Delphi, but I
have no idea how you would do that in C#.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
J

Jim Carpenter

Dmitry,

Thanks for your response. I don't think that I can safely ignore
the exception because it is failing to move the email. I ran the code
in the debugger and got some more detail on the exception:

Exception: System.Runtime.InteropServices.COMException (0xC6220009):
Can't move the items.
ErrorCode = -970850295
HelpLink =
InnerException =
Message = Can't move the items.
Source = Microsoft Outlook
TargetSite = System.Object
Move(Microsoft.Office.Interop.Outlook.MAPIFolder)

I did a search on MSDN and Usenet but couldn't find anything for the
HRESULT = -970850295. Is there some source that lists all possible
Outlook ErrorCodes/HRESULTs?

I think that the .NET equivalent of IDispatch.Invoke() would be to use
the classes of the System.Reflection namespace, but I am not sure how
this would help me here. It seems like I am able to programmatically
move EmailItems to different folders, but as soon as I open/move them
in the GUI, the code stops working. Is there anyway to see if some
other process/thread has locked the item? Is there some other API
that I should/could be using (e.g. MAPI vs. OOM)?

Thanks,

Jim Capenter
SRI International


Dmitry Streblechenko said:
That error is by design - it is > 0, meaning it is a non-critical error and
can be safely ignored. .Net runtime (Delphi does that too) expects S_OK (=
0) and raises an exception if it is anything else. The only workaround is to
explicitly call IDispatch.GetIDsOfNames("Move", ...) and IDispatch.Invoke()
handling any returned errors in your code. Easy to do in C++/Delphi, but I
have no idea how you would do that in C#.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Jim Carpenter said:
I am writing a COM Add-In for Outlook 2002 using C#:

try
{
Outlook.NameSpace ns = applicationObject.GetNamespace("MAPI");
Outlook.MAPIFolder inbox =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.MAPIFolder deleted =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems);
Outlook.MailItem item = (Outlook.MailItem) inbox.Items[0];
item = item.Move(deleted);
}
catch (Exception e)
{
Debug.WriteLine("Exception: " + e.Message);
}

When I try and move an item from the Inbox to the Deleted Items
folder, the code works correctly. If I then move the item from the
Deleted Items folder back to the Inbox using the GUI and run the code
again, it fails with a COMException: "Can't move the items." on
MailItem.Move.

It seems like the GUI is locking the MailItem and preventing the
Add-In from accessing it.

Any help would be greatly appreciated?

Thanks,

Jim Carpenter
SRI International
 
D

Dmitry Streblechenko

Hmmm... I don't recognize that error code. Where does item come form? How
long to you keep it referenced? It could be that your code is locking it:
after a message is moved, it is gone, so you must dereference it as soon as
possible.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Jim Carpenter said:
Dmitry,

Thanks for your response. I don't think that I can safely ignore
the exception because it is failing to move the email. I ran the code
in the debugger and got some more detail on the exception:

Exception: System.Runtime.InteropServices.COMException (0xC6220009):
Can't move the items.
ErrorCode = -970850295
HelpLink =
InnerException =
Message = Can't move the items.
Source = Microsoft Outlook
TargetSite = System.Object
Move(Microsoft.Office.Interop.Outlook.MAPIFolder)

I did a search on MSDN and Usenet but couldn't find anything for the
HRESULT = -970850295. Is there some source that lists all possible
Outlook ErrorCodes/HRESULTs?

I think that the .NET equivalent of IDispatch.Invoke() would be to use
the classes of the System.Reflection namespace, but I am not sure how
this would help me here. It seems like I am able to programmatically
move EmailItems to different folders, but as soon as I open/move them
in the GUI, the code stops working. Is there anyway to see if some
other process/thread has locked the item? Is there some other API
that I should/could be using (e.g. MAPI vs. OOM)?

Thanks,

Jim Capenter
SRI International


"Dmitry Streblechenko" <[email protected]> wrote in message
That error is by design - it is > 0, meaning it is a non-critical error and
can be safely ignored. .Net runtime (Delphi does that too) expects S_OK (=
0) and raises an exception if it is anything else. The only workaround is to
explicitly call IDispatch.GetIDsOfNames("Move", ...) and IDispatch.Invoke()
handling any returned errors in your code. Easy to do in C++/Delphi, but I
have no idea how you would do that in C#.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Jim Carpenter said:
I am writing a COM Add-In for Outlook 2002 using C#:

try
{
Outlook.NameSpace ns = applicationObject.GetNamespace("MAPI");
Outlook.MAPIFolder inbox =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.MAPIFolder deleted =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems);
Outlook.MailItem item = (Outlook.MailItem) inbox.Items[0];
item = item.Move(deleted);
}
catch (Exception e)
{
Debug.WriteLine("Exception: " + e.Message);
}

When I try and move an item from the Inbox to the Deleted Items
folder, the code works correctly. If I then move the item from the
Deleted Items folder back to the Inbox using the GUI and run the code
again, it fails with a COMException: "Can't move the items." on
MailItem.Move.

It seems like the GUI is locking the MailItem and preventing the
Add-In from accessing it.

Any help would be greatly appreciated?

Thanks,

Jim Carpenter
SRI International
 

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