TransferSpreadsheet--validate source exists

B

BB Ivan

I am using TransferSpreadsheet in a procedure that pulls data from a list of
similar spreadsheets and am encountering errors when (1) the spreadsheet is
currently in use or (2) the specific tab I'm looking for doesn't exist. What
is the best way to have my procedure pre-check for these situations so it can
by-pass specific spreadsheets as needed. Also, is there a way of having
TransferSpreadsheet access the spreadsheet as read-only (without exclusive
access)?
Thanks
 
J

Jeanette Cunningham

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
 
J

Jeanette Cunningham

BB Ivan,
as far as I know, it's not possible to open the spreadsheet as read-only.
As a work around, your code could make a copy of the spreadsheet to use and
delete the copy when done.


Jeanette Cunningham -- Melbourne Victoria Australia
 
B

BB Ivan

Thanks! Both posts were helpful. Still wondering if there's also a good way
of testing if, after determining that the file is not locked, it has a
specific tab...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top