Outlook VBA - Get Login ID After Moving Object
Summary
I am trying to programmatically add hyperlinks to tasks generated from emails that I have moved to a different folder. The goal is for the task to contain a hyperlink linked to the appearance item that was moved to the Processed Email folder, but I'm having some problems. What would be the best way to get the Entry id of the object after the move?
Problem
However, I don't understand how to move the MailItem and then programmatically get the new EntryID after moving it. The "naive" way doesn't work. After using the Move method to move the MailItem object, the EntryID property does not reflect the change in identifier.
More details
Creating a hyperlink to an Outlook item using the format outlook:<EntryID>
is easy enough if the Outlook item stays in the Inbox, as I can simply get the Entry ID of the object I'm linking to. However, Outlook changes the EntryID when the object is moved. I want to understand how to get the updated ID so that I can build an exact link.
Example
See the following example code. The message fields indicate that the EntryID property of objMail still returns the same value, even though the object has been moved. However, running a separate mail macro in the destination folder confirms that the EntryID has changed during 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
Thanks in advance for your help!
source to share
The Move method of the MailItem class returns an object that represents the item that was moved to the specified folder. You need to check the EntryID value of the returned object, not the original one.
In any case, you might want to consider handling the target folder's ItemAdd event to ensure that the updated record ID value is always used.
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
source to share
Hi, please clarify your answer which I cannot understand.
In any case, you may want to consider handling the ItemAdd
target folder event to ensure that the updated entry id value is used at all times.
Here is my code and I need it 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
source to share