Can anybody help with this please
With some code I have picked up on this board (with thanks IFM & others) I have managed to cobble some code together that checks if any files are open in a directory & names the file.
The last part, where it should give the name of the user who has a file open is giving me some grief.
I keep getting an “invalid procedure, call or argument” error, with this line highlighted.
i = InStrRev(strWholeFile, strFlag1, j) + Len(strFlag1)
I have tried changing various lines of the code, but have not been able to get it to run.
This is the part of the code I am stuck on
With some code I have picked up on this board (with thanks IFM & others) I have managed to cobble some code together that checks if any files are open in a directory & names the file.
The last part, where it should give the name of the user who has a file open is giving me some grief.
I keep getting an “invalid procedure, call or argument” error, with this line highlighted.
i = InStrRev(strWholeFile, strFlag1, j) + Len(strFlag1)
I have tried changing various lines of the code, but have not been able to get it to run.
Code:
Option Explicit
Public myDir As String
Public StartLine As Long
Public HowManyLines As Long
Public myFile
Public i
Public adate
Public ws
Public ActWork
Public NewWrkBk
Dim Ans As Integer
Sub CheckIfOpen()
Set ActWork = ActiveWorkbook
Worksheets("Sheet1").Select
myDir = "G:\02. Stock Lists\Current Stock lists\"
myFile = Dir(myDir & Application.PathSeparator & "*.xls", vbDirectory)
'Clear Data in Column A & G
Range("A1:A100").ClearContents
Range("G1:G100").ClearContents
Range("A1").Select
i = 0
Do While myFile <> ""
i = i + 1
Cells(i, 1) = myFile
myFile = Dir
Loop
For i = 1 To Range("A65536").End(xlUp).Row
myFile = Cells(i, 1)
'// We can use this for ANY FILE not just Excel!
If IsFileOpen(myDir & myFile) Then
Ans = MsgBox(myFile & " is already Open" & _
vbCrLf & "By " & LastUser("myDir.xls"), vbQuestion + vbOKOnly, "File in Use")
Select Case Ans
Case vbOK
End
End Select
End If
Next
End Sub
Code:
Function IsFileOpen(strFullPathFileName As String) As Boolean
Dim hdlFile As Long
'// Error is generated if you try
'// opening a File for ReadWrite lock >> MUST BE OPEN!
On Error GoTo FileIsOpen:
hdlFile = FreeFile
Open strFullPathFileName For Random Access Read Write Lock Read Write As hdlFile
IsFileOpen = False
Close hdlFile
Exit Function
FileIsOpen:
'// Someone has it open!
IsFileOpen = True
Close hdlFile
End Function
Code:
Function LastUser(strFileName As String) As String
Dim strWholeFile As String
Dim strFlag1 As String, strFlag2 As String
Dim intNameLength As Integer
Dim i As Integer, j As Integer
strFlag1 = Chr(0) & Chr(0)
strFlag2 = Chr(32) & Chr(32)
Open strFileName For Binary As #1
strWholeFile = Space(LOF(1))
Get 1, , strWholeFile
Close #1
j = InStr(1, strWholeFile, strFlag2)
i = InStrRev(strWholeFile, strFlag1, j) + Len(strFlag1)
intNameLength = Asc(Mid(strWholeFile, i - 3, 1))
LastUser = Mid(strWholeFile, i, intNameLength)
End Function