Outlook VBA - 移动对象后获取EntryID

时间:2015-07-24 13:46:58

标签: vba outlook

摘要

我尝试以编程方式将超链接添加到从我移动到另一个文件夹的电子邮件创建的任务中。目标是让任务包含链接到Outlook项目的超链接,该项目被移动到"已处理的电子邮件"夹"但我遇到了麻烦。移动后获取对象的EntryID的最佳方法是什么?

问题

但是,我不了解如何移动MailItem,然后在移动后以编程方式获取新的EntryID。 "天真"方式不起作用。使用Move方法移动MailItem对象后,EntryID属性不会反映ID的更改。

详细

如果outlook项目保留在收件箱中,则使用格式outlook:<EntryID>创建指向Outlook项目的超链接非常容易,因为我可以获取我链接到的对象的EntryID。但是,Outlook会在移动对象时更改EntryID。我想了解如何获取更新的ID,以便我可以构建一个准确的链接。

实施例

请参阅以下示例代码。消息框显示objMail的EntryID属性仍然返回相同的值,尽管移动了对象。但是,在目标文件夹中的邮件上运行单独的宏可确认EntryID已随移动而更改。

Sub MoveObject(objItem As Object)

Select Case objItem.Class
Case olMail

    Dim objMail As MailItem
    Set objMail = objItem

    MsgBox (objMail.EntryID)

    Dim inBox As Outlook.MAPIFolder
    Set inBox = Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    Dim destFolder As Outlook.MAPIFolder
    Set destFolder = inBox.Folders("Processed Email")
    If (Application.ActiveExplorer().CurrentFolder.Name <> destFolder.Name) Then 
        objMail.Move destFolder
    End If
    MsgBox (objMail.EntryID)
End Select
End Sub

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:6)

MailItem类的Move方法返回一个对象,该对象表示已移动到指定文件夹的项目。您需要检查返回对象的EntryID值,而不是源对象。

无论如何,您可以考虑处理目标文件夹的ItemAdd事件,以确保始终使用更新的条目ID值。

 Sub MoveItems() 
  Dim myNameSpace As Outlook.NameSpace 
  Dim myInbox As Outlook.Folder 
  Dim myDestFolder As Outlook.Folder 
  Dim myItems As Outlook.Items 
  Dim myItem As Object  
  Set myNameSpace = Application.GetNamespace("MAPI") 
  Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox) 
  Set myItems = myInbox.Items 
  Set myDestFolder = myInbox.Folders("Personal Mail") 
  Set myItem = myItems.Find("[SenderName] = 'Eugene Astafiev'") 
  While TypeName(myItem) <> "Nothing" 
   myItem.Move myDestFolder 
   Set myItem = myItems.FindNext 
  Wend 
 End Sub

答案 1 :(得分:1)

您好,请您详细说明您的答案我无法理解。

无论如何,您可以考虑处理目标文件夹的ItemAdd事件,以确保始终使用更新的条目ID值。

这是我的代码,移动后我需要EntryID

Sub Movetest1()


    Dim olApp As Outlook.Application
    Dim olns As Outlook.NameSpace


    Dim Fld As Folder
    Dim ofSubO As Outlook.MAPIFolder
    Dim myDestFolder As Outlook.Folder
    Dim ofolders As Outlook.Folders


    Dim objItems As Outlook.Items



    Dim myRestrictItems As Outlook.Items
    Dim i As Long


    Dim myitem As Object

'    Dim MailItem As Microsoft.Office.Interop.Outlook.MailItem

    Dim MailItem, moveditem As Outlook.MailItem




    Dim eid As String
    Dim sid As Variant
    Dim newEID As String


'---------------------------------------------------------------------------------------------------------


    Set olApp = New Outlook.Application
    Set olns = olApp.GetNamespace("MAPI")


    For Each Fld In olns.Folders
        If Fld.Name = "GSS Payables" Then

'
'            MsgBox Fld.Name
'            Debug.Print " - "; Fld.EntryID

            Set Fld = olns.GetFolderFromID("000000009DA6D76FBE7A58489450CDF6094F592A0100A2457DC435B22448A832DB721D8185B1000000B6207D0000").Folders("Inbox")
            Exit For
        End If
      Next



Set objItems = Fld.Items


eid = "000000009DA6D76FBE7A58489450CDF6094F592A0700A2457DC435B22448A832DB721D8185B1000000B620800000A2457DC435B22448A832DB721D8185B100007FF773270000"
sid = "000000009DA6D76FBE7A58489450CDF6094F592A0100A2457DC435B22448A832DB721D8185B1000000B6207D0000"



Set myDestFolder = Fld.Folders("Bhagyashri")

'Set myitem = objItems.Find("[SenderName]='Microsoft Outlook '")

Set MailItem = olns.GetItemFromID(eid)




Set moveditem = MailItem.Move(myDestFolder)

"giving error here
 newID = moveditem.entryid




Debug.Print "newID -"; newID



' get mailitem.parent.storeid

MsgBox "done"


End

答案 2 :(得分:0)

使用以下语法:

Dim MoveToFolder As outlook.MAPIFolder
Dim MyItem As outlook.MailItem
Dim NewEntryID As String

NewEntryID = MyItem.Move(MoveToFolder).ENTRYID

执行MyItem.Move后,新的ENTRYID将返回到NewEntryID变量。