Undetected locked file - ghost of killed file

  • Thread starter ragtopcaddy via AccessMonster.com
  • Start date
R

ragtopcaddy via AccessMonster.com

The following code fails to detect a txt file open in Notepad. After this
function is called, because it returns false, I have code that, inexplicably,
successfully kills the file. However, even after the code completes, I can
still see the "killed" file in the open Notepad application, while my code
shells out to notepad and opens the new file. This is all very bizarre. Does
anyone have an explanation for all of this?

Thanks,

Bill R

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
FileLocked = True
Err.Clear
End If
End Function

--
Bill Reed

"If you can't laugh at yourself, laugh at somebody else"

Message posted via AccessMonster.com
 
R

ragtopcaddy via AccessMonster.com

Likewise, the following code doesn't detect when Notepad is running

Public Function IsAppRunning(strApp As String) As Boolean
Dim appObj As Object
Dim strAppObj As String

On Error Resume Next

strAppObj = strApp & ".Application"

Set appObj = GetObject(, strAppObj)
IsAppRunning = (Err.Number = 0)

Set appObj = Nothing
Err.Clear

End Function
 
D

Douglas J Steele

I don't believe Notepad actually locks the file. Instead, I think it simply
opens a snapshot of the current contents. (Certain when I open log files
using Notepad, subsequent events get written to the log file while I have it
open)

Try it yourself: open a text file and make modifications to it, but don't
save it (nor close Notepad).

Open the same text file a second time: it's going to be the original
contents, not the modified contents. Make different modifications to it this
time.

Go back to the first instance and save it (closing down that instance of
Notepad). Open the text file again: it should be what you just saved.

Go back to the second instance and save it. It's not going to complain...

As to your other post, since you can't use Automation with Notepad, I'm not
surprised that GetObject doesn't work. Take a look at
http://www.mvps.org/access/api/api0007.htm and
http://www.mvps.org/access/api/api0013.htm at "The Access Web".
 
R

ragtopcaddy via AccessMonster.com

Douglas,

Thanks for the expert advice. I am using Dev's code.

Bill R
I don't believe Notepad actually locks the file. Instead, I think it simply
opens a snapshot of the current contents. (Certain when I open log files
using Notepad, subsequent events get written to the log file while I have it
open)

Try it yourself: open a text file and make modifications to it, but don't
save it (nor close Notepad).

Open the same text file a second time: it's going to be the original
contents, not the modified contents. Make different modifications to it this
time.

Go back to the first instance and save it (closing down that instance of
Notepad). Open the text file again: it should be what you just saved.

Go back to the second instance and save it. It's not going to complain...

As to your other post, since you can't use Automation with Notepad, I'm not
surprised that GetObject doesn't work. Take a look at
http://www.mvps.org/access/api/api0007.htm and
http://www.mvps.org/access/api/api0013.htm at "The Access Web".
The following code fails to detect a txt file open in Notepad. After this
function is called, because it returns false, I have code that, inexplicably,
[quoted text clipped - 20 lines]
End If
End Function
 
R

ragtopcaddy via AccessMonster.com

I spoke too soon. In another sub I am checking for Excel.

When the following code runs :

If Not fIsAppRunning("Excel", True) Then
Set xlObj = New Excel.Application
Else
Set xlObj = GetObject(, "Excel.Application")
End If

Dev's function returns true when in fact it should return false (Excel is not
running. I checked in Task Manager processes to be absolutely sure).
However, if I put a break on the line that calls the function and step
through, it correctly returns false and sets a new xlObj value

Any ideas?

Bill R

I don't believe Notepad actually locks the file. Instead, I think it simply
opens a snapshot of the current contents. (Certain when I open log files
using Notepad, subsequent events get written to the log file while I have it
open)

Try it yourself: open a text file and make modifications to it, but don't
save it (nor close Notepad).

Open the same text file a second time: it's going to be the original
contents, not the modified contents. Make different modifications to it this
time.

Go back to the first instance and save it (closing down that instance of
Notepad). Open the text file again: it should be what you just saved.

Go back to the second instance and save it. It's not going to complain...

