Hello.
I need to export a query to MS Excel.
In order to test if my query opens, I use below piece of code and works perfectly.
Code:
DoCmd.OpenQuery "qry_123", , acReadOnly
However, trying to execute the following code and Error occurred in line:
Code:
Set wb = xl.Workbooks.Open(sExcelWB)
Here is my full code:
Code:
Option Compare DatabaseOption Explicit
Sub cmbexport_toexcel_Click()
Dim xl, wb, ws, ch, mychart, qry_123, Target As Object ''ch is excel chart
Dim sExcelWB As String
On Error Resume Next
Set xl = CreateObject("excel.aplication"):confused:
Err.Clear
On Error GoTo 0
sExcelWB = CurrentProject.Path & "qry_123"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qry_123", sExcelWB, True
Set wb = xl.Workbooks.Open(sExcelWB) '' here error 424 ocurred
Set ws = wb.Sheets("qry_123")
Set ch = ws.Shapes.AddChart58
Set mychart = ws.ChartObjects("Chart 1")
ws.Columns.Autofit
ws.Columns("B:C").HorizontalAlignment = xlCenter
ws.Columns(4).TextToColumns , , , , -1, 0, 0, 0
ws.Columns(5).TextToColumns , , , , -1, 0, 0, 0
wb.Save
xl.Visible = True
xl.UserControl = True
Set ws = Nothing
Set wb = Nothing
End Sub
I am worried due I cannot test my code after:
Code:
Set wb = xl.Workbooks.Open(sExcelWB)
I spent hours googling to find a solution with no success.
I hope someone helps me.
Cheers