Hi all,
I've created a database that has a number of reports. These are exported out to Excel spreadsheets on the press of a button using the OutputTo command (Note: The report is exported NOT the query behind the report - I need the report layout). After export I then do various formatting by linking into Excel using macro code and a reference to the Microsoft Excel Object Library. This formatting comprises of realigning columns and changing titles into bold font etc. Many of the reports have date fields and this is where the problem occurs. I want to be able to sort in date order on these fields immediately after opening the files. However when they are first exported, the date fields do not begin as date format, instead they remain as text until you click inside each of the cells with the mouse. This appears to be a general problem with Access - try exporting an Excel with a date field through a report yourself!
There is a process known as "Convert Text to Columns" which can be selected from the Data menu. In Excel, if I select one of the date columns and then run Text To Columns on it, all the values immediately become proper dates instead of text values. SoI thought if this works in Excel why not record a macro, grab the code and use it in Access to convert the dates automatically after the file is created...so I've done this and set up the code. In the following example as a test Ive set it up to convert the 7th column (column G) which is a date field...
Dim oxlApp As Excel.Application
Dim oxlWorkbook As Workbook
Dim oxlWorksheet As Worksheet
'Start a new excel application in the background
Set oxlApp = New Excel.Application
'Open the workbook passed as the parameter in workbook object
Set oxlWorkbook = oxlApp.Workbooks.Open(strFileName)
'Open Sheet1 of the workbook in a worksheet object
Set oxlWorksheet = oxlWorkbook.Sheets("Sheet1"
'Select date column
oxlWorksheet.Columns("G:G".Select
'Run Text To Columns on it to fix dates
oxlApp.Selection.TextToColumns Destination:=Range("G1", DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, space:=False, Other:=False, FieldInfo:=Array(1, 4)
'Save the workbook
oxlWorkbook.Save
'Close the workbook and quit the Excel application
oxlWorkbook.Close False
oxlApp.Quit
'Clear vars
Set oxlWorksheet = Nothing
Set oxlWorkbook = Nothing
Set oxlApp = Nothing
The trouble is that the Text To Columns line does not seem to work. When I run it inside Excel the column is updated and the values change into dates correctly. But when I run the macro's code in access nothing happens. Does anyone know why? Has anyone ever needed to do this themselves or know an alternatie solution to fixing the dates? Note that the solution needs to be made up of actual Access code as I do not have the option of adding in extra files, dlls, or using actual stored Excel macros, due to the fact I'm on a networked PC with restrictions. Please let me know if you have any ideas as this problem is driving me insane...
I've created a database that has a number of reports. These are exported out to Excel spreadsheets on the press of a button using the OutputTo command (Note: The report is exported NOT the query behind the report - I need the report layout). After export I then do various formatting by linking into Excel using macro code and a reference to the Microsoft Excel Object Library. This formatting comprises of realigning columns and changing titles into bold font etc. Many of the reports have date fields and this is where the problem occurs. I want to be able to sort in date order on these fields immediately after opening the files. However when they are first exported, the date fields do not begin as date format, instead they remain as text until you click inside each of the cells with the mouse. This appears to be a general problem with Access - try exporting an Excel with a date field through a report yourself!
There is a process known as "Convert Text to Columns" which can be selected from the Data menu. In Excel, if I select one of the date columns and then run Text To Columns on it, all the values immediately become proper dates instead of text values. SoI thought if this works in Excel why not record a macro, grab the code and use it in Access to convert the dates automatically after the file is created...so I've done this and set up the code. In the following example as a test Ive set it up to convert the 7th column (column G) which is a date field...
Dim oxlApp As Excel.Application
Dim oxlWorkbook As Workbook
Dim oxlWorksheet As Worksheet
'Start a new excel application in the background
Set oxlApp = New Excel.Application
'Open the workbook passed as the parameter in workbook object
Set oxlWorkbook = oxlApp.Workbooks.Open(strFileName)
'Open Sheet1 of the workbook in a worksheet object
Set oxlWorksheet = oxlWorkbook.Sheets("Sheet1"
'Select date column
oxlWorksheet.Columns("G:G".Select
'Run Text To Columns on it to fix dates
oxlApp.Selection.TextToColumns Destination:=Range("G1", DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, space:=False, Other:=False, FieldInfo:=Array(1, 4)
'Save the workbook
oxlWorkbook.Save
'Close the workbook and quit the Excel application
oxlWorkbook.Close False
oxlApp.Quit
'Clear vars
Set oxlWorksheet = Nothing
Set oxlWorkbook = Nothing
Set oxlApp = Nothing
The trouble is that the Text To Columns line does not seem to work. When I run it inside Excel the column is updated and the values change into dates correctly. But when I run the macro's code in access nothing happens. Does anyone know why? Has anyone ever needed to do this themselves or know an alternatie solution to fixing the dates? Note that the solution needs to be made up of actual Access code as I do not have the option of adding in extra files, dlls, or using actual stored Excel macros, due to the fact I'm on a networked PC with restrictions. Please let me know if you have any ideas as this problem is driving me insane...