Check if text file is open

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

Guest

Hi all,

i'm building a doc management datatbase. I need to able to check if a user
still has a file open before they check it in. I can do this for most files
but for txt files its proving very difficult because notepad doesn't do any
record locking.
So the code needs to search for the doc's file name in all open windows
title bars.

The closest i've got was this code i've found:

Private Declare Function FindWindow _
Lib "user32" _
Alias "FindWindowA" _
( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) _
As Long

Then doing something like this:
Sub CheckNotepad()
Dim hwnd As Long
hwnd = FindWindow("Notepad", vbNullString)
If hwnd <> 0 Then
MsgBox "Notepad is running"
Else
MsgBox "Notepad is not running"
End If
End Sub

But this only checks if notepad is open. I have tried putting a null string
in the first variable of FindWindow and the filename of my file in the second
variable but i cant get it to work.

Can anyone tell me what i need to change, or any other solutions

TIA

Rico
 
Hi Rico,

Maybe something like this will do what you need:

Function FileOpenInNotepad(FileName As String) As Boolean
Dim hwnd As Long
hwnd = FindWindow("Notepad", FileName & " - Notepad")
If hwnd <> 0 Then
FileOpenInNotepad = True
Else
FileOpenInNotepad = False
End If
End Function

This seems to work in Windows XP UK English, but I have no idea whether
it can be relied on to work in other versions or languages.
 
Thanks very much John works fine, thought i had tried that but didn't get it
quite right
 
Back
Top