Re: Determine if a file is open by XP
XP
Wed Apr 04 09:56:01 CDT 2007
Sorry, my mistake, it is working perfectly; I some how accidentally removed
"On Error Resume Next"
Works great, thanks!
"XP" wrote:
> Thanks JHP, it works when the file is not in use; but if open (in testing, by
> me) I get "permission denied" error at the following line:
>
> Set objFile = objFSO.OpenTextFile(strFile, forAppending, createFile)
>
> "JHP" wrote:
>
> > Try this:
> > (watch for word wrap)
> >
> > Option Explicit
> > On Error Resume Next
> >
> > Dim strFile, objFSO, objFile, strComputer, objWMIService, colProcess,
> > objProcess
> > Dim strUserName, strDomainName
> > Const forAppending = 8
> > Const createFile = False
> >
> > strFile = "C:\Macro.xls"
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> >
> > If objFSO.FileExists(strFile) Then
> > Set objFile = objFSO.OpenTextFile(strFile, forAppending, createFile)
> >
> > If Err.Number = 70 Then
> > strComputer = "."
> > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
> > Set colProcess = objWMIService.ExecQuery("SELECT * FROM Win32_Process")
> >
> > For Each objProcess In colProcess
> > If objProcess.Name = "EXCEL.EXE" Then
> > objProcess.GetOwner strUserName, strDomainName
> > objProcess.Terminate()
> > WScript.Echo strUserName & " of " & strDomainName & " had " & strFile &
> > " in use"
> > End If
> > Next
> > Set colProcess = Nothing
> > Set objWMIService = Nothing
> > Else
> > objFile.Close
> > Set objFile = Nothing
> > WScript.Echo strFile & " is not in use"
> > End If
> > End If
> >
> > "XP" <XP@discussions.microsoft.com> wrote in message
> > news:C1DCD95F-8A12-4EBF-8239-EE09CA76BA7C@microsoft.com...
> > > Using Office 2003 and Windows XP;
> > >
> > > Is there a way using a script (in a VBS script file) to determine if a
> > > file
> > > is open?
> > >
> > > If so, could someone please post example code?
> > >
> > > Thanks much in advance.
> >
> >
> >