Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default test file - is open?

Hi,
I made a macro which does some changes in the workbook and saves it with a
new name. But I forgot one possible error - the file with the new name still
exists, macro will rewrite it. Someone in network can have opened it at the
moment I want do rewrite.
Is there possibility of testing, if the file is able to rewrite? I don!t
want to go through Error Statement.

Thanks

karmela
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default test file - is open?

This should work on a network.

Option Explicit
Dim tFile As String
Dim hFile As Long

Sub CheckOpen()
tFile = "C:\Documents and Settings\karmela\My Documents\Book1.xls"
'use the fullname (including path)

If IsFileOpen(tFile) Then
MsgBox tFile & " is open"
Else
'replace with your code
MsgBox tFile & " is not open"

End If
End Sub

Function IsFileOpen(strFullPathFileName As String) As Boolean
On Error GoTo FileOpen
hFile = FreeFile
Open strFullPathFileName For Random Access Read Write Lock Read
Write As hFile
IsFileOpen = False
Close hFile
Exit Function
FileOpen:
IsFileOpen = True
Close hFile
End Function

Cliff Edwards
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default test file - is open?

Hi,

thanks... there is also On error... but maybe it is better in a separated
function as in the main procedure.

Is is possible to show, who has the file opened? You know, when openning a
file, that is opened by another user, Excel shows "this file is locked by
user xy" and you can choose - just read, get notice it is writeable... etc.

Thanks karmela

PS. Thank for existing this discussion groups, you have helped me very much
:-)



"ward376" wrote:

This should work on a network.

Option Explicit
Dim tFile As String
Dim hFile As Long

Sub CheckOpen()
tFile = "C:\Documents and Settings\karmela\My Documents\Book1.xls"
'use the fullname (including path)

If IsFileOpen(tFile) Then
MsgBox tFile & " is open"
Else
'replace with your code
MsgBox tFile & " is not open"

End If
End Sub

Function IsFileOpen(strFullPathFileName As String) As Boolean
On Error GoTo FileOpen
hFile = FreeFile
Open strFullPathFileName For Random Access Read Write Lock Read
Write As hFile
IsFileOpen = False
Close hFile
Exit Function
FileOpen:
IsFileOpen = True
Close hFile
End Function

Cliff Edwards

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default test file - is open?

Look he

http://www.xcelfiles.com/IsFileOpenVBA.htm

This isn't where I first found the code I use (I don't think) but it's
functionally almost identical.

You're very welcome and Thank You for the Thank You.

Cliff Edwards

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
VBA Assistance needed to test for open file in a different direct Brent E Excel Programming 4 May 2nd 07 07:19 PM
VBA Assistance needed to test for open file in a different direct Brent E Excel Discussion (Misc queries) 2 April 30th 07 11:39 PM
Open Test file with VBA with a changing file number Michael from Austin Excel Programming 4 October 14th 05 03:18 PM
Open file - How to test the path ? Alex St-Pierre Excel Programming 3 February 7th 05 06:36 PM
Test for Open File Aaron Excel Programming 6 January 6th 05 12:37 PM


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

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"