Automatically Insert Recipient Name from To Field

Status
Not open for further replies.

learning321A

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Okay, I have been trying, and I added that piece as well but it still does not do anything. no errors, but when I hit reply or reply all, nothing changes. I tried to add a picture here but it only lets me add urls. I have this code all in the ThisOutlookSession box. What am I doing wrong? I have macro security set to low, and normal modules execute when i click on Run.

Private Sub afterReply()
oResponse.Display
' get the recipient names
Dim Recipients As Outlook.Recipients
Dim R As Outlook.Recipient
Dim i
Dim strTo As String, strCC As String

Set Recipients = oResponse.Recipients
For i = 1 To Recipients.Count
Set R = Recipients.Item(i)

Debug.Print R.Name, R.Type

If R.Type = olCC Then
strCC = R.Name & ", " & strCC
Else
strTo = R.Name & ", " & strTo
End If
Next

'insert the names
Dim olInspector As Outlook.Inspector
Dim olDocument As Word.Document
Dim olSelection As Word.Selection

Set olInspector = Application.ActiveInspector()
Set olDocument = olInspector.WordEditor
Set olSelection = olDocument.Application.Selection

olSelection.InsertBefore "CC: " & strCC
olSelection.InsertParagraphBefore
olSelection.InsertBefore "To: " & strTo

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Do you also have the simple reply code at VBA Sample: Do Something When Reply is Clicked - the macro you posted above goes at the end of the simple reply code (replacing the small macro of the same name in the simple sample).

This is the code you need - put it in thisoutlooksession and restart outlook then hit reply to a message.

Code:
Option Explicit
Private WithEvents oExpl As Explorer
Private WithEvents oItem As MailItem
Private bDiscardEvents As Boolean
Dim oResponse As MailItem
 
Private Sub Application_Startup()
   Set oExpl = Application.ActiveExplorer
   bDiscardEvents = False
End Sub
 
Private Sub oExpl_SelectionChange()
   On Error Resume Next
   Set oItem = oExpl.Selection.Item(1)
End Sub
 
' Reply
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.Reply
afterReply
End Sub

Private Sub oItem_Forward(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.Forward

afterReply
End Sub

Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.ReplyAll

afterReply
End Sub

Private Sub afterReply()
oResponse.Display
' get the recipient names
Dim Recipients As Outlook.Recipients
Dim R As Outlook.Recipient
Dim i
Dim strTo As String, strCC As String

Set Recipients = oResponse.Recipients
For i = 1 To Recipients.Count
Set R = Recipients.Item(i)

Debug.Print R.Name, R.Type

If R.Type = olCC Then
strCC = R.Name & ", " & strCC
Else
strTo = R.Name & ", " & strTo
End If
Next

'insert the names
Dim olInspector As Outlook.Inspector
Dim olDocument As Word.Document
Dim olSelection As Word.Selection

Set olInspector = Application.ActiveInspector()
Set olDocument = olInspector.WordEditor
Set olSelection = olDocument.Application.Selection

olSelection.InsertBefore "CC: " & strCC
olSelection.InsertParagraphBefore
olSelection.InsertBefore "To: " & strTo

End Sub
if nothing happens when you hit reply, add this at the end of the macros above, select a message and click Step into. Does it skip any lines?

Code:
Public Sub TestMacro()
    Dim objOL As Outlook.Application
    Dim oItem As MailItem
    Set objOL = Outlook.Application
    Set oItem = objOL.ActiveExplorer.Selection.Item(1)
    Set oResponse = oItem.ReplyAll
 
    afterReply

   Set objOL = Nothing
End Sub
 

learning321A

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Awesome awesome awesome!!! Holy schneikes, it worked!! =) Yay! :):D You are the best, thank you so much! Noone else on the net could give an answer on this. You must be the only true Outlook VBA expert out there, thank you for sharing your knowledge with the rest of us.

