dcsimg
Page 1 of 2 12 LastLast
Results 1 to 40 of 55

Thread: Outlook 2010 - Append text to body of email - without erasing email

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Outlook 2010 - Append text to body of email - without erasing email

    Hello,

    I would like to write a Macro that automatically inserts specific text into the body of an email from a certain address without erasing any of the email text. I am having trouble writing this one, and could use some advice. Please advise, thank you.

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Say more. Do you want to add some text to the body, then forward the email, or what exactly?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Apologies. I just want to add a database tag to the body. We have a program that will sort it once the tag is applied.

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Try something like this in a module:

    Code:
    Sub oLook()
        Dim oApp As Application
        Dim oNS As NameSpace
        Dim oFolder As Folder
        Dim myItem As Object
        
        Set oApp = Outlook.Application
        Set oNS = oApp.GetNamespace("MAPI")
        Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
        For Each myItem In oFolder.Items
            If TypeName(myItem) = "MailItem" Then
                On Error Resume Next
                If myItem.Sender = "Sender Name" Then   'change this line ***
                    If Err.Number <> 0 Then
                        Err.Clear
                        'write this record to an error log?
                    Else
                        myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: XYZ"   'appends code to end of body
                    End If
                End If
            End If
        Next
    End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Hello,

    Thank you for the reply. I tried this but it didn't quite work. Would there be anything else I would need to do to make it function?

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    What did happen? Are you running it from Outlook, or elsewhere?

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Running from a module in Outlook. I made the changes I needed to make, but when I tested it with that email address, the text was not in the body of the email. I am sure it is something I am missing, in terms of background knowledge. I am still dusting off the VBA skills. Thank you for your help so far.

  8. #8
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    can you show your code as is?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    This is what I have:

    Code:
    Sub oLook() Dim oApp As Application Dim oNS As NameSpace Dim oFolder As Folder Dim myItem As Object Set oApp = Outlook.Application Set oNS = oApp.GetNamespace("MAPI") Set oFolder = oNS.GetDefaultFolder(olFolderInbox) For Each myItem In oFolder.Items If TypeName(myItem) = "MailItem" Then On Error Resume Next If myItem.Sender = "pmarrone5488@gmail.com" Then If Err.Number <> 0 Then Err.Clear Else myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING" End If End If End If Next End Sub

  10. #10
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Just a guess, but won't you need to call MailItem.Save after making any changes?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Quote Originally Posted by vbfbryce View Post
    can you show your code as is?
    Sub oLook()
    Dim oApp As Application
    Dim oNS As NameSpace
    Dim oFolder As Folder
    Dim myItem As Object

    Set oApp = Outlook.Application
    Set oNS = oApp.GetNamespace("MAPI")
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    For Each myItem In oFolder.Items
    If TypeName(myItem) = "MailItem" Then
    On Error Resume Next
    If myItem.Sender = "pmarrone5488@gmail.com" Then
    If Err.Number <> 0 Then
    Err.Clear
    Else
    myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
    End If
    End If
    End If
    Next
    End Sub

  12. #12
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    I think Tin is correct in post #10, you need to add a "save" in the loop.

    I didn't catch that because i was opening the emails one by one to verify that the text was appended.

    Like so:

    Code:
    myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: XYZ"
                        myItem.Save
    Last edited by vbfbryce; Dec 8th, 2016 at 10:46 AM.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Quote Originally Posted by vbfbryce View Post
    I think Tin is correct in post #10, you need to add a "save" in the loop.

    I didn't catch that because i was opening the emails one by one to verify that the text was appended.
    I see. So, like this: "If TypeName(myItem) = "MailItem.save" Then"

    Please let me know if I am correct in my understanding.

  14. #14
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    I edited in #12 while you were posting.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Quote Originally Posted by vbfbryce View Post
    I edited in #12 while you were posting.
    Thank you very much. Now, once I press run it will be appended, no? Or only on new incoming emails?

  16. #16
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    When you run, it should append to any that are already in your inbox.

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Quote Originally Posted by vbfbryce View Post
    When you run, it should append to any that are already in your inbox.
    It's not working.

  18. #18
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    put a "stop" in front of this line:

    Code:
    If myItem.Sender = "pmarrone5488@gmail.com" Then
    hover over the myitem.sender and see what the value of it is. When I run my code, the sender does not include the "@gmail.com."

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Quote Originally Posted by vbfbryce View Post
    put a "stop" in front of this line:

    Code:
    If myItem.Sender = "pmarrone5488@gmail.com" Then
    hover over the myitem.sender and see what the value of it is. When I run my code, the sender does not include the "@gmail.com."
    When I hover over it, it only says: vbCrlf=""

    Should I remove the @gmail.com?

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Name:  VBACapture-1.jpg
Views: 70
Size:  29.1 KB

  21. #21
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    add a "watch" for "myitem" and when it stops, see whether myitem has been "set" to a valid object or not.

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Ok I got it working, but it appends the text twice. How do I ensure it only does it once?

  23. #23
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    You'd need to "mark" the ones that you append to, and skip over them on subsequent passes. You could add something to the subject, or the body, or something else maybe.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    I'm not sure I understand completely.

  25. #25
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    As an example, the first time you run the code, any emails that were appended to could have the subject changed, something like:

    Code:
    myitem.subject="APP - " & myitem.subject
    You could add a check that would then skip any emails whose subject started with "APP - " like so:

    Code:
    if left(myitem.subject,6)<>"APP - " then
       'append
    end if

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Ok so, I thought it was working. But instead of appending the text only from those emails from that particular address, it actually appended it to all emails in my inbox. How can I change this?

  27. #27
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    If your If...Thens line up correctly, it shouldn't be possible, as long as you still have something like this:

    Code:
    If myItem.Sender = "pmarrone5488@gmail.com" Then
    If your current code has changed at all, please show the updated version (but not in an image, I can't read it there).

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Quote Originally Posted by vbfbryce View Post
    If your If...Thens line up correctly, it shouldn't be possible, as long as you still have something like this:

    Code:
    If myItem.Sender = "pmarrone5488@gmail.com" Then
    If your current code has changed at all, please show the updated version (but not in an image, I can't read it there).
    Absolutely:

    Sub AppendMail()
    Dim oApp As Application
    Dim oNS As NameSpace
    Dim oFolder As Folder
    Dim myItem As Object

    Set oApp = Outlook.Application
    Set oNS = oApp.GetNamespace("MAPI")
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    For Each myItem In oFolder.Items
    If TypeName(myItem) = "MailItem" Then
    If myItem.Sender = "pmarrone5488@gmail.com" Then
    On Error Resume Next
    If Err.Number <> 0 Then
    Err.Clear
    Else
    myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE:TESTING"
    myItem.Save
    End If
    End If
    End If
    Next
    End Sub


  29. #29
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    It doesn't look like you added the check from #25.

  30. #30
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,847

    Re: Outlook 2010 - Append text to body of email - without erasing email

    If myItem.Sender = "pmarrone5488@gmail.com" Then
    If Err.Number <> 0 Then
    Err.Clear
    Else
    myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
    End If
    End If
    the logic here is not quite correct, and only seems to get worse with edits

    it checks the sender, then if no error edits the body, regardless of who the sender is, SO all emails are edited
    i see no specific reason to have inline error checking when returning the sender, so i would remove the on error resume next altogether

    You'd need to "mark" the ones that you append to, and skip over them on subsequent passes.
    you should be able to check if a constant part of the appended text is already in the emails, rather than change the subject of the item

    Code:
    If myItem.Sender = "pmarrone5488@gmail.com" Then
        if instr(myitem.body, "CODE:") = 0 then myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
    End If
    i am assuming that some part of the appended string is unlikely to appear in the item body unless it has had the tag appended
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  31. #31
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Outlook 2010 - Append text to body of email - without erasing email

    This piqued my interest, so I thought that I give it a go. I tried various mechanisms including the one recommended by Bryce (except I used SenderEmailAddress instead of Sender; Sender does not exist in my version) and did not have an issue with double additions. Here is what I finally derived. I'm not making any claims that it is better or worse than what has been presented previously.

    Note that this was developed in Outlook 2007, so there may be some changes needed for other versions.

    Code:
    Sub EditReceived(fromEmailAddress As String, tag As String)
       
       Dim app As Application
       Set app = Outlook.Application
       
       Dim ns As Outlook.NameSpace
       Set ns = app.GetNamespace("MAPI")
       
       Dim inboxFolder As Outlook.Folder
       Set inboxFolder = ns.GetDefaultFolder(Outlook.olFolderInbox)
       
       Dim filter As String
       filter = "[SenderEmailAddress] = '" & fromEmailAddress & "'"
       
       Dim filteredItems As Outlook.Items
       Set filteredItems = inboxFolder.Items.Restrict(filter)
       
       Dim item As Outlook.mailitem
       
       For Each item In filteredItems
          item.Body = item.Body & vbCrLf & tag
          item.Save
       Next
    
    End Sub

  32. #32
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,847

    Re: Outlook 2010 - Append text to body of email - without erasing email

    and did not have an issue with double additions
    what if you ran the code again?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Hello West,

    Thank you for your help. would the below make sense? Please advise, thank you:

    Sub AppendMail()
    Dim oApp As Application
    Dim oNS As NameSpace
    Dim oFolder As Folder
    Dim myItem As Object

    Set oApp = Outlook.Application
    Set oNS = oApp.GetNamespace("MAPI")
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    For Each myItem In oFolder.Items
    If TypeName(myItem) = "MailItem" Then
    If myItem.Sender = "pmarrone5488@gmail.com" Then
    If InStr(myItem.Body, "CODE:") = 0 Then myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
    End If
    End If
    Next
    End Sub

  34. #34
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Close, but you forgot to save each mail item after adding the CODE to it:

    Code:
    Sub AppendMail()
    Dim oApp As Application
    Dim oNS As NameSpace
    Dim oFolder As Folder
    Dim myItem As Object
    
    Set oApp = Outlook.Application
    Set oNS = oApp.GetNamespace("MAPI")
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    For Each myItem In oFolder.Items
        If TypeName(myItem) = "MailItem" Then
            If myItem.Sender = "pmarrone5488@gmail.com" Then
                If InStr(myItem.Body, "CODE:") = 0 Then
                    myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
                    myItem.Save   'must add this here ***
                End If
            End If
        End If
    Next
    End Sub

  35. #35
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    i see no specific reason to have inline error checking when returning the sender, so i would remove the on error resume next altogether
    Pete, for some reason I find some "mail items" which generate an error when trying to get the sender. Not sure why!

  36. #36

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Ok, home stretch! It now Says: "Sub or Function not defined", code below for reference.Name:  VBACapture-2.PNG
Views: 50
Size:  4.5 KB

    Sub AppendMail()
    Dim oApp As Application
    Dim oNS As NameSpace
    Dim oFolder As Folder
    Dim myItem As Object

    Set oApp = Outlook.Application
    Set oNS = oApp.GetNamespace("MAPI")
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    For Each myItem In oFolder.Items
    If TypeName(myItem) = "MailItem" Then
    If myItem.Sender = "pmarrone5488@gmail.com" Then
    If InStr(myItem.Body, "CODE:") = 0 Then myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
    myItem.Save
    End If
    End If
    End If
    Next
    End Sub

  37. #37
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    what line is giving that error?

  38. #38

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    I am not sure how to tell, it only shows that when I run the macro

  39. #39
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Outlook 2010 - Append text to body of email - without erasing email

    Copy my code exactly as it appears in #34. It doesn't even run the way you have it in #36, because your IF...THEN's don't match up.

  40. #40

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

    Re: Outlook 2010 - Append text to body of email - without erasing email

    I did that, it says "Sub or function not defined"

    ----------------
    Sub AppendMail()
    Dim oApp As Application
    Dim oNS As NameSpace
    Dim oFolder As Folder
    Dim myItem As Object

    Set oApp = Outlook.Application
    Set oNS = oApp.GetNamespace("MAPI")
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    For Each myItem In oFolder.Items
    If TypeName(myItem) = "MailItem" Then
    If myItem.Sender = "pmarrone5488@gmail.com" Then
    If InStr(myItem.Body, "CODE:") = 0 Then
    myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
    myItem.Save
    End If
    End If
    End If
    Next
    End Sub

Page 1 of 2 12 LastLast

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width