Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default Testing if a file is open

HI:

I would like to test if a specific file is open, and if it is I want to close it. Can
someone tell me the expression to use to deterine is a file (say collector.xls) is open?

Thank you

John Baker
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Testing if a file is open

Hi John
use the following formula to check for an open workbook:
Function IsWorkbookOpen(Name As String)
Dim WB As Workbook
On Error Resume Next
Set WB = Workbooks(Name)
On Error GoTo 0
IsWorkbookOpen = Not (WB Is Nothing)
End Function

so you can then use the following
Sub Check_WB()
If IsWorkbookOpen("Book1.xls") Then
Workbooks("Book1.xls").Close
Else
Workbooks.Open Filename:="D:\temp\Book1.xls"
End If
End Sub

HTH
Frank

John Baker wrote:
HI:

I would like to test if a specific file is open, and if it is I want
to close it. Can someone tell me the expression to use to deterine is
a file (say collector.xls) is open?

Thank you

John Baker



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default Testing if a file is open

John,

Check the last function on this page at John Walkenbach's site:

http://j-walk.com/ss/excel/tips/tip54.htm

hth,

Doug

"John Baker" wrote in message
...
HI:

I would like to test if a specific file is open, and if it is I want to

close it. Can
someone tell me the expression to use to deterine is a file (say

collector.xls) is open?

Thank you

John Baker



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Testing if a file is open

John,

Since you want to close the workbook if it is open, you can just
ignore the error if the workbook is not open. For example,

On Error Resume Next
Workbooks("Collector.xls").Close savechanges:=True 'or False
On Error Goto 0


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"John Baker" wrote in message
...
HI:

I would like to test if a specific file is open, and if it is I

want to close it. Can
someone tell me the expression to use to deterine is a file

(say collector.xls) is open?

Thank you

John Baker



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to open a 82 KB XLSM file due to "Too many different cellformats" & "Converter failed to open the file." errors. Phillip Pi Setting up and Configuration of Excel 0 April 23rd 09 08:53 PM
In Excel - Use Windows Explorer instead of File Open to open file KymY Excel Discussion (Misc queries) 1 August 5th 06 09:59 PM
testing. do not open. S S Excel Worksheet Functions 1 April 16th 06 01:16 AM
Testing for a open worksheet from Outlook Phillips[_3_] Excel Programming 1 December 30th 03 07:45 AM
Testing for open workbook nigelab Excel Programming 4 November 2nd 03 04:31 PM


All times are GMT +1. The time now is 05:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"