LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Multiple instances of the same file being opened

What happens:
A file on a network drive - call it "one", which when opened creates
a file on the local drive - call it "two", & drops all holds on file
"one" in doing so. File "two", when printed, updates file "one" then
file "two" in that order leaving file "two" open on the local drive
until closed.

The premise:
File "one" is left available to all users with no restrictions at any
time and is updated along with "two" as changes are made. The reason
for doing it this way is because some users do not relinquish control
of file "one" for days at a time which is totally unacceptable.

The problem:
A means of eliminating multiple instances of file "two" being opened
on one computer whether it is in the same instance of excel or
separate instances of excel. Remember that file "one" creates an
instance of file "one" as it is opened.

The solution?:
'===================================
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.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.

' Check to see which error occurred.
Select Case errnum

' No error occurred.
' File is NOT already open.
Case 0
IsFileOpen = False

' Error number for "Permission Denied."
' File is already.
Case 70
IsFileOpen = True

' Another error occurred.
Case Else
Error errnum
End Select
End Function

Private Sub Workbook_Open()
'=====================================
Dim MyFile As String
' Returns "one.xls" if it exists. Checks is "one" exists
' skipping the remaining checks if it does not
MyFile = Dir("c:\one.xls")
If MyFile = "" Then
MsgBox "File does not exist."
GoTo ThatsAll
End If
'=====================================
' Test to see if the file is open.
If IsFileOpen("c:\one.xls") Then
' Test to see if the file is open in this instance of excel
Dim bk As Workbook
On Error Resume Next
Set bk = Workbooks("one.xls")
On Error GoTo 0
If Not bk Is Nothing Then
'MsgBox(prompt[, buttons] [, title] [, helpfile, context])
MsgBox "Please use the previously opened file. This
file will close after clicking OK"
ThisWorkbook.Close True ' close the file without saving
GoTo secondcheck
End If
' Display a message stating the file in use.
MsgBox "File is already open"
Application.Quit ' close Excel
'
secondcheck: '
'=====================================
Workbooks.Open "c:\two.xls"

ThatsAll:
End If

'===========================
End Sub

The questions:
I have tested this and as near as I am able to tell the code will do
the job.... But... will this work for all possibilities and is there
better ways of accomplishing the same thing?

Please help.
 
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
add multiple x values to multiple instances of y coordinate evrk Charts and Charting in Excel 0 January 18th 11 10:57 PM
Multiple instances of Excel (Multiple workbooks) Shane Setting up and Configuration of Excel 3 November 13th 09 05:07 PM
Create list of unique instances from list of multiple Instances Dishon Excel Worksheet Functions 0 March 3rd 08 10:46 AM
Opening file in Excel 2003 opens multipule instances of same file Ed_B Excel Discussion (Misc queries) 1 June 21st 07 07:10 PM
cannot open the excel file, the file is already opened cannot open the excel document Excel Discussion (Misc queries) 1 May 19th 06 07:45 AM


All times are GMT +1. The time now is 02:07 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"