Cannot extract data from outlook message body using excel macro

scottluz

New Member
Joined
Dec 10, 2009
Messages
3
I am using this macro to extract data from an outlook message body. It will not grab any of the data that I need from the body, but will set up the Headers in excel. I can see data in the temp file but it is not transferring.

****** http-equiv="Content-Type" content="text/html; charset=utf-8">****** name="ProgId" content="Word.Document">****** name="Generator" content="Microsoft Word 11">****** name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Csluzinas%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]--> Option Explicit
<o:p> </o:p>
Sub ReadInbox()
Dim OutlookApp As Object
Dim OA_NameSpace As Object
Dim OA_Folder As Object
Dim OA_MailItem As Object
<o:p> </o:p>
Dim ws As Worksheet
<o:p> </o:p>
Dim Created As Boolean
<o:p> </o:p>
Dim NextRecord As Long
<o:p> </o:p>
Application.ScreenUpdating = False
<o:p> </o:p>
Set ws = Workbooks.Add(xlWorksheet).Sheets(1)
<o:p> </o:p>
ws.[A1] = "Name"
ws.[B1] = "E-Mail"
'ws.[C1] = "subject"
'ws.[D1] = "REMOTE_HOST"
<o:p> </o:p>
NextRecord = 2
<o:p> </o:p>
On Error Resume Next
Set OutlookApp = GetObject("Outlook.Application")
If OutlookApp Is Nothing Then
Set OutlookApp = CreateObject("Outlook.Application")
Created = True
If OutlookApp Is Nothing Then
MsgBox "Unable to start Outlook."
Exit Sub
End If
End If
On Error GoTo 0
<o:p> </o:p>
Set OA_NameSpace = OutlookApp.GetNamespace("MAPI")
'Set OA_Folder = OA_NameSpace.GetDefaultFolder(6)
'Set OA_Folder = OA_NameSpace."Customer Service"
Set OA_Folder = OA_NameSpace.PickFolder
'Set OA_Folder = OA_NameSpace.Folders("Scott Luzinas").Folders("Customer Service")
<o:p> </o:p>
OA_Folder.Items.Sort "Received", True
<o:p> </o:p>
For Each OA_MailItem In OA_Folder.Items
Dim OrderInfo As Variant
<o:p> </o:p>
OrderInfo = GrabInfo(OA_MailItem.Body)
<o:p> </o:p>
If IsArray(OrderInfo) Then
ws.Range(Cells(NextRecord, 1), Cells(NextRecord, 2)) = OrderInfo
NextRecord = NextRecord + 1
End If
Next
<o:p> </o:p>
Application.ScreenUpdating = True
<o:p> </o:p>
Set OA_Folder = Nothing
Set OA_NameSpace = Nothing
Set OutlookApp = Nothing
End Sub
<o:p> </o:p>
Private Function GrabInfo(message As String) As Variant
Dim tmpInfo(4) As String
<o:p> </o:p>
Dim f As Integer
<o:p> </o:p>
Const TMPFILE As String = "C:\temp\outlook_extraction.tmp"
<o:p> </o:p>
If message = vbNullString Then GrabInfo = vbNullString: Exit Function
<o:p> </o:p>
f = FreeFile
<o:p> </o:p>
Open TMPFILE For Output As #f
Write #f, message
Close #f
<o:p> </o:p>
f = FreeFile
<o:p> </o:p>
Open TMPFILE For Input As #f
<o:p> </o:p>
Do While Not EOF(f)
Dim tmpLine As String
<o:p> </o:p>
Line Input #f, tmpLine
<o:p> </o:p>
If InStr(1, tmpLine, "=") Then
Select Case UCase(Left(tmpLine, InStr(1, tmpLine, "=") - 1))
Case "Name": tmpInfo(0) = SplitString(tmpLine, "=")
Case "E-Mail": tmpInfo(1) = SplitString(tmpLine, "=")
'Case "subject": tmpInfo(1) = SplitString(tmpLine, "=")
'Case "REMOTE_HOST": tmpInfo(1) = SplitString(tmpLine, "=")
<o:p> </o:p>
End Select
End If
Loop
<o:p> </o:p>
Close #f
<o:p> </o:p>
'Kill TMPFILE
<o:p> </o:p>
GrabInfo = tmpInfo
End Function
<o:p> </o:p>
Private Function SplitString(value As String, delimeter As String) As String
SplitString = Trim(Mid(value, InStr(1, value, delimeter) + 2, Len(value) - InStr(1, value, delimeter)))
End Function
<o:p> </o:p>
End Function
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,213,534
Messages
6,114,184
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top