Reply to mail item with a post item using VBA

255 views Asked by At

I would like to reply to a mailitem in my inbox with a postitem.

The behavior I'm looking to replicate:

  1. Selecting an email message in the active explorer

  2. Clicking "ctrl + T"

  3. Adding text to the post

  4. Clicking "post"

The resulting postitem contains the e-mail body of the email message replied to, the added text (#3 above) and also takes on the e-mail message's conversationID property, so it appears within the conversation.

I tried:

Sub ReplyToMessage()
 
    ' Declare Variables
    Dim objItem As Outlook.MailItem
    Dim objReply As Outlook.MailItem
   
    ' Get the selected message
    Set objItem = Application.ActiveExplorer.Selection.Item(1)
   
    ' Create a reply message
    Set objReply = objItem.Reply
   
    ' Set the message class to olPostItem
    objReply.MessageClass = "IPM.Post"    ' this errors out as
   
    ' Add text to the reply message
    objReply.Body = "Your reply message goes here."
   
    ' Display the reply message
    objReply.Display

End Sub

The above creates an e-mail reply, not a post.

I tried creating a new post item and copying the body, subject, etc., but when taking that approach, the post doesn't take on the conversationID of the message I'm replying to, which is critical.

I tried using sendkeys to send "^n", but that runs without response from Outlook.

To summarize: How can I write VBA to replicate what is occurring when "ctrl + T" is pressed with a mailitem selected in the active explorer, creating a postitem that replies in-conversation to a mailitem?

I am running Microsoft Office Professional Plus 2016 (16.0.5387.1000) and it is 64 bit.

1

There are 1 answers

0
Dmitry Streblechenko On

You need to convince Outlook to forget about that item

Set objReply = objItem.Reply
objReply.Body = "Your reply message goes here." & vbCrLf & objReply.Body
' Set the message class to olPostItem
objReply.MessageClass = "IPM.Post"    
objReply.Save
entryId = objReply.EntryID
'reopen as PostItem
set objReply = Nothing
set objReply = Application.Session.GetItemFromID(entryId)
objReply.Display