Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default macro to check file open

I have to write macro code for the following sequences. What will be
the code to do this?

find if file abc.xls is open, if it is open then close it
else
endif
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default macro to check file open

On Error Resume Next
Workbooks("abc.xls").Close


__________________________________________________ ____________________

"ezil" wrote in message
...
I have to write macro code for the following sequences. What will be
the code to do this?

find if file abc.xls is open, if it is open then close it
else
endif



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default macro to check file open

On 18 Jul, 13:26, ezil wrote:
I have to write macro code for the following sequences. What will be
the code to do this?

find if file abc.xls is open, if it is open then close it
else
endif




Hi ezil,

Insert a new module into your VBA project and paste the following
function into it:

Public Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.

Open filename For Input Lock Read As #filenum ' Attempt to open
file and lock it.

Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.

Select Case errnum ' Check to see which error occurred.

Case 0 ' No error occurred. File NOT already open by another
user.
IsFileOpen = False

Case 70 ' "Permission Denied." True if file is open, or has
been renamed, moved or deleted
IsFileOpen = True

Case Else ' Another error occurred.
Error errnum

End Select
End Function


Insert another module into your project and paste the following code:

Option Explicit
Public xlPath As String, FName As String, filenum As Long


Double click on ThisWorkbook and in Workbooks_Open paste the
following code where xlPath should equal the path to the workbook you
want to close and FName should equal the name of the file you want to
close:

xlPath = "C:\Numerical Registers\"
FName = "abc.xls"

Application.ScreenUpdating = False
On Error Resume Next
If Dir(xlPath & FName) < "" Then 'If abc.xls exists then
If IsFileOpen(xlPath & FName) = True Then 'Check to see
if abc.xls is open
Workbooks(xlPath & FName).Close 'Close abc.xls
End If
Else
Dim Msg, Style, Title, Response
Msg = "The file you are trying to close does not
exist" 'If abc.xls does not exist, diplay message
Style = vbOKOnly
Title = "Is File Open?"
Response = MsgBox(Msg, Style, Title)
Cancel = True
End If
Application.ScreenUpdating = True


Now when you open the file in which you pasted the above code, it will
check to see if ABC is open. If it is, it will close it.

Best regards,

Andy

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
How to check for Open File! Ayo Excel Discussion (Misc queries) 2 August 11th 08 06:55 PM
check if file is already open mohavv Excel Discussion (Misc queries) 1 October 15th 07 12:08 AM
Check to see if a file is already open Jason Zischke Excel Programming 2 June 30th 06 08:56 AM
File Open Check dthmtlgod Excel Programming 1 January 13th 06 02:51 PM
check if file already is open Mats Nilsson Excel Programming 2 September 12th 05 09:30 AM


All times are GMT +1. The time now is 06:30 AM.

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

About Us

"It's about Microsoft Excel"