PDA

View Full Version : Insert Recipient's name in reply



drlex
06-30-2011, 08:16 PM
This code dosn't work in Outlook 2010
I get the error in
strGreetName = Left$(Msg.SenderName, InStr(1, Msg.SenderName, " ") - 1)

JP2112
07-01-2011, 06:02 AM
Can you paste your code directly into the post editor (and wrap it in code tags)? I can barely read it and anyway I can't cut and paste it to test it.

drlex
07-01-2011, 06:53 AM
code located on page
vbaexpress.com/kb/getarticle.php?kb_id=1046

JP2112
07-01-2011, 09:44 AM
Knew it looked familiar. What error do you get? Did you try stepping through the code?

drlex
07-01-2011, 04:41 PM
step1

drlex
07-01-2011, 04:42 PM
step 2

drlex
07-01-2011, 04:43 PM
debug - step 3 - look first post ...

drlex
07-01-2011, 04:56 PM
and try 2

JP2112
07-12-2011, 06:13 PM
Are you selecting an email (not a meeting request, post, etc) before running the code?

So I understand you correctly, when you run it again you don't get an error in the same place, you're getting an error in a later line of code? What is the error message?

KoolPal
07-07-2014, 08:04 AM
Hi,

I'm reviving an old thread since I am struggling with a derivative of this macro


Sub InsertNameInReply()

Dim Msg As Outlook.MailItem
Dim MsgReply As Outlook.MailItem
Dim strGreetName As String
Dim lGreetType As Long

' set reference to open/selected mail item
On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
Set Msg = ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set Msg = ActiveInspector.CurrentItem
Case Else
End Select
On Error GoTo 0

If Msg Is Nothing Then GoTo ExitProc

strGreetName = Mid$(Msg.SenderName, 2 + InStr(1, Msg.SenderName, ", ", Len(Msg.SenderName)) - 1)

Set MsgReply = Msg.Reply

With MsgReply
.subject = "RE:" & Msg.subject
.HTMLBody = "<span style=""font-family : verdana;font-size : 10pt""><p>Hello " & strGreetName & ",</p></span>" & .HTMLBody
.Display
End With

ExitProc:
Set Msg = Nothing
Set MsgReply = Nothing
End Sub

When I try to run this macro when I am in reply mode, I get the debug error in the following line:

Set MsgReply = Msg.Reply

Can anyone help me with this code such that it works on an open email as well?

Thanks.

westconn1
07-08-2014, 03:32 AM
first remove on error resume next, so if any error occurs earlier in the code it will not be hidden

KoolPal
07-08-2014, 08:09 AM
Sub InsertNameInReply()

Dim Msg As Outlook.MailItem
Dim MsgReply As Outlook.MailItem
Dim strGreetName As String
Dim lGreetType As Long

' set reference to open/selected mail item
'On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
Set Msg = ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set Msg = ActiveInspector.CurrentItem
Case Else
End Select
On Error GoTo 0

If Msg Is Nothing Then GoTo ExitProc

strGreetName = Mid$(Msg.SenderName, 2 + InStr(1, Msg.SenderName, ", ", Len(Msg.SenderName)) - 1)

Set MsgReply = Msg.Reply

With MsgReply
.subject = "RE:" & Msg.subject
.HTMLBody = "<span style=""font-family : verdana;font-size : 10pt""><p>Hello " & strGreetName & ",</p></span>" & .HTMLBody
.Display
End With

ExitProc:
Set Msg = Nothing
Set MsgReply = Nothing
End Sub

I commented out the following:
On Error Resume Next yet the macro failed on
Set MsgReply = Msg.Reply

Please advise what else could I do?

Thanks a lot for pitching in to help.

westconn1
07-08-2014, 02:23 PM
what error are you getting, it certainly looks correct and testing similar gives no error

try

If msg.Class = olMail Then set msgreply = msg.reply

of course on its own that will still cause an error on the next line, but at least you can then fix

KoolPal
07-08-2014, 10:09 PM
I am trying to run this macro when an email is in Reply mode. As far as I understand the vba code, it's trying to set the email in reply mode. I think this is causing the error.

Can you help me with this?

Thanks.

westconn1
07-09-2014, 01:50 AM
i guess that explains it, you can not reply to an email that is already a reply
you could try to reply after the mail is sent or explicitly saved

if an email is not received, there is no one to reply to

KoolPal
07-10-2014, 07:31 AM
Umm... I am seeking help to use this code when I AM in a reply mode. Can you please guide me with the necessary changes to the code provided?

westconn1
07-10-2014, 02:19 PM
Can you please guide me with the necessary changes to the code provided?what is it you want to achieve?
you are already in reply mode, so what else do you want to do?

please explain exactly what you want to achieve

KoolPal
07-13-2014, 03:13 AM
Perhaps my original post was not clear enough.

I am trying to insert a greeting using the code mentioned in this thread. I am ALREADY in reply mode when I WANT to use this code.

When I try to run this macro when I am in reply mode, I get the debug error in the following line:

Set MsgReply = Msg.Reply

westconn1
07-13-2014, 02:39 PM
Perhaps my original post was not clear enough.
i am still not clear on what you want to achieve,
try changing to


strGreetName = Mid$(Msg.SenderName, 2 + InStr(1, Msg.SenderName, ", ", Len(Msg.SenderName)) - 1)

'Set MsgReply = Msg.Reply

With Msg
' .subject = "RE:" & Msg.subject ' in reply mode,this should already be assigned
.HTMLBody = "<span style=""font-family : verdana;font-size : 10pt""><p>Hello " & strGreetName & ",</p></span>" & .HTMLBody
.Display
End With does this do what you want?

skatonni
07-15-2014, 02:57 PM
SenderName is on the original message not the reply, so this returns nothing.

strGreetName = Mid$(Msg.SenderName, 2 + InStr(1, Msg.SenderName, ", ", Len(Msg.SenderName)) - 1)
Debug.Print " strGreetName: " & strGreetName

westconn1
07-16-2014, 01:13 AM
SenderName is on the original message not the reply, so this returns nothing.good point, need to change to msg.recipients(1).name

KoolPal
07-18-2014, 06:50 AM
Thanks for helping guys, but no luck. Still getting an error.

westconn1
07-18-2014, 03:13 PM
Still getting an error.on which line? what error?

KoolPal
12-09-2014, 08:13 AM
Again reviving this thread. Can any expert help with a code to insert a Dear 'First Name' in a new mail / reply once the recipients are in the TO field?

westconn1
12-09-2014, 01:01 PM
post the code you have so far

gmayor
12-13-2014, 12:35 AM
Try the following, which is about as close as you can get.


Sub AddName()
Dim olEmail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim vName As Variant
On Error Resume Next
Set olEmail = ActiveInspector.CurrentItem
If Not olEmail.To = vbNullString Then
With olEmail
.BodyFormat = olFormatHTML
vName = Split(.To, Chr(32))
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.Text = "Dear " & vName(0) & vbCr
oRng.collapse 0
oRng.Select
.Display
End With
Else
MsgBox "There is no recipient in the 'To' field'"
End If
End Sub