dcsimg
Page 2 of 2 FirstFirst 12
Results 41 to 55 of 55

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

  1. #41
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

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

    Quote Originally Posted by westconn1 View Post
    what if you ran the code again?
    Then I misunderstood the issue as I interpreted it as the OP's
    Ok I got it working, but it appends the text twice. How do I ensure it only does it once?
    as each run added the text twice.

    If prevention of reprocessing an item is necessary, then it is easy to add a boolean UserProperty to the processed mailitem. Then use that property as part of the filtering mechanism. This should be much quicker than doing a text search.

  2. #42
    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

    How do you run it? If you're in the code window, and hit F5, do you get that same error? It works as is for me.

    (Replying to #40)

  3. #43
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,853

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

    except I used SenderEmailAddress instead of Sender; Sender does not exist in my version
    i also did not have a sender property for mail items, which does of course, cause an error, only sendername, which requires additional code to try to return the senders email address

    i ran the code as posted in #40, i did not get the sub or function not defined error, mailitem.sender did cause an error, but that was expected

    check menu > tools > references for missing references, the common cause of that error
    step through the code one line at a time to find out which line causes the error
    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

  4. #44
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,853

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

    Then I misunderstood the issue as I interpreted it as the OP's
    i assumed the code had been run multiple times (testing or processing new emails), easy to see different meanings

    it would appear there are many differences in outlook versions

    @whig4life, use code tags when posting code, it is much easier for us to see if there are any errors in code when the indenting is showing, which is lost when just posting in the text
    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

  5. #45

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

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

    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
                End If
            End If
        End If
    Next
    End Sub
    I still cannot get it working. Apologies for the delay in posting. I am not sure what I have gotten wrong.
    Last edited by whig4life; Jan 27th, 2017 at 07:45 AM. Reason: Added more detail.

  6. #46
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,853

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

    I still cannot get it working.
    please explain, does that mean the code does not run? error occurs? wrong result? other?

    if it was my email inbox i would not want to loop all the items in that every time, much too many
    i would look at using the restrict method of the items collection

    as discussed above, does myitem actually have a sender property? 2 of us says it did not in the versions we are using, you need to check this as it will cause an error, what version of outlook are you using? is the sender sending from outside or through exchange server?
    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

  7. #47

    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 westconn1 View Post
    please explain, does that mean the code does not run? error occurs? wrong result? other?

    if it was my email inbox i would not want to loop all the items in that every time, much too many
    i would look at using the restrict method of the items collection

    as discussed above, does myitem actually have a sender property? 2 of us says it did not in the versions we are using, you need to check this as it will cause an error, what version of outlook are you using? is the sender sending from outside or through exchange server?
    Yes, it still says "sub or function not defined" and when I sent a test email, the text I put in there is not appended. So, I get that error on the one hand, and it also does not work.


    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
                End If
            End If
        End If
    Next
    End Sub

  8. #48
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,853

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

    ofolder should be a mapifolder like
    Dim oFolder As MAPIFolder
    yours would give userdefined type error

    but i can not see anywhere that should cause sub or function not defined error

    with the above change, your code runs without error upto myitem.sender as that is not valid in my version of outlook

    please answer
    what version of outlook are you using? is the sender sending from outside or through exchange server?
    what value do you have when the code gets to myitem.sender?
    msgbox myitem.sender
    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

  9. #49

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

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

    The sender is sending from outside exchange. I am using Outlook 2010.

    How do I replace myitem.sender with something that works? Please advise. Thank you.

    -----------------

    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
                End If
            End If
        End If
    Next
    End Sub

  10. #50
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,853

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

    what value do you have when the code gets to myitem.sender?
    msgbox myitem.sender
    if the sender is in your address book it may myitem,sender may return the name in the address book rather than their email address

    How do I replace myitem.sender with something that works?
    depends what is being returned by myitem.sender
    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

  11. #51

    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 westconn1 View Post
    if the sender is in your address book it may myitem,sender may return the name in the address book rather than their email address

    depends what is being returned by myitem.sender
    Okay, I admit I am confused. I am trying to insert text when I receive email from a particular email address. I want this text to apply to all messages in the email box from that address, one time per email only.

    I was directed to the macro that I have but it does not work still, and I am trying to see if I can get it to work.

    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
                End If
            End If
        End If
    Next
    End Sub

  12. #52

    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 westconn1 View Post
    if the sender is in your address book it may myitem,sender may return the name in the address book rather than their email address

    depends what is being returned by myitem.sender
    Okay, I admit I am confused. I am trying to insert text when I receive email from a particular email address. I want this text to apply to all messages in the email box from that address, one time per email only.

    I was directed to the macro that I have but it does not work still, and I am trying to see if I can get it to work.

    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
                End If
            End If
        End If
    Next
    End Sub

  13. #53
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,853

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

    i can not see any reason why your code should not do as you want unless it is not matching the sender

    you could do a count of how many times the sender is matched, like
    Code:
    Sub AppendMail()
    Dim oApp As Application
    Dim oNS As NameSpace
    Dim oFolder As Folder
    Dim myItem As Object
    dim cnt as long
    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
                cnt = cnt +1
                If InStr(myItem.Body, "CODE:") = 0 Then
                    myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
                    myItem.Save
                End If
            End If
        End If
    Next
    msgbox "sender matched " & cnt & " times"
    End Sub
    if you get 0 matches, run this code just as a test, select an email from the sender you want first

    Code:
    Sub testsender()
    Dim myitem As MailItem
    Set myitem = Application.ActiveExplorer.Selection(1)
    MsgBox myitem.Sender
    End Sub
    this well show what myitem.sender should compare to, is it an email address or just a name from your address book?
    post exactly what is in the messagebox

    if the sender is matched correctly, then will have to try to find why it is not updating the message bodies
    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

  14. #54

    Thread Starter
    Junior Member
    Join Date
    Dec 2016
    Posts
    25

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

    When I run the debugger, the following portions are skipped:

    Code:
    If InStr(myItem.Body, "CODE:Testing") = 0 Then
                    myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
                    myItem.Save

    Full --V
    ---------------------------

    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:Testing") = 0 Then
                    myItem.Body = myItem.Body & vbCrLf & vbCrLf & "CODE: TESTING"
                    myItem.Save
                End If
            End If
        End If
    Next
    End Sub

  15. #55
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,853

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

    the following portions are skipped:
    that would indicate the the email address never matches the myitem.sender

    what is the exact format of the returned value of myitem.sender? see post #53
    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

Page 2 of 2 FirstFirst 12

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