PC Review Forums Newsgroups Microsoft Outlook Microsoft Outlook Program Addins COMException: "Can't move the items." on MailItem.Move

Reply

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

 
Thread Tools Rate Thread
Old 19-12-2003, 02:32 AM   #1
Jim Carpenter
Guest
 
Posts: n/a
Default COMException: "Can't move the items." on MailItem.Move


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
  Reply With Quote
Old 19-12-2003, 03:37 AM   #2
Dmitry Streblechenko
Guest
 
Posts: n/a
Default Re: COMException: "Can't move the items." on MailItem.Move

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" <jamescarpenter@hotmail.com> wrote in message
news:6138e84f.0312181832.62ecf8a2@posting.google.com...
> 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



  Reply With Quote
Old 19-12-2003, 07:47 PM   #3
Jim Carpenter
Guest
 
Posts: n/a
Default Re: COMException: "Can't move the items." on MailItem.Move

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" <dmitry@dimastr.com> wrote in message news:<O6v5aFexDHA.2712@tk2msftngp13.phx.gbl>...
> 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" <jamescarpenter@hotmail.com> wrote in message
> news:6138e84f.0312181832.62ecf8a2@posting.google.com...
> > 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

  Reply With Quote
Old 19-12-2003, 09:05 PM   #4
Dmitry Streblechenko
Guest
 
Posts: n/a
Default Re: COMException: "Can't move the items." on MailItem.Move

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" <jamescarpenter@hotmail.com> wrote in message
news:6138e84f.0312191147.4181f343@posting.google.com...
> 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" <dmitry@dimastr.com> wrote in message

news:<O6v5aFexDHA.2712@tk2msftngp13.phx.gbl>...
> > 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" <jamescarpenter@hotmail.com> wrote in message
> > news:6138e84f.0312181832.62ecf8a2@posting.google.com...
> > > 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



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off