Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

vba - MailItem.SaveAs when referencing with EntryID

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:empest.msg", olMsg

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...