PDA

View Full Version : Solved: Check if file is Open ?



gunny2k9
06-13-2011, 06:36 AM
Ok i know ther is code easily found on google but the examples work on 1 file .. i need to open many and thats done with


Dim xFile As Integer
Dim Filename As Variant

With Application
' Set File Name Array to selected Files (allow multiple)
Filename = .GetOpenFilename(Filter, FilterIndex, Title, , True)
End With

' Exit on Cancel
If Not IsArray(Filename) Then
MsgBox "No file was selected."
Exit Sub
End If

' Open Files
For xFile = LBound(Filename) To UBound(Filename)
msg = msg & Filename(xFile) & vbCrLf ' This can be removed
Workbooks.Open Filename(xFile)
xName = Right(Filename(xFile), 14)
Call Interigate
Next xFile



any one got any ideas how i can easly check if file is open or not ? ..

PS the file/files are on a Network Drive hence the need to check if there open, as it may be opened be some one else where

gunny2k9
06-14-2011, 05:51 AM
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



Dim strfilename As String
For xFile = LBound(Filename) To UBound(Filename)
strfilename = Filename(xFile)
If Not FileLocked(strfilename) Then
flag = "OK"
Else
flag = "locked"
End If
If flag = "locked" Then
GoTo DoNothing
Exit Sub
End If
flag = ""
Next xFile


nvm got there in the end :)