5
votes

Summary

I'm trying to add hyperlinks to tasks created from emails that I have moved to another folder.

The goal is to have the task contain a hyperlink to the Outlook item that was moved to a "Processed Email" folder".

Problem

I don't understand how to move a MailItem and then get its new EntryID after it moves.

The "naive" way doesn't work. After using the Move method to move a MailItem object, the EntryID property does not reflect a change in ID.

Details

Creating a hyperlink to an Outlook item using the format outlook:<EntryID> is easy enough if the Outlook item remains in the Inbox, since I can just get the EntryID of the object that I am linking to. However, Outlook changes the EntryID when an object is moved.

I want to understand how to get the updated ID so that I can construct an accurate link.

Example

The message boxes show the EntryID property of objMail returns the same value despite the fact that the object has moved. However, running a separate macro on the mail in the destination folder confirms that the EntryID has changed with the move.

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

3 Answers

6
votes

The Move method of the MailItem class returns an object that represents the item which has been moved to the designated folder. You need to check out the EntryID value of the returned object, not the source one.

Anyway, you may consider handling the ItemAdd event of the target folder to make sure that an updated entry ID value is used all the time.

 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
votes

Hello can you please elaborate your answer I am not able to understand it.

Anyway, you may consider handling the ItemAdd event of the target folder to make sure that an updated entry ID value is used all the time.

Here is my code and I need EntryID after moving.

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
0
votes

Use the following syntax:

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

NewEntryID = MyItem.Move(MoveToFolder).ENTRYID

After MyItem.Move is executed the new ENTRYID will be returned to the NewEntryID variable.