ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   check to see if a file is open (https://www.excelbanter.com/excel-programming/430743-check-see-if-file-open.html)

JT

check to see if a file is open
 

I'm using the code below to display a dialog box for the user to select a
file. I'm then checking to see if the file is opened. However, it appears
the code is running faster than the file opens and I and getting the error
message that the file was not opened when in fact it is.

Is there a way to do slow down the code until the file is opened without
coding a hard pause of X number of seconds?

Any help or suggestion would be greatly appreciated. Thanks!

FName = Application.GetOpenFilename

If FName < False Then
Workbooks.Open (FName)
End If

If Workbooks.Count < Wcount + 1 Then
MsgBox "The file was not opened."
Sheets("Macros").Activate
End If

code continues here.......

--
JT

Mike H

check to see if a file is open
 

Hi,

Your code will pause at

FName = Application.GetOpenFilename


but a general method for checking if a workbook is open or not is

Sub IsOpen()
On Error Resume Next

Set wBook = Workbooks(FName)
If wBook Is Nothing Then 'Not open
MsgBox "Workbook is not open", vbCritical
Set wBook = Nothing
On Error GoTo 0
Else 'It is open
MsgBox "Yes it is open", vbInformation
Set wBook = Nothing
On Error GoTo 0
End If
End Sub

Mike

"JT" wrote:

I'm using the code below to display a dialog box for the user to select a
file. I'm then checking to see if the file is opened. However, it appears
the code is running faster than the file opens and I and getting the error
message that the file was not opened when in fact it is.

Is there a way to do slow down the code until the file is opened without
coding a hard pause of X number of seconds?

Any help or suggestion would be greatly appreciated. Thanks!

FName = Application.GetOpenFilename

If FName < False Then
Workbooks.Open (FName)
End If

If Workbooks.Count < Wcount + 1 Then
MsgBox "The file was not opened."
Sheets("Macros").Activate
End If

code continues here.......

--
JT



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

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