zelarra821
Registered User.
- Local time
- Today, 05:37
- Joined
- Jan 14, 2019
- Messages
- 840
Hello, people.
I have this code to export a form to Excel:
However, I am not able to leave the book open. Right now the code closes the workbook and then Excel.
I have tried to obtain the path of the file to then open the path, but I could not obtain said path either.
Thanks.
I have this code to export a form to Excel:
Code:
Private Sub CmdExportarExcel_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim strSql As String
Dim rsDatos As DAO.Recordset
'Abrimos Excel
Set xlApp = New Excel.Application
'Creamos un libro
Set xlBook = xlApp.Workbooks.Add
'Creamos una hoja
Set xlSheet = xlBook.Worksheets("Hoja1")
'Si hemos filtrado, obtenemos los datos del recordset
If FilterOn = True Then
Set rsDatos = CurrentDb.OpenRecordset("SELECT Campaña, Subvencion, Precio, Observaciones FROM (" & _
Replace(Me.RecordSource, ";", "") & ") AS Consulta WHERE " & Me.Filter)
'En caso contrario, la tabla entera
Else
Set rsDatos = CurrentDb.OpenRecordset("SELECT Campaña, Subvencion, Precio, Observaciones FROM (" & _
Replace(Me.RecordSource, ";", "") & ") AS Consulta")
End If
'Exportamos los datos a Excel
xlSheet.Range("A1").Value = "Campaña"
xlSheet.Range("B1").Value = "Subvencion"
xlSheet.Range("C1").Value = "Precio"
xlSheet.Range("D1").Value = "Observaciones"
xlSheet.Range("A2").CopyFromRecordset rsDatos
'Cerramos el recordset
rsDatos.Close
'Cerramos el libro y la aplicación
xlBook.Close savechanges:=True
xlApp.Quit
'Reseteamos los objetos
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
However, I am not able to leave the book open. Right now the code closes the workbook and then Excel.
I have tried to obtain the path of the file to then open the path, but I could not obtain said path either.
Thanks.