Buonasera a tutti.
Ho creato il codice sottostante per esportare una Query di Acces in Excel e sembra funzionare bene solo alla prima esecuzione, poichè lanciata una seconda volta mi da gli errori come da immagini allegate.
Ho notato che il file di Excel aperto la prima volta non si chiude e sono costretto a chiuderlo con Ctrl+Alt+Canc - Termina Operazione.
Mi aiutate a capire per favore come posso risolvere il problema?

Queste schermate di errore le ottengo quando lancio la 2^ volta la funzione e poi chiudo il file di Excel ancora aperto nel modo sottoriportato.


Questa è la funzione che uso:
Option Compare Database
Option Explicit
Const fPath = "C:\prova"
Const fname = "Dati Esportati.xls"
Const fName2 = "Accessi Esportati.xls"
Function OpenExcel()
On Error GoTo ErrorHandler
Dim appExcel As Excel.Application
Dim bks As Excel.Workbooks
Dim sht As Excel.worksheet
Dim strSheet As String
Dim MyFile As String
MyFile = fPath & fName2 'Point to the Data file
DoCmd.OutputTo acQuery, "QueryTuttiAccessi", "MicrosoftExcel(*.xls)", _
MyFile, True, ""
'Open the Code file and run the processing macro
MyFile = fPath & fname ' point to the Code file
Set appExcel = GetObject(, "Excel.Application")
appExcel.Workbooks.Open (MyFile)
Set sht = appExcel.ActiveWorkbook.Sheets(1)
sht.Activate
'appExcel.Application.Visible = True
With appExcel
.Run "OpenAndProcess" 'Change to suit name of Excel routine
End With
ErrorHandlerExit:
Exit Function
ErrorHandler:
If Err = 429 Then
'Excel is not running; open Excel with CreateObject
Set appExcel = CreateObject("Excel.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
End Function
Spero di essere stato chiaro.
Ringrazio anticipatamente chi mi aiuta in questo.
Ciao Nicola.