Hi there,
I'd be really grateful for any help with sorting out the date format in my Outlook to Excel importer.
Background
I've got an online form, and when the user submits it their response is sent to my email address.
I receive the form responses in the following format, with a semicolon separating each form field:
message;location;page_url;date time
example:
This is a good article;UK; 14:47:00
Every day I transfer all the form responses from Outlook into Excel by clicking a button in Excel which runs the following VB code. This puts each form field (separated by a semicolon) in its own cell.
VB code
Sub Button1_Click()
Set olApp = GetObject("", "Outlook.Application")
Set MyFolder = olApp.ActiveExplorer.CurrentFolder
Set MyItems = MyFolder.Items
i = 0
For Each SpecificItem In MyItems
i = i + 1
'Range("A" & i).Select
'ActiveCell.FormulaR1C1 = SpecificItem.Body
s = s & Left(SpecificItem.Body, Len(SpecificItem.Body) - 1) & vbCrLf
Next
Open "c:\temp\survey.txt" For Output As #1
Print #1, s
Close #1
ChDir "C:\TEMP"
Workbooks.OpenText FileName:="C:\TEMP\survey.txt", Origin:=xlWindows, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array( _
16, 1), Array(17, 1))
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
MsgBox (i & " files have been read and imported into Excel.")
End Sub
The problem
The problem is that sometimes the date format screws up. For example in the example given in the background section above, the date appears in Outlook as:
12/03/2010 14:47:00
But comes through to Excel as:
03/12/2010 14:47:00
with the day and month swapped around. This doesn't happen to all dates - some are unaffected, for example 24/03/2010 14:03:20 appears correctly in both Outlook and Excel.
Possible cause
I think all dates with a day of 12 or less are interpreted as dates in an American format and are passed through to Excel with the day and month swapped around, while all dates with a day of 13 or more come through as text and the day and month are unchanged.
How can I get all dates to be interpreted as either a date or text and not a mixture of both?
Hope that makes sense, but please let me know if it doesn't!
Many thanks,
Katie
P.S. I'm using XP and Excel 2003
I'd be really grateful for any help with sorting out the date format in my Outlook to Excel importer.
Background
I've got an online form, and when the user submits it their response is sent to my email address.
I receive the form responses in the following format, with a semicolon separating each form field:
message;location;page_url;date time
example:
This is a good article;UK; 14:47:00
Every day I transfer all the form responses from Outlook into Excel by clicking a button in Excel which runs the following VB code. This puts each form field (separated by a semicolon) in its own cell.
VB code
Sub Button1_Click()
Set olApp = GetObject("", "Outlook.Application")
Set MyFolder = olApp.ActiveExplorer.CurrentFolder
Set MyItems = MyFolder.Items
i = 0
For Each SpecificItem In MyItems
i = i + 1
'Range("A" & i).Select
'ActiveCell.FormulaR1C1 = SpecificItem.Body
s = s & Left(SpecificItem.Body, Len(SpecificItem.Body) - 1) & vbCrLf
Next
Open "c:\temp\survey.txt" For Output As #1
Print #1, s
Close #1
ChDir "C:\TEMP"
Workbooks.OpenText FileName:="C:\TEMP\survey.txt", Origin:=xlWindows, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array( _
16, 1), Array(17, 1))
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
MsgBox (i & " files have been read and imported into Excel.")
End Sub
The problem
The problem is that sometimes the date format screws up. For example in the example given in the background section above, the date appears in Outlook as:
12/03/2010 14:47:00
But comes through to Excel as:
03/12/2010 14:47:00
with the day and month swapped around. This doesn't happen to all dates - some are unaffected, for example 24/03/2010 14:03:20 appears correctly in both Outlook and Excel.
Possible cause
I think all dates with a day of 12 or less are interpreted as dates in an American format and are passed through to Excel with the day and month swapped around, while all dates with a day of 13 or more come through as text and the day and month are unchanged.
How can I get all dates to be interpreted as either a date or text and not a mixture of both?
Hope that makes sense, but please let me know if it doesn't!
Many thanks,
Katie
P.S. I'm using XP and Excel 2003