BB Ivan,
here are a couple of posts to help with the spreadsheet being in use.
I know no way of using SQL to detect whether the file is open. There
are various ways of doing so using VBA and I've found the following to
be very fast:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q209189
The following sample Visual Basic for Applications macro calls the
FileLocked function and passes the full path and name of the file for
testing. If the function returns True, error number 70 "Permission Denied"
has most likely occurred, and the file is currently open and locked by
another process. If the function returns False, the file is not open, and
the macro opens the document.
Sub YourMacro()
Dim strFileName As String
' Full path and name of file.
strFileName = "C:\test.doc"
' Call function to test file lock.
If Not FileLocked(strFileName) Then
' If the function returns False, open the document.
Documents.Open strFileName
End If
End Sub
Function FileLocked(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
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
' Display the error number and description.
MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
FileLocked = True
Err.Clear
End If
End Function
In comparison, automating Excel and opening the workbook in its native
app is very slow.
Jamie.
One way is to attempt to rename the file and trap the resulting error.
Air code follows
Dim strFileSpec As String
strFileSpec = "C:\MyWorkbook.xls"
On Error Resume Next
Name strFileSpec As strFileSpec & ".$$$"
Select Case Err.Number
Case 75
MsgBox "Cannot access " & strFileSpec _
& ". Probably another program has opened it. " _
& "Close it and try again" , vbOkOnly + vbInformation
Exit Sub
Case > 0
MsgBox "Some other problem"
Exit Sub
End Select
Err.Clear
On Error Goto 0
Name strFileSpec & ".$$$" As strFilespec
'continue with the import
John Nurick
Jeanette Cunningham -- Melbourne Victoria Australia