Check if an specific Excel file is open before transfer spreadshee

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

Is there a way to check if an specific Excel file is open before starting a
transfer spreadsheet? Or, this might be better for me: before running the
action script behind the submit button?

Kind regards, Stefan van der Hooft.
 
Hi Stefan,

If you want to check that no other process has the file open, a classic
approach is to try to open it exclusively yourself. Air code:

Function IsFileAvailable(FileSpec as String) As Boolean
If Len(Dir(FileSpec)) > 0 Then 'make sure file exists
lngFileNum = FreeFile()
On Error Resume Next
Open FileSpec For Input Lock Read Write As #lngFileNum
If Err.Number = 0 Then 'file opened successfully
IsFileAvailable = True
Close #lngFileNum
Else 'file could not be opened, for whatever reason
IsFileAvailable = False
End If
On Error GoTo 0
End If
End Function



If you want to confirm that the file *is* open in an instance of Excel
running in the same Windows session, try to grab the instance of Excel
and iterate through its Workbooks collection. More air code:

Function IsWorkbookOpenLocally(FileSpec As String) As Boolean
Dim Result As Boolean
Dim oXL As Object
Dim j As Long

Result = False
On Error GoTo Err_IsWorkbookOpenLocally:
Set oXL = GetObject(, "Excel.Application")

'If we're here, Excel is running
For j = 1 to oXL.Workbooks.Count
If oXL.Workbooks(j).FullName = FileSpec Then
'Workbook is open
Result = True
Exit For
End If
Next j

Exit_IsWorkbookOpenLocally:
Set oXL = Nothing
IsWorkbookOpenLocally = Result
Exit Function

IsWorkbookOpenLocally:
Result = False
Resume Exit_IsWorkbookOpenLocally:

End Function

If you want to check multiple instances of Excel, things get more
complicated.
 
I tried the function “IsWorkbookOpenLocallyâ€. Issues arising:
• Before it ran I got error “Argument not optionalâ€
• If I delete the Argument “(FileSpec As String) As Boolean†(which I am
sure I shouldn’t)
I then get error “ActiveX component can’t create object†relative to the
code line “Set oXL = GetObject(, "Excel.Application")â€
• Is my code line “If oXL.Workbooks(j).EntrySheet.xls = FileSpec Thenâ€
correct?
Help Please
 
Back
Top