dmgaddy
10-12-2016, 08:26 AM
So the question is, what am I missing in my code (below)? I need to copy some text out of the body of an email in Outlook 2010.
The email is from a temperature monitor and I want to write the temperature and humidity reported to a text file.
I can Debug.Print it to the immediate window but I cannot write it to the text file.
The body of the email is:
Temperature reached 73.76 Fahrenheit at 10/12/2016 9:49:54 AM Humidity reached 37 %RH at 10/12/2016 9:49:54 AM
The only changes in the email are the values following, "Temperature reached," "Humidity reached," and the "Date / Time pair" following each.
Using this code:
Sub GetValuesUsingRegEx()
Dim olMail As Outlook.MailItem
Dim Reg1 As RegExp
Dim Reg2 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim s As String
Dim n As Integer
Set olMail = Application.ActiveExplorer().Selection(1)
' Debug.Print olMail.Body
Set Reg1 = New RegExp
Set Reg2 = New RegExp
' \s* = invisible spaces
' \d* = match digits
' \w* = match alphanumeric
n = FreeFile()
Open "C:\test.txt" For Output As #n
With Reg1
.Pattern = "Temperature reached\s*(\d*)\.+(\d*)"
.Global = True
End With
If Reg1.Test(olMail.Body) Then
Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
s = "M.SubMatches(0); "".""; M.SubMatches(1); ""º"""""
Debug.Print s
Debug.Print M.SubMatches(0); "."; M.SubMatches(1); "º"
Print #n, s ' write to file
Close #n
Next
End If
With Reg2
.Pattern = "Humidity reached\s*(\d*)"
.Global = True
End With
If Reg2.Test(olMail.Body) Then
Set M1 = Reg2.Execute(olMail.Body)
For Each M In M1
Debug.Print M.SubMatches(0); "%"
Next
End If
End Sub
I get returned:
This in the Immediate window:
M.SubMatches(0); "."; M.SubMatches(1); "º""
73.76º
37%
Can anyone tell me how to get the value s which should equal 73.76 to write to the text file rather than the code, M.SubMatches(0); "."; M.SubMatches(1); "º"" which is currently being written there?
DMG
The email is from a temperature monitor and I want to write the temperature and humidity reported to a text file.
I can Debug.Print it to the immediate window but I cannot write it to the text file.
The body of the email is:
Temperature reached 73.76 Fahrenheit at 10/12/2016 9:49:54 AM Humidity reached 37 %RH at 10/12/2016 9:49:54 AM
The only changes in the email are the values following, "Temperature reached," "Humidity reached," and the "Date / Time pair" following each.
Using this code:
Sub GetValuesUsingRegEx()
Dim olMail As Outlook.MailItem
Dim Reg1 As RegExp
Dim Reg2 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim s As String
Dim n As Integer
Set olMail = Application.ActiveExplorer().Selection(1)
' Debug.Print olMail.Body
Set Reg1 = New RegExp
Set Reg2 = New RegExp
' \s* = invisible spaces
' \d* = match digits
' \w* = match alphanumeric
n = FreeFile()
Open "C:\test.txt" For Output As #n
With Reg1
.Pattern = "Temperature reached\s*(\d*)\.+(\d*)"
.Global = True
End With
If Reg1.Test(olMail.Body) Then
Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
s = "M.SubMatches(0); "".""; M.SubMatches(1); ""º"""""
Debug.Print s
Debug.Print M.SubMatches(0); "."; M.SubMatches(1); "º"
Print #n, s ' write to file
Close #n
Next
End If
With Reg2
.Pattern = "Humidity reached\s*(\d*)"
.Global = True
End With
If Reg2.Test(olMail.Body) Then
Set M1 = Reg2.Execute(olMail.Body)
For Each M In M1
Debug.Print M.SubMatches(0); "%"
Next
End If
End Sub
I get returned:
This in the Immediate window:
M.SubMatches(0); "."; M.SubMatches(1); "º""
73.76º
37%
Can anyone tell me how to get the value s which should equal 73.76 to write to the text file rather than the code, M.SubMatches(0); "."; M.SubMatches(1); "º"" which is currently being written there?
DMG