简体   繁体   中英

get certain text from outlook email body using vba?

I am using the following code to retrieve the company name from my email body. so for instance if I have

Company name: Hewden

then I want to get 'Hewden'

underneath company name in my email body I also have the following:

company number: 123
company status: live

kind regards,

automated email

I am using the following code to try and retrieve just the company name 'Hewden' without any of the preceding or after text.

i am getting an invalid procedure call or argument error, on the line b4 = olkmsgt.Body

and am not sure why, can someone please show me how to amend this to get it to do what i want.

thanks

Dim b4 As String
                        b4 = olkMsg.Body

    Dim indexOfNameb As Integer
        indexOfNameb = InStr(1, b4, "Company Name: ")

    Dim indexOfNamec As Integer
        indexOfNamec = InStr(1, b4, "Company number: ")

    Dim finalStringb As String

        finalStringb = Mid(b4, indexOfNameb, indexOfNamec - indexOfNameb)
        Dim LResult36 As String
        LResult36 = Replace(finalStringb, "Company Name: ", "")

       excWks4.Cells(intRow4, 1) = LResult36

You may want to check if the object / item olkMsg is Outlook MailItem. It may be invitation, task etc. which has no body property.

if TypeName(olkMsg) = "MailItem" then
b4 = olkMsg.Body
...

Edit (Answer to comment): InStr() is Case Sensitive. "Company name: " is not "Company Name: " Try to put all the strings to UCases to get rid of the case sensitivness:

If TypeName(olkMsg) = "MailItem" Then
    b4 = olkMsg.Body

    Dim indexOfNameb As Integer
        indexOfNameb = InStr(UCase(b4), UCase("Company name: "))

    Dim indexOfNamec As Integer
        indexOfNamec = InStr(UCase(b4), UCase("Company number: "))

    Dim finalStringb As String

        finalStringb = Mid(b4, indexOfNameb, indexOfNamec - indexOfNameb)
        Dim LResult36 As String
        LResult36 = Replace(finalStringb, "Company Name: ", "")

       excWks4.Cells(intRow4, 1) = LResult36
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM