3
votes

I'm developing a drag and drop of a MailItem from Outlook (I know it's a MailItem and not any other type) into an Access memo field. Trying to call SaveAs on a MailItem object.

I get

Error 287 - Application-defined or object-defined error.

I've tried using the namespace, not using the namespace, using .Item, etc.

Here's my current code:

Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")

Dim olMail As Outlook.MailItem
Set olMail = olNs.GetItemFromID(olNs.Application.ActiveExplorer.Selection(1).EntryID)

olMail.SaveAs strPathAndFile, Outlook.OlSaveAsType.olMSG

Access 2010, Outlook 2010 both 32 bit. Win 7 machine is 64 bit.

Tried it on an all 32-bit machine, same error.

Tried Dmitry's code below, same error.

3

3 Answers

4
votes

There is really no need to reopen the message:

Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.ActiveExplorer.Selection(1)
olMail.SaveAs strPathAndFile, Outlook.OlSaveAsType.olMSG

Secondly, what is the value of the strPathAndFile variable?

As a test, can you install Redemption and try the following script (you can run it from OutlookSpy - click Script, button, paste the script, click Run). If there is a problem on the MAPI level, Redemption will report it instead of giving an obscure error message.

  set Session = CreateObject("Redemption.RDOSession")
  Session.MAPIOBJECT = Application.Session.MAPIOBJECT
  set Msg = Session.GetMessageFromID(Application.ActiveExplorer.Selection(1).EntryID)
  Msg.SaveAs "c:\temp\test.msg", olMsg
2
votes

Finally got this working using Dmitry Streblechenko's awesome Redemption library. Thanks!

Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")

Dim oRDOSession As Redemption.RDOSession
Set oRDOSession = CreateObject("Redemption.RDOSession")
oRDOSession.MAPIOBJECT = olNs.Application.Session.MAPIOBJECT

If Not oRDOSession.LoggedOn Then oRDOSession.Logon

Dim oMsgItem As Redemption.RDOMail
Set oMsgItem = oRDOSession.GetMessageFromID(olNs.Application.ActiveExplorer.Selection(1).EntryID)

strPathAndFile = "some\path\" & UniqueValueStr(Now()) & ".msg"

oMsgItem.SaveAs strPathAndFile
2
votes

Why do you need to use Redemption?

I don't see any difference between using the OOM and Redempton.

Why do you need to use the GetItemFromID method in the code?

olNs.GetItemFromID(olNs.Application.ActiveExplorer.Selection(1).EntryID)

You have already got a reference to the selected object in the explorer window:

olNs.Application.ActiveExplorer.Selection(1)

Also I'd suggest breaking the chaing of calls and declaring each property or method call on a separate line.