Extract data from outlook body with VBA (1 Viewer)

Mona4980

New member
Local time
Today, 12:26
Joined
Oct 27, 2022
Messages
2
Hello,
I am new to this forum. Not to VBA. I have a routine that checks for unread messages from a particular sender of email. That part works fine. What I am now trying to do is to read the body with VBA and return the results so that I can enter the results in a table. Here is what I am using. After getting the email I call GetBodyData.
The problem is that the ParseTextLinePair function does not return the results I am looking for. The location is correct and all. I am thinking that the problem lies in this line intLocCRLF = InStr(intLocLabel, strSource, vbCrLf) but am not sure. the compare is where I think the issue is. The data I am looking for is right after Mode\Equipment: It is not on the next line. How can I get the data that comes right after the Mode\Equipment:



Sub GetBodyData()
Dim objOL As Outlook.Application
Dim objItem As Object
Dim objFwd As Outlook.MailItem
Dim strResults As String
On Error Resume Next
Set objOL = Application
Set objItem = objOL.ActiveExplorer.Selection(1)
If Not objItem Is Nothing Then
strResults = ParseTextLinePair(objItem.Body, "Mode\Equipment:")
If strResults <> "" Then
' do some stuff here. I will enter this into a table
Else
MsgBox "Could not extract data from message."
End If
End If
Set objOL = Nothing
Set objItem = Nothing
Set objFwd = Nothing
End Sub

Function ParseTextLinePair _
(strSource As String, strLabel As String)
Dim intLocLabel As Integer
Dim intLocCRLF As Integer
Dim intLenLabel As Integer
Dim strText As String
intLocLabel = InStr(strSource, strLabel)
intLenLabel = Len(strLabel)
If intLocLabel > 0 Then
intLocCRLF = InStr(intLocLabel, strSource, vbCrLf)
If intLocCRLF > 0 Then
intLocLabel = intLocLabel + intLenLabel
strText = Mid(strSource, intLocLabel, intLocCRLF - intLocLabel)
Else
intLocLabel = _
Mid(strSource, intLocLabel + intLenLabel)
End If
End If
ParseTextLinePair = Trim(strText)
End Function
 

Users who are viewing this thread

Top Bottom