ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check if file is open, if yes close woorkbook (https://www.excelbanter.com/excel-programming/424831-check-if-file-open-if-yes-close-woorkbook.html)

Mouimet

Check if file is open, if yes close woorkbook
 
Hi,
From my Mainworkbook the macro select some data, create a new file from this
then save as a new filename, close it, keeping the Mainworkbook open. Macro
continue and do the same process again using new data and save it as another
filename. He do this 5 times.
Problem:
If one of the file is already open by someone else the macro show an error
message and stop. How to check if the file is open? If it is open, just close
this workbook without saving it and continue with the macro to save the next
file.

If it is, I need to close this file to be able to get back on my main file
Here one part of the macro:

================

'First, the data is extract from the mainfile and copy to a new workbook
' now it's time to save this new file

Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=True, _
Filename:="G:\2009\Infos\ListeTSRs.xls"
Application.DisplayAlerts = True
=========================
' macro continue, using the main file, select the new data, create a file,
then
again save as a new name.
=================

Bob Bridges[_2_]

Check if file is open, if yes close woorkbook
 
Off hand I don't know of a way to check ahead of time to see
whether a file is already open by some other user. But it
seems to me you could still do what you want by trapping that
particular error and working around it. Find out what the error
code is, then trap errors and check the error; if it's this
particular one, just move on to the next file, and if it isn't, turn
error checking back off and return to the same statement so
your program can bomb out and tell you why.

--- "Mouimet" wrote:
From my Mainworkbook the macro select some data, create a
new file from this then save as a new filename, close it,
keeping the Mainworkbook open. Macro continue and do the
same process again using new data and save it as another
filename. He do this 5 times.

Problem: If one of the file is already open by someone else
the macro show an error message and stop. How to check
if the file is open? If it is open, just close this workbook
without saving it and continue with the macro to save the
next file.



All times are GMT +1. The time now is 12:42 PM.

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