wsturdev
asked on
How to Release link to Excel file
I am using the attached code to link to an Excel Spreadsheet from Access 2007.
The user selects the Excel file to look at and review its contents. He can then clear the display and select another. He keeps selecting and reviewing the contents of Excel files until he finds the one he wants, and then clicks a button to proceed.
The problem occurs when, after reviewing an Excel file, he clears the display, but then goes back and selects the same file again. The "File_Is_Locked" function cannot access the file a second time because "it is open by another process". Somehow, even though I have deleted the TableDef, the Excel file is still marked as opened.
Even if I just try to open the Excel file from the desktop, I cannot because "it is open by another process".
How can I mark the Excel file as closed after I am done with it?
This is the progression:
1. First, I use "File_Is_Locked" to make sure the file is not already open by Excel or some other process.
2. "Delete_Temp_Whatever" deletes the TableDef in case some other process happened to not delete it when it was done using it.
3. "Set tbl = db.CreateTableDef..." successfully establishes the link to the Excel file.
4. "Delete_Temp_Whatever" deletes the TableDef because we are done using it.
The user selects the Excel file to look at and review its contents. He can then clear the display and select another. He keeps selecting and reviewing the contents of Excel files until he finds the one he wants, and then clicks a button to proceed.
The problem occurs when, after reviewing an Excel file, he clears the display, but then goes back and selects the same file again. The "File_Is_Locked" function cannot access the file a second time because "it is open by another process". Somehow, even though I have deleted the TableDef, the Excel file is still marked as opened.
Even if I just try to open the Excel file from the desktop, I cannot because "it is open by another process".
How can I mark the Excel file as closed after I am done with it?
This is the progression:
1. First, I use "File_Is_Locked" to make sure the file is not already open by Excel or some other process.
2. "Delete_Temp_Whatever" deletes the TableDef in case some other process happened to not delete it when it was done using it.
3. "Set tbl = db.CreateTableDef..." successfully establishes the link to the Excel file.
4. "Delete_Temp_Whatever" deletes the TableDef because we are done using it.
Private Sub MyHandler()
g_strExcelTabName = "Rules$A1:C1"
g_strLinkedTableName = "Temp_Whatever"
Set db = CurrentDb()
Delete_Temp_Whatever 'In case some other process happened to not delete it
Debug.Print g_strFullPathExcelFileKT
Set tbl = db.CreateTableDef(g_strLinkedTableName, _
dbAttachSavePWD, g_strExcelTabName, "Excel 12.0 Xml;HDR=NO;IMEX=1;ACCDB=YES;Database=" & g_strFullPathExcelFileKT & _
";Pwd=MyPswd")
db.TableDefs.Append tbl
...
...
...
Delete_Temp_Whatever 'Get rid of the temporary table
End Sub
Public Sub Delete_Temp_Whatever()
On Error GoTo Delete_Temp_Whatever_Error_Recovery
DoCmd.DeleteObject acTable, "Temp_Whatever"
Delete_Temp_Whatever_Error_Recovery:
Exit Sub
End Sub
Function File_Is_Locked(strFileName As String) As Boolean
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
Debug.Print Err.Number
Debug.Print Err.Description
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
File_Is_Locked = True
Err.Clear
Else
File_Is_Locked = False
End If
End Function
ASKER
Yes, but that causes the Nav Pane to show up (there is apparently some kind of fault in the TransferSpreadsheet function. Microsoft gave me the code I am using.
and it gave birth to another problem ;-)
can you upload a copy of the excel file.. i'll experiment on this when i get home.
ASKER
The attached file is supposed to be an XLSM, so please change the extension from .xlsx to .xlsm.
Sample.xlsx
Sample.xlsx
ASKER
capricorn1 -- any luck?
ASKER
Calling capricorn1... ;-) I know you must be busy, but did you have any luck testing?
wsturdev,
using windows 7 A2007, i can't reproduce the error.
the file .xlsm is not locked by the process.
using windows 7 A2007, i can't reproduce the error.
the file .xlsm is not locked by the process.
ASKER
I have been able to use one of our allotted support incidents with MS and am awaiting a resolution.
I will let you know the outcome.
I will let you know the outcome.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, _
"myExcelLink", "C:myExcel.xls", True, "Sheet1$"