Reputation:
I have form on my website which gets emailed when the customer completes it, then looks like this:-
You got mail from Mr Kelley McIntyre. Here is the form data: First Name : Mr XXXXX Last Name : XXXXXX Company Name : Army Email Address : [email protected] Telephone/Mobile No : 0123456789 Date of Event : 14/12/2013 Number of Guests : 80 Budget : 6500-7000 Type of Event : Other Catering Required : Yes Drinks and Entertainment Requirements : christmas meal, welcome drink, wine at table British Army Warrant Officers & Sergeants plus wives and partners How Did You Hear About Us? : Google
As you can see its fairly simple form, however I need to export this data into Excel every time I get one of these emails, so I can keep a record of all the enquiries we get.
Can someone help? I know how to do a Macro, but if its VBA, then I'm lost, so its needs to be in idiot format if possible!
Upvotes: 0
Views: 4238
Reputation: 6433
You can start with writing a macro to process an mail item. And setup Outlook Rule to pickup this type of email from Subject/Account then run the macro. Change sExcelFile, sRecordSheet, iC as you see fit. I have made assumptions.
This Code below is for Outlook, please note you need a running Outlook all the time to have this automation. It should get you started half way. Note you need "Microsoft Excel x.0 Object Library" in your References.
Public Sub Rules_WebSiteFormRecord(oMail As MailItem)
Const sExcelFile As String = "C:\Test\Record.xlsx"
Const sRecordSheet As String = "Record" ' Worksheet name
Dim oExcel As Excel.Application, oWB As Excel.Workbook, oWS As Excel.worksheet
Dim arrTxt As Variant, oLine As Variant, iR As Long, iC As Long, bWrite As Boolean
Set oExcel = CreateObject("excel.application")
Set oWB = oExcel.Workbooks.Open(FileName:=sExcelFile)
Set oWS = oWB.Worksheets(sRecordSheet)
' Make Excel visible for Debug purpose:
oExcel.Visible = True
' Find next row of Last used row in Excel worksheet
iR = oWS.Cells(Rows.Count, 1).End(xlUp).Row + 1
' Process email body and store it into columns of worksheet "sRecordSheet"
'Debug.Print oMail.Body
' Store received time of email in Column A
oWS.Cells(iR, 1).Value = oMail.ReceivedTime
' Split the email body into lines then process each
arrTxt = Split(oMail.Body, vbCrLf)
For Each oLine In arrTxt
bWrite = False
' store data according to text in line
If InStr(1, oLine, "First Name", vbTextCompare) Then
iC = 2 ' Column of First Name
bWrite = True
ElseIf InStr(1, oLine, "Last Name", vbTextCompare) Then
iC = 3 ' Column of First Name
bWrite = True
' Add the rest of the fields...
End If
If bWrite Then
oWS.Cells(iR, iC).Value = Split(oLine, ":")(1)
iR = iR + 1
End If
Next
Set oWS = Nothing
' Close the workbook with saving changes
oWB.Close True
Set oWB = Nothing
Set oExcel = Nothing
' mark it as Read if no error occurred
If Err.Number = 0 Then
oMail.UnRead = False
Else
MsgBox "ERR(" & Err.Number & ":" & Err.Description & ") while processing " & oMail.Subject
Err.Clear
End If
End Sub
Upvotes: 1