We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Determine if an excel file is open

cdmac2
cdmac2 asked
on
Medium Priority
232 Views
Last Modified: 2008-02-01
Hello,

My Access 2000 VBA code opens up an Excel Spreadsheet, edits some data, then closes and saves the spreadsheet.  How can I determine if the Excel file is already in use (So I can abort) before I open it.

Thanks!
MikeMc
Comment
Watch Question

To see if the file is open you could use the subroutine below:

Public Function IsFileOpen(filename As String)
    Dim filenum As Integer, errnum As Integer

    On Error Resume Next   ' Turn error checking off.
    filenum = FreeFile()      ' Get a free file number.
   
    ' 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

You would then just call it like this:

If IsFileOpen("C:\Myfile.xls\") then
     'Abort Code
Else
    'Run Other Code
End If

Hope this helps, and for more infor check out: http://support.microsoft.com/?kbid=213383

JRG

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Yep, that does it... thanks!

-Mike
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.