Background: Trying to automate an existing process using Access 2010.
I'm trying to do something really simple, output some queries into .xlsx format. However I can't get it to work properly (code below). I have two separate problems.
1) I need to export in .xlsx format. However when I use acSpreadsheetTypeExcel12 Access will create the Excel file, but it is corrupted. When you try and open it in Excel you get a message saying 'Excel cannot open file <FILENAME> because the file format of file extension is not valid'. If I use acSpreadsheetTypeExcel11 or acSpreadsheetTypeExcel10 I get a Access runtime error 3170 'Could not find installable ISAM'.
Currently I am using acSpreadsheetTypeExcel9 and then resaving the excel file manually as .xlsx. Is there any way to export as .xlsx from Access?
2) I need to output some excels worksheets that have spaces in the worksheet name (this is a user defined property not mine). However the transferspreadsheet command doesn't seem to like this, and instead will replace spaces in the sheet name with '_'. Is there a way of getting Access to allow spaces in Excel sheet names?
Can anyone help with this?
Code:
Function QueryExports()
Dim str_dir As String
Dim str_filepath_WF_IP As String
Dim str_filepath_WF_AE As String
Dim str_reportmonth As String
Dim str_date As String
'Get report month
str_reportmonth = InputBox("Please enter the current report month in CAPS", "Get reporting month")
If str_reportmonth = "" Then
MsgBox "No value specified, code stopped"
Exit Function
End If
'Get current filepath
str_dir = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
'Get current date
str_date = Format(Date, "yyyy.mm.dd")
str_filepath_WF_IP = str_dir & "FFT_NHSE_IP_" & str_reportmonth & "_" & str_date & ".xlsx"
str_filepath_WF_AE = str_dir & "FFT_NHSE_AE_" & str_reportmonth & "_" & str_date & ".xlsx"
'Create IP Webfile output
Call ExportToXLSX("821_NHSE_IP_Ward", str_filepath_WF_IP, "IP Ward")
Call ExportToXLSX("822_NHSE_IP_Site", str_filepath_WF_IP, "IP Site")
Call ExportToXLSX("823_NHSE_IP_Trust", str_filepath_WF_IP, "IP Trust")
'Create AE Webfile output
Call ExportToXLSX("824_NHSE_AE_Site", str_filepath_WF_AE, "AE Site")
Call ExportToXLSX("825_NHSE_AE_Org", str_filepath_WF_AE, "AE Trust")
End function
Private Sub ExportToXLSX(str_query As String, str_filepath As String, str_sheetname As String)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, str_query, str_filepath, True, str_sheetname
End Sub