Skip to content

Instantly share code, notes, and snippets.

@xymox12
Created September 26, 2013 13:04
Show Gist options
  • Save xymox12/6713869 to your computer and use it in GitHub Desktop.
Save xymox12/6713869 to your computer and use it in GitHub Desktop.
Script I found online to move a html table in an email body into a excel spreadsheet. Not my own, Original Author: John_w http://www.ozgrid.com/forum/showthread.php?t=176467
Option Explicit
Public Sub Import_Survey_Emails()
Dim outApp As Outlook.Application
Dim outNs As Outlook.Namespace
Dim outFolder As Outlook.MAPIFolder
Dim outMail As Outlook.MailItem
Dim lastRow As Long
Dim destCell As Range
Dim n As Integer
'For Sheet1, determine the last populated cell in column E (PHP_DATE) and set destination cell to cell A on next row
With Sheets("Sheet1")
lastRow = .Cells(Rows.Count, "E").End(xlUp).row
Set destCell = .Cells(lastRow + 1, "A")
End With
Set outApp = New Outlook.Application
Set outNs = outApp.GetNamespace("MAPI")
'Loop through emails and extract details into Excel cells
n = 0
Set outFolder = outNs.GetDefaultFolder(olFolderInbox).Folders("CASINO SURVEY")
If Not outFolder Is Nothing Then
For Each outMail In outFolder.Items
If outMail.Class = Outlook.OlObjectClass.olMail Then
If outMail.Subject = "TORONTO CASINO COMMUNITY SURVEY" Then
Extract_Email_Body outMail.HTMLBody, destCell
n = n + 1
End If
End If
Next
MsgBox n & " email(s) processed"
End If
End Sub
Private Sub Extract_Email_Body(HTML As String, DestinationCell As Range)
Dim HTMLdoc As HTMLDocument
Dim rowColumnsMap As Variant, columnLetter As Variant
Dim rowIndex As Integer
Dim table As HTMLTable
'Excel column Email table row index Email table row content
'A or B 0 1) Are you in favour of a Casino in Toronto?
'C or D 1 2) IF a new casino were to be located in Toronto, would you prefer the revenue to go into:
'K 2 Comments
'G 3 Name
'H 4 Address
'I 5 Postal Code
'J 6 Email Address
'F 7 REMOTE_ADDR
'E 8 PHP_DATE
'Mapping of email table rows (starting at row index 2) to Excel destination column letters
'Interpretation: row index 2 maps to column K, row index 3 maps to column G, etc.
rowColumnsMap = Array("K", "G", "H", "I", "J", "F", "E")
'Put the HTML string in a HTMLDocument for parsing
Set HTMLdoc = New HTMLDocument
HTMLdoc.Body.innerHTML = HTML
Set table = HTMLdoc.getElementsByTagName("TABLE")(0)
'Table row index 0 - put 1 in column A or B depending on the answer
If UCase(Trim(table.Rows(0).Cells(1).innerText)) = "YES" Then
Debug.Print "A", 0, UCase(Trim(table.Rows(0).Cells(1).innerText))
DestinationCell.Offset(0, 0).Value = 1 'Column A - YES
ElseIf UCase(Trim(table.Rows(0).Cells(1).innerText)) = "NO" Then
Debug.Print "B", 0, UCase(Trim(table.Rows(0).Cells(1).innerText))
DestinationCell.Offset(0, 1).Value = 1 'Column B - NO
End If
'Table row index 1 - put 1 in column C or D depending on the answer
If InStr(UCase(table.Rows(1).Cells(1).innerText), UCase("Public Transit Capital Account")) > 0 Then
Debug.Print "D", 1, UCase(table.Rows(1).Cells(1).innerText)
DestinationCell.Offset(0, 3).Value = 1 'Column D - PCTA
Else
Debug.Print "C", 1, UCase(table.Rows(1).Cells(1).innerText)
DestinationCell.Offset(0, 2).Value = 1 'Column C - GEN REV
End If
'Loop through mapping columns from table row index 2
With DestinationCell.Parent
rowIndex = 2
For Each columnLetter In rowColumnsMap
Debug.Print columnLetter, rowIndex, ">" & table.Rows(rowIndex).Cells(1).innerText & "<"
.Cells(DestinationCell.row, columnLetter).Value = table.Rows(rowIndex).Cells(1).innerText
rowIndex = rowIndex + 1
Next
End With
'Update destination cell to next row
Set DestinationCell = DestinationCell.Offset(1, 0)
End Sub
@xymox12
Copy link
Author

xymox12 commented Sep 26, 2013

XmlDocument xmlDoc = new XmlDocument(); might be the route to change to XML rather than HTML in body (http://forums.asp.net/t/1098374.aspx)

@xymox12
Copy link
Author

xymox12 commented Sep 26, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment