Jump to content

Recommended Posts

I am looking to add code into Outlook. I want to use visual basic to check an e-mail's body text and print if it contains the word "cash". It will contain either that or PayPal.

 

So basically all I wanna know is what to put in the if statement to check for it. I have the code to use to print it.

 

I imagine it would be something like " if mail.body.text = cash" or something like that.

 

I'm new to Visual Basic so any help will be welcomed. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/297491-visual-basic-check-e-mail-body-text/
Share on other sites

The answer is it depends. I know a good bit about VBScript as opposed to Visual Basic, but it is mostly transferable.

 

If you only need to know if the body contains the word "cash" you would still need to account for variations such as "Cash", "CASH", etc. So, I would do the following: Convert the body to all lower or upper case characters. Then check for the target word. That is the simplest solution, but has one downfall. If 'cash' is contained in another word as opposed to being its own word it would still provide a match. To work around that you would need to use Regular Expression which is more complicated. Based on your actual need you may need to go that route.

 

Note that if this is for some type of spam detection process you will likely get a lot of emails falling through the cracks since they will use other characters to get around detection, e.g. "ca$h".

 

So, you could use the LCase function to convert the body to all lower case and then the InStr function to see if your target word is contained in the content. Here is some mock code:

 

 

VAR bodyText = LCase(mail.body.text)
IF InStr (bodyText, "cash", vbTextCompare) Then
    ' "cash" does exist in the body
Else
    ' "cash" does not' exist in the body
End If

Ok thanks. It will contain either cash or paypal depending on the method the customer chooses. The email gets printed out, and shouldn't be printed if it says paypal. We want to print the email only when the matching paypal receipt is recieved. Do you know some VB code that will check for the matching email once the paypal email has arrived? They will both either have the order ID in the subject or body text.

I've tried the following code to see if a message box appears when an email comes in with "cash" in the body text (have a rule set up to check them). But its not working, any help please?

Function payment()

    Var bodyText = LCase(Mail.Body.Text)
    If InStr(bodyText, "cash", vbTextCompare) Then
    
        MsgBox "Paid by Cash"
        
    Else
    
        MsgBox "Paid by PayPal"
        
    End If

End Function

Sub CheckMail(MyMail As MailItem)

    MsgBox (payment)

End Sub

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.