ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   File Open Check (https://www.excelbanter.com/excel-programming/350349-file-open-check.html)

dthmtlgod

File Open Check
 
I have an Excel spreadsheet that exports its date to Access when all my
macros are done running. It errors out if the database/file is open.

Is there a way to check to see if the datebase/file is open before I try to
export?



Bob Phillips[_6_]

File Open Check
 
Here is an example of some code to check it


Function IsFileOpen(FileName As String)
Dim iFilenum As Long
Dim iErr As Long

On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0

Select Case iErr
Case 0: IsFileOpen = False
Case 70: IsFileOpen = True
Case Else: Error iErr
End Select

End Function

Sub test()
If Not IsFileOpen("C:\MyTest\volker2.xls") Then
Workbooks.Open "C:\MyTest\volker2.xls"
End If
End Sub


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"dthmtlgod" wrote in message
...
I have an Excel spreadsheet that exports its date to Access when all my
macros are done running. It errors out if the database/file is open.

Is there a way to check to see if the datebase/file is open before I try

to
export?






All times are GMT +1. The time now is 05:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com