Problem : Run time error ‘2302’: Microsoft Office Access can’t save the output data to the file you’ve selected: How to fix ?
If one user of my application clicks on a button to create an Excel file report, my application works fine.
However, if that user has the Excel file open and then another usesr clicks on a button to create yet another Excel file,
I get the error message as follows:
Run time error ‘2302’: Microsoft Office Access can’t save the output data to the file you’ve selected
Is there a way that I can modify the following code to see if the Excel file is already open and if so, create another file and open the new file.
Perhaps I could increment the file suffix name to create unique file names everytime the file is created and opened.
Set com = New ADODB.Command
.CommandType = adCmdStoredProc
.CommandText = “dbo.procSpForeign”
.Parameters.Append .CreateParameter(“RptYear”, adInteger, adParamInput, 4, intYearSP)
.ActiveConnection = cn
Set rstQueryFS = .Execute
ExportedFile = “C:\UDL\FRGNCTRY.XLS”
DoCmd.OutputTo acOutputTable, “tblSpFCY”, acFormatXLS, ExportedFile
If isFileExist(ExportedFile) Then StartDocXLS ExportedFile
Private Function StartDocXLS(FileName)
‘Set xlWB = GetObject(FileName)
‘xlWB.Application.Visible = True
‘Set xlWB = Nothing
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
‘open excel template
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open(FileName)
Set xlWS = xlWB.Worksheets(1)
xlApp.ScreenUpdating = True
Private Function isFileExist(filePath As String) As Boolean
isFileExist = (filePath <> “” And Dir$(filePath) <> “”)
Solution: Run time error ‘2302’: Microsoft Office Access can’t save the output data to the file you’ve selected: How to fix ?
You can try to open the file with a lock – if it fails, the file is open by another user. The example code I experimented with (the syntax for this command is horrible):
Open ActiveWorkbook.FullName For Input Access Read Lock Read As #1
This bums out with a “permission denied” error – my activeworkbook is of course open. If you try it on a non-open file, it works fine. You can test for this (using On Error, sadly) and then create the special incremented filenames.
Hope this helps,