0

I'm trying to check if a particular mail item has already been forwarded and if not, to forward that mail item. I'm not having having much luck unfortunately.

This is my code:

Private Sub Forward_Mail(OItem As RDOMail)

Dim oForwardMail As RDOMail

Dim itemTags As Variant
Dim ForwardedTagMissing As Boolean
Dim CCField As String

On Error GoTo Release

  itemTags = OItem.GetProps("http://schemas.microsoft.com/mapi/proptag/0x10820040")(0)
  ForwardedTagMissing = IsError(itemTags)

  If ForwardedTagMissing Then

     Set oForwardMail = OItem.Forward

     With oForwardMail

       .Subject = OItem.Subject
       .HTMLBody = OItem.HTMLBody       
       .Recipients.Add "Test.Email@test.com"        
       .Display

     End With
   
     'Mark unread
     OItem.UnRead = False
   
  End If

  Set oForwardMail = Nothing

End Sub

The problem with this code is that it sets the mail item to forwarded, even if the mail item hasn't been forwarded yet. Any ideas what I'm doing wrong?

EC99
  • 137
  • 10
  • What is the value of itemTags? Are you only looking for forwarded emails? Or replied as well? – Dmitry Streblechenko Apr 25 '22 at 14:53
  • Just forwarded emails. I'm checking for if there's a property for the forwarded date and time and if that is an error, then it hasn't been forwarded yet. Is there a better way to do this? – EC99 Apr 25 '22 at 14:59
  • I've described the mechanics of getting such data from Extended MAPI. Redemption and Outlook are just wrappers around that API. – Eugene Astafiev Apr 26 '22 at 16:20

2 Answers2

0

The property you need to read would be PR_LAST_VERB_EXECUTED (0x10810003). You are interested in the EXCHIVERB_FORWARD = 104 value. The DASL property name is http://schemas.microsoft.com/mapi/proptag/0x10810003.

You may also check out the time when it forwarded, see PR_LAST_VERB_EXECUTED_TIME, the DASL name is http://schemas.microsoft.com/mapi/proptag/0x10820040.

You may find the Exporting Emails from Parent and SubFolders to Excel using VBA thread helpful.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Even if looking for this, OItem.Forward seems to mark the item as forwarded, even if it has not been sent yet. – EC99 Apr 25 '22 at 15:09
  • You can check out the `Sent Items` folder to make sure the item was sent out. – Eugene Astafiev Apr 25 '22 at 15:12
  • This isn't ideal. When using Outlook.MailItem, you could do `IsNull(propaccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10820040"))` and then the same as in my original code. This would **not** mark the mail item as forwarded, unless you send the item. However, it seems Redemption works differently and for some reason marks the item as forwarded, when it hasn't been yet. – EC99 Apr 25 '22 at 15:16
  • Redemption is just a wrapper around Extended MAPI. There are no other properties that can indicated that. – Eugene Astafiev Apr 25 '22 at 15:18
  • I think you've misunderstood my question Eugene. Your initial answer (and the code I posted) does exactly as I need in terms of finding out whether a mail item has been forwarded already. However, for some reason, when forwarding the mail item and displaying it, Redemption seems to mark the item as "Forwarded" already without even forwarding the item. If we were to use the Microsoft Outlook.Application method, this doesn't happen. Why does it happen with Redemption and how do I get around this? – EC99 Apr 25 '22 at 15:26
  • I understood the post in the way it was written. Redemption which is actually a wrapper around Extended MAPI and the Outlook object model are different wrappers around a low-level API. It is expected to have different results. You can grab the [EntryID](https://www.dimastr.com/redemption/rdomail.htm) property value of the RDOMail and get a corresponding equivalent from the OOM using the [GetItemFromID](https://learn.microsoft.com/en-us/office/vba/api/outlook.namespace.getitemfromid) method. But the item should be saved to the store to get a valid ID and then find OOM's equivalent. – Eugene Astafiev Apr 26 '22 at 16:19
0

If you mean Outlook (and Redemption) marks the original message as forwarded before the user actually clicks the Send button on the new message, that is to be expected. Outlook simply disregards the change if the new message is cancelled.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Correct. This is the problem. However, when I cancel the new message, it doesn't disregard the change, the message stays forwarded forever. I've tried this overnight now too. It adds a timestamp to say the original forwarded time too. – EC99 Apr 26 '22 at 06:18
  • When using the standard office library and run `Outlook.MailItem.Forward` followed by `Outlook.MailItem.Display` it never marks the MailItem as forwarded, only when you've sent the message. Redemption seems to mark it as forwarded as soon as you run `RDOMail.Forward.Display`. Any ideas? – EC99 Apr 26 '22 at 08:12
  • It does appear to be a bug in Redemption. I am not sure however if a good workaround is possible - it can certainly delay stamping the original message until the new message is sent by calling RDOMail.Send, but if the new message is displayed, Redemption would have no way of knowing whether the user sent it or not. OOM is in a better position since it owns the inspector and knows whta the user clicks on. – Dmitry Streblechenko Apr 26 '22 at 14:56
  • Hmm is there a way to convert an RDOMail into an Outlook.MailItem and pass that to the sub in my original post? Then perhaps I could use OOM to forward the mention. However, would doing that trigger the object model guard? – EC99 Apr 26 '22 at 15:17
  • Sure, you can save it (if it is unsaved), then open it using `Application.Session.GetItemFromID` in OOM – Dmitry Streblechenko Apr 26 '22 at 15:33
  • Thanks Dmitry. How do I obtain the message's `EntryID` and `StoreID` for the `Application.Session.GetItemFromID` parameters? – EC99 Apr 26 '22 at 16:04
  • Use `RDOMail.EntryID` property. The second (store entry id) parameter is optional. But you can still retrieve it from `RDOMail.Store.EntryID` – Dmitry Streblechenko Apr 26 '22 at 16:26
  • This doesn't work either unfortunately. Doing it this way if you use `ForwardMail.Recipients.Add "TestEmailAddress@test.com"` it calls the object model guard, but correctly marks the message as forwarded when that's been sent. But if you use `ForwardMail.To = "TestEmailAddress@test.com"` to avoid the object model guard and then `.Display` and send manually, it adds a timestamp to the forwarded message, but Outlook does not recognize it as having been sent. – EC99 Apr 26 '22 at 16:35
  • Do you mean after `ForwardMail` had been sent and you open it in the Sent Items folder, Outlook shows it unsent? – Dmitry Streblechenko Apr 26 '22 at 16:48
  • It’s very strange - it displays the message on the mail item that says it’s been forwarded along with the time stamp it was forwarded in the inbox, but it doesn’t display the “Forwarded” icon in the mailbox on that message. And I run the sub above to check if it has the 0x10820040 proptag, it does not – EC99 Apr 26 '22 at 16:57
  • The icon is displayed when `PR_ICON_INDEX` property is set. In your case, it must be set to `262` to show the "forwarded" icon. – Dmitry Streblechenko Apr 26 '22 at 17:34