Results 1 to 2 of 2

Thread: [RESOLVED] Check for Open File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    330

    Resolved [RESOLVED] Check for Open File

    I have an Access 2000 database writing to a word document. The issue I am running into is that the database doesn't always open the file but it was coded to always close the file. I need to check first to see if the file is open before I close it. I have located a couple different pieces of code but none seem to do the trick, my guess is that it is vba and the code pieces have found are VB or .net

    Code:
    Public Function IsFileOpen(FileName As String) As Boolean
        Dim iFilenum As Long
        Dim iErr As Long
         
        On Error GoTo Errorhandler
        
        
        iFilenum = FreeFile()
        
        Open FileName For Input Lock Read As #iFilenum
        Close iFilenum
        
        iErr = Err
        
    Errorhandler:
         
        Select Case iErr
        Case 0:
                  IsFileOpen = False
        Case 70:
                  IsFileOpen = True
                  'add on: close the file
        Case Else: Error iErr
        End Select
    End Function
    This is one I found and it seems that when I use it there is no error traping. That is the same as the next code sample.

    Code:
    Public Function IsFileLocked(strFileName As String) As Boolean
        On Error Resume Next
        Dim FF As Integer
        FF = FreeFile
        'An error occurs if the document is currently open.
        Open strFileName For Binary Access Read Lock Read As #FF
        Close #FF
        'Check for Error
        If Err.Number Then
            Err.Clear
            IsFileLocked = True
        End If
    End Function
    The other option I thought of was just checking to see if I could activate the window with that name, but again the error doesn't trap

    Code:
    Public Function IsOpen(FileName As String) As Boolean
    
    On Error Resume Next
        Word.Windows(FileName).Activate
        
        If Err Then
            IsOpen = False
        Else
            IsOpen = True
        End If
    End Function
    So I have decided that I really do not know what I am doing and need a bit of help.

    Thanks,
    Swoozie
    Somedays you just should not get out of bed.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Check for Open File

    Ok Try this


    Code:
    Sub ChkFileStatus()
    
        ' Test to see if the file is open.
        If IsFileOpen("c:\Test.xls") Then
            ' Display a message stating the file in use.
            MsgBox "File already in use!"
        Else
            ' Display a message stating the file is not in use.
            MsgBox "File not in use!"
        End If
    
    End Sub
    
    ' This function checks to see if a file is open or not.
    Function IsFileOpen(filename As String)
        Dim filenum As Integer, errnum As Integer
    
        On Error Resume Next
        filenum = FreeFile()
        
        ' Attempt to open the file and lock it.
        Open filename For Input Lock Read As #filenum
        Close filenum          ' Close the file.
        errnum = Err           ' Save the error number that occurred.
        On Error GoTo 0        ' Turn error checking back on.
    
        ' Check to see which error occurred.
        Select Case errnum
    
            ' No error occurred.
            ' File is NOT already open by another user.
            Case 0
             IsFileOpen = False
    
            ' Error number for "Permission Denied."
            ' File is already opened by another user.
            Case 70
                IsFileOpen = True
    
            ' Another error occurred.
            Case Else
                Error errnum
        End Select
    
    End Function
    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width