I obtained some Access code from a co-worker that is used to open query data in a pre-created, pre-formatted, Excel spreadsheet that resides on my local hdd.
Here is the code:
Public Function OpenXls(ByVal strQueryName As String, strFilename As String)
On Error GoTo OpenXls_err
Dim strFullFileName As String
Dim strTempDir As String
Dim appexcel As Object
'Set temp dir
strTempDir = Environ("WINDIR") & "\Temp\"
strFullFileName = strTempDir & strFilename
'Create spreadsheet
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strQueryName, strFullFileName, True
Set appexcel = CreateObject("Excel.Application")
'Open Workbook
appexcel.workbooks.Open strFullFileName
appexcel.Visible = True
Exit Function
OpenXls_err:
MsgBox "Error (" & CStr(Err.Number) & ") " & Err.Description, _
vbExclamation, "Error!"
The code works great except for in one circumstance. When I add a query to the Excel sheet (from Access or MS Query) and attempt to run this code I get a ~ "workbook open method of object failed error".
I want to be able to open the Excel sheet via this code, refresh the query already contained in the Excel sheet, and be done.
Obviously adding the query to the sheet is causing my error. Is there a different "Open method" that could be used?
Bookmarks