Hello, I need to export data to Excel from Access and then open the Excel sheet. I have been able to find a code that works well enough, but I have 2 problems remaining that I can't solve.
1. If I try to open the excel sheet and it is already opened, I can't and Access freeze. I was able to find a way around the problem by having a function that determines if Excel is already running; if it is, a message box appears and ask the user to manually close the sheet and then I use exit sub. I would prefer if Access could check if the file test.xls is open, and if it is, automaticly close it. Is it possible?
2. I don't want the users to be able to save the excel file, but I need to export data to it. If I make the file read-only I can't export my data. Any way I can easily solve the problem? I'm not familiar at all with vba coding in Excel so please I would need detailed informations if I must code in Excel
Here's the code I'm using:
Private Sub CmdExport_Click()
On Error GoTo ViewExcelError
Dim xlApp As Excel.Application
Dim xlBook As Excel.workBook
Dim xlSheet As Excel.workSheet
Dim DB As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQl As String
If fIsAppRunning("Excel") Then
MsgBox "Excel is already opened. Please close it and restart the command"
Exit Sub
End If
Set DB = CurrentDb
Set qdf = DB.QueryDefs("ReqAuto")
strSQl = "SELECT NoAuto, Mun, DateTrans, Year(DateTrans) As AnTrans, Month(DateTrans) As MoisTrans, " & _
"UVois, CUtil, AnConst, AnApp, PrixD, PrixJ, ÉvalMun, Fbat_J, SupTer " & _
"FROM ReqTotale;"
' Create an Excel workbook file based on the
' object specified in the second argument.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"ReqAuto", "C:\Access\test.xls", True
' Create a Microsoft Excel object.
Set xlApp = CreateObject("Excel.Application")
' Open the spreadsheet to which you exported the data.
Set xlBook = xlApp.Workbooks.Open("C:\Access\test.xls")
Set xlSheet = xlBook.Worksheets("ReqAuto")
xlApp.visible = True
'print sheet
'xlSheet.PrintOut
xlBook.Saved = True
Set xlSheet = Nothing
Set xlBook = Nothing
'xlApp.Quit
Set xlApp = Nothing
ExitViewExcel:
Exit Sub
ViewExcelError:
MsgBox Err.Description
Resume ExitViewExcel
End Sub
1. If I try to open the excel sheet and it is already opened, I can't and Access freeze. I was able to find a way around the problem by having a function that determines if Excel is already running; if it is, a message box appears and ask the user to manually close the sheet and then I use exit sub. I would prefer if Access could check if the file test.xls is open, and if it is, automaticly close it. Is it possible?
2. I don't want the users to be able to save the excel file, but I need to export data to it. If I make the file read-only I can't export my data. Any way I can easily solve the problem? I'm not familiar at all with vba coding in Excel so please I would need detailed informations if I must code in Excel

Here's the code I'm using:
Private Sub CmdExport_Click()
On Error GoTo ViewExcelError
Dim xlApp As Excel.Application
Dim xlBook As Excel.workBook
Dim xlSheet As Excel.workSheet
Dim DB As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQl As String
If fIsAppRunning("Excel") Then
MsgBox "Excel is already opened. Please close it and restart the command"
Exit Sub
End If
Set DB = CurrentDb
Set qdf = DB.QueryDefs("ReqAuto")
strSQl = "SELECT NoAuto, Mun, DateTrans, Year(DateTrans) As AnTrans, Month(DateTrans) As MoisTrans, " & _
"UVois, CUtil, AnConst, AnApp, PrixD, PrixJ, ÉvalMun, Fbat_J, SupTer " & _
"FROM ReqTotale;"
' Create an Excel workbook file based on the
' object specified in the second argument.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"ReqAuto", "C:\Access\test.xls", True
' Create a Microsoft Excel object.
Set xlApp = CreateObject("Excel.Application")
' Open the spreadsheet to which you exported the data.
Set xlBook = xlApp.Workbooks.Open("C:\Access\test.xls")
Set xlSheet = xlBook.Worksheets("ReqAuto")
xlApp.visible = True
'print sheet
'xlSheet.PrintOut
xlBook.Saved = True
Set xlSheet = Nothing
Set xlBook = Nothing
'xlApp.Quit
Set xlApp = Nothing
ExitViewExcel:
Exit Sub
ViewExcelError:
MsgBox Err.Description
Resume ExitViewExcel
End Sub