Hirdetés

Új hozzászólás Aktív témák

  • eszgé100

    őstag

    Sziasztok!

    Az alabbi funkcioval megvizsgalom, hogy egy adott fajl meg van-e mar nyitva a ciklus egy korabbi lepesebeol, ha nincs, akkor a kovetkezo lepes a ciklusban megnyitja a hatterben elokeszitve a kod tovabbi lepeseihez.

    Remekul mukodik, de sajnos halozaton megosztottak a munkafuzetek igy elofordul, hogy mas is eppen hasznalja valamelyiket. Ebben az esetben ugyanugy 70-es hibakodot kapok, ami szerint meg van nyitva, de nem tudok innentol kulonbseget tenni, hogy az a sajat gepemen van-e megnyitva, vagy valaki masen. Olyan feltetelt szeretnek megvizsgalni, hogyha mas felhasznalonal van megnyitva, akkor nalam automatikusan nyiljon meg Read-Only modban, ugyanugy hatterben.

    Function IsFileOpen(sPath As String)

    Dim fileNum As Integer
    Dim errNum As Integer

    'Allow all errors to happen
    On Error Resume Next
    fileNum = FreeFile()

    'Try to open and close the file for input.
    'Errors mean the file is already open
    Open sPath For Input Lock Read As #fileNum
    Close fileNum

    'Get the error number
    errNum = Err

    'Do not allow errors to happen
    On Error GoTo 0

    'Check the Error Number
    Select Case errNum

    'errNum = 0 means no errors, therefore file closed
    Case 0
    IsFileOpen = False

    'errNum = 70 means the file is already open
    Case 70
    IsFileOpen = True

    'Something else went wrong
    Case Else
    IsFileOpen = errNum

    End Select

    End Function

    A kodreszlet, ami a megnyitast vegzi:

    Application.ScreenUpdating = True
    ma.Visible = True

    fileName = Right(sPath, Len(sPath) - InStrRev(sPath, "\"))
    Application.StatusBar = "Processing File: " & fileName
    Application.ScreenUpdating = False

    If IsFileOpen(sPath) = False Then Workbooks.Open sPath
    Windows(fileName).Visible = False

Új hozzászólás Aktív témák