electrico Posted January 20, 2010 Posted January 20, 2010 Hi Everyone!!! I have network drive L: I have a file named PO98432.xls I have a gui that opens this file for work. How I can do the function or smth..that will deny access to file if someone already opened it and working with it?? (file status is: busy) or something like that. Much thanks for the help. elect
99ojo Posted January 20, 2010 Posted January 20, 2010 (edited) Hi, try this: $file = FileOpen ("L:\PO98432.xls", 1) ; open excel file in write mode ; if open fails, msgbox If $file = - 1 Then MsgBox (0,"", "Not ready to open in write mode") ; if opened then close filehandle and msgbox Else FileClose ($file) ; very important to close the file, don't forget in your code MsgBox (0,"", "Ready to open in write mode") ;add code to open excelfile and work with it EndIf ;-)) Stefan Edited January 20, 2010 by 99ojo
whim Posted January 20, 2010 Posted January 20, 2010 I recently learned Siao's _FileInUse() is more reliable though (sorry, lost the link to relevant thread) Func _FileInUse($sFilename) ; author: Siao Local $aRet, $hFile $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _ "str", $sFilename, _ ;lpFileName "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ "dword", 0, _ ;dwShareMode = DO NOT SHARE "dword", 0, _ ;lpSecurityAttributes = NULL "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL "hwnd", 0) ;hTemplateFile = NULL $hFile = $aRet[0] If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1 $aRet = DllCall("Kernel32.dll", "int", "GetLastError") SetError($aRet[0]) Return 1 Else ;close file handle DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Return 0 EndIf EndFunc ;==>_FileInUse hope that helps, wim
electrico Posted January 20, 2010 Author Posted January 20, 2010 Seen replays, thanks guys. Will try it ASAP.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now