Check if Excel workbook is open in Access (1 Viewer)

Andre Bruin

New member
Local time
Today, 00:13
Joined
Apr 22, 2016
Messages
8
Hi,

I'm importing data from an Excel file via VBA Access. Every 5 seconds Access checks in a folder if an Excel sheet is available.
Now I want that Access first checks if the Excel file is open already and if the file is open, it should be ignored.

thanks in advance,

rg,
André
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:13
Joined
May 7, 2009
Messages
19,247
if somewhere in your code you already instanciated an excel.application object, you can test if the workbook is open.

dim xlWB as excel.workbook
for each xlWB in xlApp.WorkBooks
if xlWB.Name = "name of excel file"
' workbook is open
end if
next
set xlwb=nothing
 
Last edited:

Andre Bruin

New member
Local time
Today, 00:13
Joined
Apr 22, 2016
Messages
8
I get an error with 'Dim xlWB As excel.workbook'

"The datatype is not defined"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:13
Joined
Feb 28, 2001
Messages
27,229
If the data type is not defined, you have a bad reference to the Excel library.
 

dbay

Registered User.
Local time
Today, 02:13
Joined
Jul 15, 2007
Messages
87
Here is what I use to check if a file is open or not.
Just pass it the Path to the file. If it is Open it will return False.

Code:
Public Function Check_Open_File(ByVal STR_Path As String) As Boolean

    Dim INT_Number As Integer

On Error GoTo err_proc
    
    INT_Number = FreeFile()
    Open STR_Path For Input Lock Read As #INT_Number
    Close INT_Number
    
    Check_Open_File = True
    
exit_proc:
On Error Resume Next
    Exit Function

err_proc:
    Check_Open_File = False
    Select Case Err.Number
        Case 53
            'MsgBox ("" & STR_Path & "" & Chr(13) & "" & Chr(13) & "File not Found!"), vbExclamation, "Missing File Error"
        Case 70
            'MsgBox ("" & STR_Path & "" & Chr(13) & "" & Chr(13) & "File is Open!"), vbExclamation, "Open File Error"
    End Select
    Resume exit_proc
    
End Function
 

Users who are viewing this thread

Top Bottom