Automatically Insert Recipient Name from To Field

Status
Not open for further replies.
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
 
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
 
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!
 
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.
 
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.
 
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!!
 
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!
 
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.
 
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.
 
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.
 
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.
 
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.
Similar threads
Thread starter Title Forum Replies Date
C Automatically Insert Recipient Name from To Field Outlook VBA and Custom Forms 4
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
J Calendar events created on iPhone automatically changing default alert from 'None' to 'Time of Event' Using Outlook.com accounts in Outlook 0
Z Automatically adjust Outlook Reading Pane from bottom to right depending on portrait or landscape window Using Outlook 1
G Automatically delete email when a condition is met Outlook VBA and Custom Forms 1
Hornblower409 Automatically or Manually Backup Multiple Versions of VbaProject.OTM Outlook VBA and Custom Forms 1
B Outlook 2019 Automatically move email after assigning category Using Outlook 4
G automatically choosing "add to autocorrect" option Using Outlook 0
L Why are some email automatically going to "archive" Using Outlook 2
Z Outlook 365 Automatically assign categories to incoming mail in a shared folder Round Robin Outlook VBA and Custom Forms 1
G Automatically delete messages in the synchronization folder Outlook VBA and Custom Forms 3
E Remove flag automatically Using Outlook 4
T Outlook 365 Move newly created tasks automatically on save. Outlook VBA and Custom Forms 1
M Outlook 365 Switching from AOL to Yahoo automatically Using Outlook 5
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
Paul Hobbs Automatically accept "Empty Folders" prompt Outlook VBA and Custom Forms 6
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
D Custom Search Folders not refreshing/updating automatically Using Outlook 0
M Automatically add senders first name to a greeting Outlook VBA and Custom Forms 1
C Add Form to Appointments Received, Automatically Outlook VBA and Custom Forms 6
J Automatically forward email and apply template Outlook VBA and Custom Forms 0
Y Outlook 2013 Stop Outlook from automatically assigning categories to Tasks Using Outlook 0
O Forward a email with modified body Automatically. Outlook VBA and Custom Forms 0
A How to open a specific link automatically with outlook 2016 Outlook VBA and Custom Forms 6
P Automatically Categorize Meetings once they are accepted Outlook VBA and Custom Forms 5
W Automatically open attachments without automatically printing them Using Outlook 0
N How to set automatically the default or user defined Quickstyle Templates by Answer in Outlook Using Outlook 1
O Run macro automatically at sending an email Using Outlook 11
D Outlook 2016 automatically increment anniversaries Using Outlook 1
T Office 2013 no longer updating automatically Using Outlook 2
B Automatically Forward Emails and Remove/Replace All or Part of Body Outlook VBA and Custom Forms 8
D Print attachments automatically and moves the mail to a new folder Outlook VBA and Custom Forms 9
A How to open a specific link automatically with outlook Outlook VBA and Custom Forms 13
N how to sync automatically when outlook opens Using Outlook 10
A Sort emails into subfolders based on sender and deleting emails automatically Outlook VBA and Custom Forms 3
undercover_smother Automatically Forward All Sent Mail and Delete After Send Outlook VBA and Custom Forms 10
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
stephen li VBA Outlook send mail automatically by specified outlook mail box Outlook VBA and Custom Forms 1
R Make Enter Network Password Prompt Go Away Automatically Using Outlook 0
I Print Automatically Attachments Outlook VBA and Custom Forms 3
S Automatically selecting folders and deleting messages in Outlook VBA Outlook VBA and Custom Forms 7
M Outlook 2016 Rules Not Working Automatically Using Outlook 5
Diane Poremsky Automatically create a task when sending a message Using Outlook 0
D Is it possible to automatically send an email when it is moved to a folder? Exchange Server Administration 1
A Automatically send email based on drop-down field? Outlook VBA and Custom Forms 2
M Automatically create event in calendar when task is created Outlook VBA and Custom Forms 1
Diane Poremsky Create Appointment From Email Automatically Using Outlook 0
Cameron Piper Automatically update custom forms across multiple computers Outlook VBA and Custom Forms 1
T Automatically open link in email received Outlook VBA and Custom Forms 33

Similar threads

Back
Top