Automatic Greeting "Hello Name," when Reply All is clicked

Post number 2 has been selected as the best answer.

Status
Not open for further replies.

pp_44

New Member
Outlook version
Outlook on the web
Email Account
Exchange Server
Hi all,

The code below adds "Hello FirstName," to the email body when replying to an email. Nice.

The issue however is, that it opens two reply windows. The code does its work twice.

What has to be changed to the code so it only happens once?

Thanks

Public WithEvents GExplorer As Outlook.Explorer Public WithEvents GMailItem As Outlook.MailItem Private Sub Application_Startup() Set GExplorer = Outlook.Application.ActiveExplorer End Sub Private Sub GExplorer_SelectionChange() Dim xItem As Object On Error Resume Next Set xItem = GExplorer.Selection.Item(1) If xItem.Class <> olMail Then Exit Sub Set GMailItem = xItem End Sub Private Sub GMailItem_Reply(ByVal Response As Object, Cancel As Boolean) AutoAddGreetingtoReply Response End Sub Private Sub GMailItem_ReplyAll(ByVal Response As Object, Cancel As Boolean) AutoAddGreetingtoReply Response End Sub Sub AutoAddGreetingtoReply(Item As Object) Dim oMail As MailItem Dim oReply As MailItem Dim GreetTime As String Dim olInsp As Inspector Dim wdDoc As Object Dim oRng As Object Select Case Application.ActiveWindow.Class Case olExplorer Set oMail = ActiveExplorer.Selection.Item(1) End Select Set oReply = oMail.ReplyAll With oReply Set olInsp = .GetInspector Set wdDoc = olInsp.WordEditor Set oRng = wdDoc.Range .Display With oRng .collapse 1 .Text = "Hello " & Split(oMail.SenderName)(0) & "," & vbCr & vbCr .collapse 0 .Select End With End With lbl_Exit: Set oMail = Nothing Set oReply = Nothing Set olInsp = Nothing Set wdDoc = Nothing Set oRng = Nothing Exit Sub End Sub
 
Here is code that works perfectly. For anyone who might need it.

In Outlook press ALF+F11, then find "ThisOutlookSession", paste it there and then restart Outlook. (Make sure you have enabled Macros in Outlook)

Code:
Public WithEvents GExplorer As Outlook.Explorer

Public WithEvents GMailItem As Outlook.MailItem

Private Sub Application_Startup()

    Set GExplorer = Outlook.Application.ActiveExplorer

End Sub

Private Sub GExplorer_SelectionChange()

    Dim xItem As Object

    On Error Resume Next

    Set xItem = GExplorer.Selection.Item(1)

    If xItem.Class <> olMail Then Exit Sub

    Set GMailItem = xItem

End Sub

Private Sub GMailItem_Reply(ByVal Response As Object, Cancel As Boolean)

    AutoAddGreetingToReply Response

End Sub

Private Sub GMailItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)

    AutoAddGreetingToReply Response

End Sub

Sub AutoAddGreetingToReply(Item As Object)

    Dim xGreetStr As String

    Dim xReplyMail As MailItem

    Dim xSenderName As String

    Dim xRecipient As Recipient

    On Error Resume Next

    If Item.Class <> olMail Then Exit Sub

    Set xReplyMail = Item

    For Each xRecipient In xReplyMail.Recipients

        If xSenderName = "" Then

            xSenderName = Split(xRecipient.Name)(0)

        End If

    Next xRecipient

    Select Case Time

           Case 0.3 To 0.5

                xGreetStr = ""

           Case 0.5 To 0.75

                xGreetStr = ""

           Case Else

                xGreetStr = ""

    End Select

    With xReplyMail

        .Display

        .HTMLBody = "<HTML><Body>Hello " & xSenderName & ",</HTML></Body>" & xGreetStr & .HTMLBody & vbCr & vbCr

        SendKeys "{DOWN}", True

        SendKeys "{DOWN}", True

        .Select

    End With

End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
P How to get a QR code for automatic signin with Outlook for iOS Using Outlook 5
U Rolling Back Office broke automatic updates Using Outlook 4
Commodore Automatic switch between working offline/online Using Outlook 4
S Outlook 2007 - Automatic purge fail Using Outlook 0
A Automatic forwarding to different people on a rotational basis Using Outlook 2
B resend if no reply and send an automatic reminder Outlook VBA and Custom Forms 0
B Removing Recipients from an automatic response Outlook VBA and Custom Forms 2
J Automatic color category according to variable domains Outlook VBA and Custom Forms 4
N automatic response with an attachment based on the subject line Outlook VBA and Custom Forms 1
E How to send automatic emails in outlook 2010 Using Outlook 1
N VBA Script to Send Automatic Emails from Outlook 2010 Outlook VBA and Custom Forms 1
M Outlook 2016 Desktop - Automatic Rule Processing Using Outlook 3
P Threat to being a spammer while sending automatic Emails through VBA Using Outlook 3
reza Macro to automatic reply using orignal mail Outlook VBA and Custom Forms 10
B Automatic picture download and changing email addresses Using Outlook 3
J why won't the automatic send/receive work Using Outlook 7
S rule, automatic replies, restricting them to within the company Using Outlook 1
I Automatic reply to an email Using Outlook 0
P Daily Automatic Reply Using Outlook 1
P Automatic reply on incoming messages in outlook using VBA(only Specifi person) Using Outlook 14
R automatic signature Using Outlook 0
B Looking for an add-in or a way to send automatic replies based off a list Using Outlook 2
H Automatic excel data added to Certain Emails that are received Using Outlook 3
M Automatic Bcc Using Outlook 2
C starting Outlook 2003 sp3 sets Junk Email setting to 'No automatic filtering' Using Outlook 3
K automatic cleanup folder Using Outlook 3
B How to automatic save Sending or replying contacts e-mails into contacts? Using Outlook 0
J Form design - how do I insert an automatic date/time field? Using Outlook 2
C Text reply mail in Outlook 2010 has automatic margin indent Using Outlook 9
P Automatic personalized reply Outlook VBA and Custom Forms 1
D Stop automatic opening BCM (Business Contact Manager) 1
P propagate automatic formatting to other Outlook folder Outlook VBA and Custom Forms 1
D Re: Propogate Automatic Formatting to Other Folder in Outlook 2007 Outlook VBA and Custom Forms 1
S Sending automatic email from Access using Outlook Outlook VBA and Custom Forms 1
T Code to import address book in automatic mode Outlook VBA and Custom Forms 1
G Send a greeting message to a contact on birthday Outlook VBA and Custom Forms 5
M Automatically add senders first name to a greeting Outlook VBA and Custom Forms 1
R New chap saying hello and needing advice on Outlook 2007 thumbnails Using Outlook 3
N Filter end date works differently in OL'07/OL'10 vs OL'03? Hello?! Using Outlook 12

Similar threads

Back
Top