ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Determining if a file is open using VBA (https://www.excelbanter.com/excel-programming/373540-determining-if-file-open-using-vba.html)

Quietman

Determining if a file is open using VBA
 
I working with several files, I write macros to open them, but I don't want
the macro to run if the file is already open, what command can I use to check
for this

Working in Excel
--
Helping Is always a good thing

Ron de Bruin

Determining if a file is open using VBA
 
Try this function
Copy it in a normal module

Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function


You use it like this

If bIsBookOpen(sFileNameXls) Then......

--
Regards Ron de Bruin
http://www.rondebruin.nl



"QuietMan" wrote in message ...
I working with several files, I write macros to open them, but I don't want
the macro to run if the file is already open, what command can I use to check
for this

Working in Excel
--
Helping Is always a good thing




Quietman

Determining if a file is open using VBA
 
Found This, but thanks for all the help

Sub Chk_IsFileOpen()
If Windows("Filenam.xls").Visible = True Then
Exit Sub
Else
Workbooks.Open Filename:= _
"P:\~ ~ ".xls"
End Sub
--
Helping Is always a good thing


"Ron de Bruin" wrote:

Try this function
Copy it in a normal module

Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function


You use it like this

If bIsBookOpen(sFileNameXls) Then......

--
Regards Ron de Bruin
http://www.rondebruin.nl



"QuietMan" wrote in message ...
I working with several files, I write macros to open them, but I don't want
the macro to run if the file is already open, what command can I use to check
for this

Working in Excel
--
Helping Is always a good thing





Tom Ogilvy

Determining if a file is open using VBA
 
That works if the workbook isn't open? I would expect it to raise a
subscript out of range error.

--
Regards,
Tom Ogilvy

"QuietMan" wrote in message
...
Found This, but thanks for all the help

Sub Chk_IsFileOpen()
If Windows("Filenam.xls").Visible = True Then
Exit Sub
Else
Workbooks.Open Filename:= _
"P:\~ ~ ".xls"
End Sub
--
Helping Is always a good thing


"Ron de Bruin" wrote:

Try this function
Copy it in a normal module

Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function


You use it like this

If bIsBookOpen(sFileNameXls) Then......

--
Regards Ron de Bruin
http://www.rondebruin.nl



"QuietMan" wrote in message
...
I working with several files, I write macros to open them, but I don't
want
the macro to run if the file is already open, what command can I use to
check
for this

Working in Excel
--
Helping Is always a good thing








All times are GMT +1. The time now is 11:27 AM.

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