none
VB Script - Close found excel instance RRS feed

  • General discussion

  • Hi Folks -

    I have a Sub and Function that detect to see who has an excel file open.

    I want to add another function in there (or whatever is best practice) to then close that found excel instance without saving.

    Can someone help me? I can't seem to get anything to work.

    Thanks!

    Call CheckFile("W:\temp\", "TEST.xlsx")
    
    Sub CheckFile(PATH, NAME)
    
    	If objFSO.FileExists(PATH & "~$" & NAME) Then
    		WScript.Echo NAME & " is locked by " & GetFileOwner(PATH & "~$" & NAME)
    		outFile="userout.temp"
    		Set objFile = objFSO.CreateTextFile(outFile,True)
    		objFile.Write NAME & " is locked by " & GetFileOwner(PATH & "~$" & NAME) & vbCrLf
    		objFile.Close
    		WScript.Quit 99
    	End If
    
    End Sub
    
    Function GetFileOwner(strFileName)
        Set objWMIService = GetObject("winmgmts:")
        Set objFileSecuritySettings = _
        objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'")
        intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
    
        If intRetVal = 0 Then
           GetFileOwner = objSD.Owner.Name
        Else
           GetFileOwner = "Unknown"
        End If
    End Function

    • Changed type Bill_Stewart Wednesday, May 9, 2018 2:39 PM
    • Moved by Bill_Stewart Wednesday, May 9, 2018 2:39 PM This is not "scripts on demand"
    Monday, March 19, 2018 11:54 PM

All replies