Search code examples
vbaemailhyperlinkoutlookmove

Outlook VBA link to moved mail


I want to move an mail item to an different folder and then return a link to the moved mail. Without moving it works as:

Dim objMail As Outlook.MailItem
Dim sFrag As String
Set objMail = Application.ActiveExplorer.Selection.Item(i_item) 
sFrag =  "<a href='outlook:" + objMail.EntryID + "'>"   + objMail.Subject + "</a>"" 

Here there string sFrag provides on proper hyperlink to to a valid Outlook element. If I click on an hyperlink containing this property the element is opened in outlook.

However if I extend this to:

Dim objMail As Outlook.MailItem
Dim sFrag As String   
Dim oOlApp As Outlook.Application
Dim targetFolder As folder

Set objMail = Application.ActiveExplorer.Selection.Item(i_item)
Set oOlApp = Outlook.Application
Set objNmSpc = oOlApp.GetNamespace("MAPI")
Set targetFolder = objNmSpc.PickFolder
objMail.Move targetFolder
sFrag = "<a href='outlook:" + objMail.EntryID + "'>"  + objMail.Subject + "</a>" 

After that the link in sfrag fails. If I want to open this link, an Outlook-windows displays Operation failed. Seems that objMail.EntryID is not updated correctly after the objMail.Move command.

Why? How to fix this?


Solution

  • Move is a function, not a sub - it retruns the new item:

    set objMail = objMail.Move(targetFolder)