Consulting

Results 1 to 3 of 3

Thread: sub to reply, add CC and content but loses original message and HTML formating.

  1. #1
    VBAX Regular
    Joined
    Sep 2016
    Posts
    7
    Location

    sub to reply, add CC and content but loses original message and HTML formating.

    Good afternoon,

    I currently use this code to reply to a selected email, add a CC address, attach the original message and add some predetermined text in the response:

    Sub Redirect()
    
    
    Dim mymail As Outlook.MailItem
    
    
    Dim myReply As Outlook.MailItem
    
    
    Dim numItems As Integer
    
    
    Set mySelected = Outlook.ActiveExplorer.Selection
    
    
    numItems = mySelected.Count
    
    
    For i = 1 To numItems
    
    
    Set mymail = mySelected(1)
    
    
    Set myReply = mymail.Reply
    
    
    mytext = myReply.Body
    
    
    With myReply
    
    
    
    
    mytext = "Hello," & vbCrLf
    
    
    mytext = mytext & vbCrLf & "I have received your request and forwarded it to Blah Blah, who will handle it from here."
    
    
    
    
    
    
    
    
    End With
    
    
    
    
    myReply.Body = mytext
    Dim Reply As Outlook.MailItem
    Dim Original As Outlook.MailItem
    Set Original = Application.ActiveExplorer.Selection(1)
    Set Reply = Original.Reply
    myReply.Attachments.Add Original
    myReply.Recipients.Add "email address"
    
    
    myReply.Display
    
    
    
    
    
    
    Set mymail = Nothing
    
    
    Set myReply = Nothing
    
    
    Next
    
    
    Set mySelected = Nothing
    
    
    End Sub
    My only issues are that the response text is not in HTML format, I lost my signature and the original content is not underneath my signature any more.

    Anybody has a solution? I would really appreciate the help!

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    This replaces the body
    myReply.Body = mytext
    Instead try
    myReply.Display ' This generates the signature
    
    myReply.HTMLBody = mytext & myReply.HTMLBody
    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.

  3. #3
    Personally I would take a different approach:
    Option Explicit
    
    Sub Redirect()
    Dim myMail As Outlook.MailItem
    Dim myReply As Outlook.MailItem
    Dim mySelected As Outlook.Selection
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    Dim i As Long
    Dim myText As String
        myText = "Hello," & vbCrLf
        myText = myText & vbCrLf & "I have received your request and forwarded it to Blah Blah, who will handle it from here."
        Set mySelected = Outlook.ActiveExplorer.Selection
        For i = 1 To mySelected.Count
            Set myMail = mySelected(i)
            Set myReply = Outlook.CreateItem(olMailItem)
            With myReply
                .BodyFormat = olFormatHTML
                .To = myMail.SenderEmailAddress
                .subject = "Re: " & myMail.subject
                'blind copy to the person it was forwarded to
                .Recipients.Add("someone@somewhere.com").Type = 3
                Set olInsp = .GetInspector
                Set wdDoc = olInsp.WordEditor
                Set oRng = wdDoc.Range(0, 0)
                .Display
                oRng.Text = myText
                .Attachments.Add myMail
            End With
        Next i
    lbl_Exit:
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
        Set myMail = Nothing
        Set myReply = Nothing
        Set mySelected = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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