No error when executing Open statement on text file already opened in Notepad

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
I use this code to test for files already opened by another user or process:

Code:
Function IsFileOpen(strFile As String) As Boolean
Dim i As Integer
i = FreeFile
If Dir(strFile) = "" Then IsFileOpen = False: Exit Function
Err = 0
On Error Resume Next
Open strFile For Binary Access Read Lock Read Write As #i
If Err Then IsFileOpen = True
Close #i
End Function

Works fine. However, if I have a text file open within Notepad (haven't tested with other programs) on my computer (haven't tested if it behaves the same way when another user has the file open in Notepad) and execute the function, the function returns False (ie suggesting file isn't open). This line executes successfully:

Code:
Open strFile For Binary Access Read Lock Read Write As #i

I tested with this code with and without the target file being opened:

Code:
Sub test()
MsgBox IsFileOpen("C:\Test\Test.txt")
End Sub

Firstly, can someone else confirm whether this is the case for them, and secondly explain why this is the case - I would have thought the Open statement should produce an error (and thus the function to return True). Or does Notepad specifically not Lock the file it has opened? Or some other reason?

Thanks for any and all replies!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Greetings Richard,

Apologies beforehand, as I did not test and must hit the rack, lest tomorrow be an uphill battle all the long day...


I do believe that you are correct in that the file (txt) is not locked.


Anyways, just in case you don't get a better answer (which you most likely will), maybe try similar to http://www.mrexcel.com/forum/showthread.php?t=373613 , excepting just set a flag if you want (rather than close).

I could well be off the mark, but thought "maybe".

Hope this helps and a great day to you and yours,

Mark
 
Upvote 0
Notepad doesn't lock files. You can open one up, then go into explorer and delete it, and it is quite happy ...
 
Upvote 0
Okay - still here (couldn't resist trying to get a formula right...)

Would be a guess, but maybe so stuff can be modified by "opneing" the file w/o starting an app (like notepad). A total stab in the dark, but maybe related to older WIN and .ini files?

If you get a chance, let me know if using Tasks was sensible, whether setting a flag or setting is as a reference.

I'm out (for real), again, have a great day,

Mark
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,123
Members
452,546
Latest member
Rafafa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top