check to see if a file is open

J

JT

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.......
 
M

Mike H

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top