Jump to content
Sign in to follow this  
electrico

If file is already busy???

Recommended Posts

electrico

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

Share this post


Link to post
Share on other sites
99ojo

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 by 99ojo

Share this post


Link to post
Share on other sites
whim

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

Share this post


Link to post
Share on other sites
electrico

Seen replays, thanks guys. Will try it ASAP.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.