As to your other post, since you can't use Automation with Notepad, I'm not
surprised that GetObject doesn't work. Take a look at
http://www.mvps.org/access/api/api0007.htm and
http://www.mvps.org/access/api/api0013.htm at "The Access Web".
The following code fails to detect a txt file open in Notepad. After this
function is called, because it returns false, I have code that, inexplicably,
[quoted text clipped - 20 lines]
End If
End Function

--
Bill Reed

"If you can't laugh at yourself, laugh at somebody else"

Message posted via AccessMonster.com
 
D

Douglas J Steele

What happens if you leave off the second parameter (the True)?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


ragtopcaddy via AccessMonster.com said:
I spoke too soon. In another sub I am checking for Excel.

When the following code runs :

If Not fIsAppRunning("Excel", True) Then
Set xlObj = New Excel.Application
Else
Set xlObj = GetObject(, "Excel.Application")
End If

Dev's function returns true when in fact it should return false (Excel is not
running. I checked in Task Manager processes to be absolutely sure).
However, if I put a break on the line that calls the function and step
through, it correctly returns false and sets a new xlObj value

Any ideas?

Bill R

I don't believe Notepad actually locks the file. Instead, I think it simply
opens a snapshot of the current contents. (Certain when I open log files
using Notepad, subsequent events get written to the log file while I have it
open)

Try it yourself: open a text file and make modifications to it, but don't
save it (nor close Notepad).

Open the same text file a second time: it's going to be the original
contents, not the modified contents. Make different modifications to it this
time.

Go back to the first instance and save it (closing down that instance of
Notepad). Open the text file again: it should be what you just saved.

Go back to the second instance and save it. It's not going to complain...

As to your other post, since you can't use Automation with Notepad, I'm not
surprised that GetObject doesn't work. Take a look at
http://www.mvps.org/access/api/api0007.htm and
http://www.mvps.org/access/api/api0013.htm at "The Access Web".
The following code fails to detect a txt file open in Notepad. After this
function is called, because it returns false, I have code that,
inexplicably,
[quoted text clipped - 20 lines]
End If
End Function

--
Bill Reed

"If you can't laugh at yourself, laugh at somebody else"

Message posted via AccessMonster.com
 
R

ragtopcaddy via AccessMonster.com

Same thing
What happens if you leave off the second parameter (the True)?
I spoke too soon. In another sub I am checking for Excel.
[quoted text clipped - 42 lines]

--
Bill Reed

"If you can't laugh at yourself, laugh at somebody else"

Message posted via AccessMonster.com
 
R

ragtopcaddy via AccessMonster.com

But, if I set the next statement at the 1st line and hit F5, the code runs
properly.
What happens if you leave off the second parameter (the True)?
I spoke too soon. In another sub I am checking for Excel.
[quoted text clipped - 42 lines]

--
Bill Reed

"If you can't laugh at yourself, laugh at somebody else"

Message posted via AccessMonster.com
 
R

ragtopcaddy via AccessMonster.com

I tried to work around the problem, as a test, with the following:

ReDo:
If Not fIsAppRunning("Excel") Then
Set xlObj = New Excel.Application
Else
On Error GoTo ReDo
Set xlObj = GetObject(, "Excel.Application")
End If

But the code seems to ignor the On Error line and gets stuck on trying to get
the object, which doesn't exist.
What happens if you leave off the second parameter (the True)?
I spoke too soon. In another sub I am checking for Excel.
[quoted text clipped - 42 lines]
 
R

ragtopcaddy via AccessMonster.com

If I put a break line in at:

fIsAppRunning = True

Which is getting set when the code runs even though Excel is not running,
then lngH=1180950, even though the only place where it could be set to that
(where strClassName <> "") is equal to 0:

lngH = apiFindWindow(strClassName, vbNullString)

Very curious.

Bill R
What happens if you leave off the second parameter (the True)?
I spoke too soon. In another sub I am checking for Excel.
[quoted text clipped - 42 lines]

--
Bill Reed

"If you can't laugh at yourself, laugh at somebody else"

Message posted via AccessMonster.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top