Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 26

Thread: Insert Recipient's name in reply

  1. #1
    VBAX Regular
    Joined
    Jun 2011
    Posts
    6
    Location

    Unhappy Insert Recipient's name in reply

    This code dosn't work in Outlook 2010
    I get the error in
    strGreetName = Left$(Msg.SenderName, InStr(1, Msg.SenderName, " ") - 1)


    Attached Images Attached Images

  2. #2
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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.
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  3. #3
    VBAX Regular
    Joined
    Jun 2011
    Posts
    6
    Location
    code located on page
    vbaexpress.com/kb/getarticle.php?kb_id=1046

  4. #4
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    Knew it looked familiar. What error do you get? Did you try stepping through the code?
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  5. #5
    VBAX Regular
    Joined
    Jun 2011
    Posts
    6
    Location
    step1
    Attached Images Attached Images

  6. #6
    VBAX Regular
    Joined
    Jun 2011
    Posts
    6
    Location
    step 2
    Attached Images Attached Images

  7. #7
    VBAX Regular
    Joined
    Jun 2011
    Posts
    6
    Location
    debug - step 3 - look first post ...

  8. #8
    VBAX Regular
    Joined
    Jun 2011
    Posts
    6
    Location
    and try 2
    Attached Images Attached Images

  9. #9
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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?
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  10. #10
    VBAX Regular
    Joined
    Dec 2013
    Posts
    7
    Location
    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.

  11. #11
    first remove on error resume next, so if any error occurs earlier in the code it will not be hidden

  12. #12
    VBAX Regular
    Joined
    Dec 2013
    Posts
    7
    Location
    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.

  13. #13
    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

  14. #14
    VBAX Regular
    Joined
    Dec 2013
    Posts
    7
    Location
    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.

  15. #15
    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

  16. #16
    VBAX Regular
    Joined
    Dec 2013
    Posts
    7
    Location
    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?

  17. #17
    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

  18. #18
    VBAX Regular
    Joined
    Dec 2013
    Posts
    7
    Location
    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

  19. #19
    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?

  20. #20
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •