View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Hank Scorpio Hank Scorpio is offline
external usenet poster
 
Posts: 103
Default Testing for open workbook

On Sat, 1 Nov 2003 03:43:08 -0500, nigelab
wrote:

I need to create vba code within a excel macro to detemine if a
workbook is already open. If it is, I want to continue with the macro.
If it isnt, I want to open the workbook and then continue with the
macro.


This is a little rough, but it'll give you an idea of how to do it:

Sub TestForOpenFile()

Dim s_WbkName As String
Dim wbk As Workbook

'Note that the workbook name may or may not have
'an extension, depending on your Windows Explorer
'settings. I'd therefore test both.

'If the workbook's not open, an error will
'occur (which we suppress), and wbk will
'remain nothing.

On Error Resume Next
Set wbk = Application.Workbooks("TestWorkbook")
If wbk Is Nothing Then
Set wbk = Application.Workbooks("TestWorkbook.xls")
End If

'Change this to your file
s_WbkName = "C:\Documents and Settings\" _
& "Hank Scorpio\My Documents\Testworkbook.xls"

On Error GoTo 0

If wbk Is Nothing Then

'Make sure that the file is there
'before you try to open it.
If Dir(s_WbkName, _
vbNormal) = "" Then

MsgBox "The file does not exist."

Exit Sub

Else

Workbooks.Open s_WbkName

End If

End If

MsgBox "This is the rest of the macro."

End Sub



---------------------------------------------------------
Hank Scorpio
scorpionet who hates spam is at iprimus.com.au (You know what to do.)
* Please keep all replies in this Newsgroup. Thanks! *