Peter Andersen
New Member
Hallo All,
I have a problem with an older macro that used to work. I want to check if a specific workbook is opened by another user. I have searched and found many treads, but non that solved my problem. So what am I not seeing in my code?
The problem is that I get “Run time error 52 bad file name or number” when the macro reaches the following line code:
Open FullFileName For Input Lock Read As #f
The thing is, when I run the macro without calling the Function, the workbook is opened. So the path and name must be right or?
I don’t know if the enclosed code piece is enough for any one of to see what the problem is. Then please let me know.
the variable Ark is the name of the workbook.
Sub Copy workbook ()
Do stuff
placering = Application.ActiveWorkbook.Path ( the workbook I am looking for is in the same folder as the active workbook)
y = fileIsOpen(placering & "\" & "*" & ark & "*" & ".*")
More stuff
End sub
Function fileIsOpen(FullFileName As String) As Boolean
Dim f As Integer
f = FreeFile
on error resume next
Open FullFileName For Input Lock Read As #f
Close #f
If Err.Number <> 0 Then
FileAlreadyOpen = True
Err.Clear
Else
FileAlreadyOpen = False
End If
On Error GoTo 0
End function
Regards Peter
I have a problem with an older macro that used to work. I want to check if a specific workbook is opened by another user. I have searched and found many treads, but non that solved my problem. So what am I not seeing in my code?
The problem is that I get “Run time error 52 bad file name or number” when the macro reaches the following line code:
Open FullFileName For Input Lock Read As #f
The thing is, when I run the macro without calling the Function, the workbook is opened. So the path and name must be right or?
I don’t know if the enclosed code piece is enough for any one of to see what the problem is. Then please let me know.
the variable Ark is the name of the workbook.
Sub Copy workbook ()
Do stuff
placering = Application.ActiveWorkbook.Path ( the workbook I am looking for is in the same folder as the active workbook)
y = fileIsOpen(placering & "\" & "*" & ark & "*" & ".*")
More stuff
End sub
Function fileIsOpen(FullFileName As String) As Boolean
Dim f As Integer
f = FreeFile
on error resume next
Open FullFileName For Input Lock Read As #f
Close #f
If Err.Number <> 0 Then
FileAlreadyOpen = True
Err.Clear
Else
FileAlreadyOpen = False
End If
On Error GoTo 0
End function
Regards Peter