With this code, I can now manipulate it to have specific wording, etc (like Dear, etc). I wanted to ask, though....I see you have many different subs in here. Is it possible to pull some of this out of the main outlooksession module and put it into a normal module? Reason I ask is because the users may not want to have this happen on ALL of their replies. If I can make it an on-demand macro, I can just give them hotkeys or quick access buttons to run this version of a reply when they need to. I know, I ask the world! Please let me know if that can be done.

I am so glad I found your site, thank you thank you!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
For on-demand, use the TestMacro sub to call up the afterreply macro. ReplyAll should work for most messages, single recipient or more, but you can duplicate the TestMacro for Reply and Forward if necessary. (And give it a better name. :))

Because AfterReply is a private macro, it is only 'seen' by macros in the module with it - but it can be moved to a new module, along with TestMacro.

if you need it to work with selected or opened messages, that is possible too. See [URL='https://www.slipstick.com/developer/outlook-vba-work-with-open-item-or-select-item/#getcurrentitem'] https://www.slipstick.com/developer/outlook-vba-work-with-open-item-or-select-item/ [/URL]for the GetCurrentItem function.
 

learning321A

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Yup, that worked once again your awesomeness.
Next question...Could this functionality be incorporated further? Like, could it call a template and THEN insert the greetings? Or, perhaps better, could it run as it does now, but then ALSO call a specific signature and insert it? That one might be better.

I am reading through your developer page now to find this stuff if it exists.
 

learning321A

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Thanks, I tried to use this, but got an error saying file not found on this line:
Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & "My Sig.htm")

When you say to use the getcurrentitem function, how do I do that...do I copy and paste that somewhere? Sorry, still trying to learn!!
 

learning321A

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Hmm, I might have figured out a part of this....but ran into a different issue. Is there an item I can use other than the strTO, so I can call out each alias one by one? StrTO seems to pull the entire line of all people. Is it possible to just pull one name? That way I could insert things like 'and' so it would be Dear "name1" and "name2".
Does that make sense?
Thanks! And I understand if you cannot spend time on this anymore, you have been more than amazing!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This gets the names into a string - strTo = R.Name & ", " & strTo - you can replace the "," part with " and ", but then you need to account for more than one name. You could count the # of recipients in each field first then use if statements to build the string, based on the # of entries. i don't have any code samples that do that though.
 

learning321A

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Thanks, I will give that a shot. Do you know what this issue means?
I got an error saying file not found on this line:
Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & "My Sig.htm")
I found the path and the sig file names and tried to change My Sig.htm to the actual names.htm but that did not work. Tried the .rtf files too but no joy.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
don't use the RTF files - you need to use html.

The error is definitely about the path.
Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & "My Sig.htm")
strsigfilepath is looking at enviro & "\Microsoft\Signatures\", where enviro is your user account's appdata path:
enviro = CStr(Environ("appdata")) which translates to:
C:\Users\username\AppData\Roaming\Microsoft\Signatures\
and the signature by that name must exist

you can replace strSigFilePath & "My Sig.htm" with the full path to the html file you plan to use.
 

learning321A

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Yay, I got that to work, woot woot!! Is there a way to get this signature piece to be input into the after reply sub somehow? So basically, it would say dear blah blah, then insert the signature.

Also, I did try some if stmts to get the names to separate out but could not get that part to work.
 

learning321A

Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Hi Diane, hoping you will take a look at this. I have gotten everything to work now with a macro I have put together using your info along with other info and some of my own, trying to get to a specific end point for my users. The one piece I am missing now is how to get the reply to pull the individual names from just the TO field and assign them a value that I can then place somewhere in the body. So, say I have two recipients. I might have the first person go in the greeting, but the second person go in the body somewhere. I tried to use some of your code you taught me, with the strTO item, but when I try to add it, I keep getting an object error. Can you help at all to nail down this final piece of my puzzle? I have been able to use the strgreetnameall, the sendername, and recipients, but those are getting me ALL of the people, not just the TO people, and not individually. How can I use the strTO item to add to other code?

Thanks so much!!
 
Status
Not open for further replies.
Top