PC Review


Reply
Thread Tools Rate Thread

Check if Excel file is already open

 
 
Robert Crandal
Guest
Posts: n/a
 
      27th Dec 2009
I need to check if an Excel file is already open.
How do i do this?

thank u


 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      27th Dec 2009
Hi

Look at this:

Sub Test()
If Not IsWbOpen("Book1.xls") then
Workbooks.Open("Book1.xls")
End If
End Sub

Function IsWbOpen(WbName As String) As Boolean
For Each wb In Application.Workbooks
If wb.Name = WbName Then
IsWbOpen = True
Exit For
End If
Next
End Function

Regards,
Per

"Robert Crandal" <(E-Mail Removed)> skrev i meddelelsen
news:SpEZm.119911$(E-Mail Removed)...
>I need to check if an Excel file is already open.
> How do i do this?
>
> thank u
>
>


 
Reply With Quote
 
Robert Crandal
Guest
Posts: n/a
 
      27th Dec 2009
Hi Per Jessen

Does your function need to have the path along with
the filename itself?? I'm just curious if it would better
to use "C:\Mydata\Book1.xls" as opposed to just
"Book1.xls" as the parameter to your function.

thank u


"Per Jessen" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi
>
> Look at this:
>
> Sub Test()
> If Not IsWbOpen("Book1.xls") then
> Workbooks.Open("Book1.xls")
> End If
> End Sub
>
> Function IsWbOpen(WbName As String) As Boolean
> For Each wb In Application.Workbooks
> If wb.Name = WbName Then
> IsWbOpen = True
> Exit For
> End If
> Next
> End Function
>


 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      27th Dec 2009
Try this

Sub nn()
MsgBox IsBKOpen("MyBook.xls")
End Sub


Function IsBKOpen(ByRef BName As String) As Boolean
On Error Resume Next
IsBKOpen = Not (Application.Workbooks(BName) Is Nothing)
End Function

Mike

"Robert Crandal" wrote:

> I need to check if an Excel file is already open.
> How do i do this?
>
> thank u
>
>
> .
>

 
Reply With Quote
 
Per Jessen
Guest
Posts: n/a
 
      27th Dec 2009
Hi Robert

No path needed in my function.

Regards,
Per

"Robert Crandal" <(E-Mail Removed)> skrev i meddelelsen
news:%sFZm.12952$(E-Mail Removed)...
> Hi Per Jessen
>
> Does your function need to have the path along with
> the filename itself?? I'm just curious if it would better
> to use "C:\Mydata\Book1.xls" as opposed to just
> "Book1.xls" as the parameter to your function.
>
> thank u
>
>
> "Per Jessen" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Hi
>>
>> Look at this:
>>
>> Sub Test()
>> If Not IsWbOpen("Book1.xls") then
>> Workbooks.Open("Book1.xls")
>> End If
>> End Sub
>>
>> Function IsWbOpen(WbName As String) As Boolean
>> For Each wb In Application.Workbooks
>> If wb.Name = WbName Then
>> IsWbOpen = True
>> Exit For
>> End If
>> Next
>> End Function
>>

>


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      27th Dec 2009
Just to add to Per's response.

His function doesn't need and can't use the path.

This kind of thing would fail to do what you want:
workbooks("C:\somepath\somename.xls")

The workbooks collection only cares about the name of the workbook.

You could enhance Per's function to look to see if there is any book1.xls open.
And if there is, look to see if it's the one from the folder you want.

Option Explicit
Sub testme()
MsgBox IsWorkBookOpen(myPath:="C:\my documents\excel", _
WkbkName:="Book1.xls")
End Sub
Function IsWorkBookOpen(myPath As String, WkbkName As String) As String

Dim TestWkbk As Workbook
Dim myMsg As String

Set TestWkbk = Nothing
On Error Resume Next
Set TestWkbk = Workbooks(WkbkName)
On Error GoTo 0

If TestWkbk Is Nothing Then
myMsg = "Not Open"
Else
'remove the trailing slash if there
If Right(myPath, 1) = "\" Then
myPath = Left(myPath, Len(myPath) - 1)
End If
If LCase(myPath) = LCase(TestWkbk.Path) Then
myMsg = "It's Open!"
Else
myMsg = "A file with the same name is open!"
End If
End If

IsWorkBookOpen = myMsg

End Function

You could just pass it the fullname (path and name) and do some parsing in the
function if you wanted.



Robert Crandal wrote:
>
> Hi Per Jessen
>
> Does your function need to have the path along with
> the filename itself?? I'm just curious if it would better
> to use "C:\Mydata\Book1.xls" as opposed to just
> "Book1.xls" as the parameter to your function.
>
> thank u
>
> "Per Jessen" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Hi
> >
> > Look at this:
> >
> > Sub Test()
> > If Not IsWbOpen("Book1.xls") then
> > Workbooks.Open("Book1.xls")
> > End If
> > End Sub
> >
> > Function IsWbOpen(WbName As String) As Boolean
> > For Each wb In Application.Workbooks
> > If wb.Name = WbName Then
> > IsWbOpen = True
> > Exit For
> > End If
> > Next
> > End Function
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How check if file to open is Excel or if Excel file is corrupt? laavista Microsoft Excel Programming 3 19th Nov 2009 08:19 PM
Check if an Excel file is open scott Microsoft Access External Data 2 14th Jan 2008 10:22 PM
how to check if more than one excel file is open? Graff Microsoft Excel Programming 3 30th Nov 2007 08:37 PM
check who has an excel file open =?Utf-8?B?c2x5bWVhdA==?= Microsoft Access External Data 0 5th Jul 2006 02:04 PM
How to check Excel file already Open Rudy S Microsoft Excel Programming 2 25th Jan 2005 02:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:28 AM.