How to check if a file is open

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Fitz1000
Newbie
Posts: 6
Joined: Wed Oct 18, 2006 11:35 pm

How to check if a file is open

Post by Fitz1000 » Mon Oct 23, 2006 11:44 pm

I am trying to write and If statement to see if a file is open (if it is, I need to end the macro, if it is not, I need to proceed to the next instruction). I have found IfFileExists but this is not what I need. I found this article http://support.microsoft.com/default.as ... US;q138621 in the MS Knowledgebase but can't seem to make it work in Macro Scheduler. Anyone have an easy way to do this?

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Oct 24, 2006 7:18 am

Here you go:

Code: Select all

VBSTART
' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.
Function IsFileOpen(ByVal sFileName)
  Const ForAppending = 8
  Set oFSO = CreateObject("Scripting.FileSystemObject")

  On Error Resume Next ' Turn error checking off.
  ' Attempt to open the file and lock it.
  Set f = oFSO.OpenTextFile(sFileName, ForAppending, False)
  iErrNum = Err.Number ' Save the error number that occurred.
  f.Close ' Close the file.
  On Error GoTo 0 ' Turn error checking back on.

  ' Check to see which error occurred.
  Select Case iErrNum
    ' No error occurred.
    ' File is NOT already open by another user.
    Case 0
      IsFileOpen = False

    ' Error number for "Permission Denied."
    ' File is already opened by another user.
    Case 70
      IsFileOpen = True

    ' Another error occurred.
    Case Else
      Err.Raise iErrNum
  End Select
End Function
VBEND

VBEval>IsFileOpen("c:\somefile.txt"),IsItOpen
If>IsItOpen=False
  ..
  .. rest of macro here
  ..
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Fitz1000
Newbie
Posts: 6
Joined: Wed Oct 18, 2006 11:35 pm

Post by Fitz1000 » Tue Oct 24, 2006 5:37 pm

I was soooo close! Thanks for responding, it works like a champ.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Oct 24, 2006 6:35 pm

A very useful bit of code, added to snippets, thanks Marcus!
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Oct 24, 2006 8:35 pm

It would be nice if it could incorporate a check that the file exists and return an error code. (I know you can do separately in the macro).

The caveat is that not all programs "open" the file they are using.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts