PDA

View Full Version : How can you stop users from opening a file twice.



slang
06-03-2008, 04:44 PM
Is there a way to check to see if a workbook is already open and not allow them to open it again?
I have my app out there and some users have their taskbars using the group simalar icons and they are opening multiple instances.

AAARRRGGGG!!!:banghead:

Simon Lloyd
06-03-2008, 06:39 PM
I'm not quite sure perhaps something like:

Private Sub Workbook_Open()
If ThisWorkbook.IsInplace = True Then
MsgBox "Workbook Already Open"
ThisWorkbook.Close (False)
Else
MsgBox "Single Instance Open"
End If
End Sub

slang
06-04-2008, 07:55 AM
Thanks, it works great!

In my app all of the sheets are hidden except the one the user is working in. How can I prevent them from closing Excel and saving the changes without being in the main sheet?:whip

I would like a msgbox that told them to continue to the end of the process and back to the main screen.

Would that be another before save function like the one below? That one you sent works great to stop them from renaming the file.:devil2:

I am starting to get back into this VBA stuff after 8 years it is coming back. Sloooowly. Must be the age..:think:
Thanks again.....

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sFile
If SaveAsUI Then
Application.EnableEvents = False
Cancel = True
ThisWorkbook.Save
End If
Application.EnableEvents = True
End Sub

CreganTur
06-04-2008, 09:29 AM
Hmmm...:think: If I understand what you want, then maybe something like this will help:

Sub FileSave()
MsgBox "Saving changes to the document is not allowed." & vbCrLf & vbCrLf _
& "Please close the document when you are finished.", , "Action Canceled"
End Sub

Sub FileSaveAs()
MsgBox "Saving changes to the document is not allowed." & vbCrLf & vbCrLf _
& "Please close the document when you are finished.", , "Action Canceled"
End Sub


This code sets it so that if the User selects Save or Save As (or clicks the save icon on the toolbar) then the message will appear.

Then you could build your own save button into the macro that will save and close workbook(if desired). Or, you could disable these events on the last sheet using the load/open event.

This code works in Word, so it might need a little tweaking for Excel use...not sure.

slang
06-04-2008, 03:28 PM
The problem it when the user closes the workbook from another sheet other than the main sheet and another yes to the save changes on exit.
Is there anyway of stopping them from exiting from excel unless they are on the sheet "Main"?

mikerickson
06-04-2008, 07:39 PM
Putting this in the ThisWorkbook module is a belt and braces approach.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Sheets("Main").Activate
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Sheets("Main").Activate
End Sub

grichey
06-04-2008, 07:53 PM
Putting this in the ThisWorkbook module is a belt and braces approach.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Sheets("Main").Activate
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Sheets("Main").Activate
End Sub



belt and braces :rofl: :rofl:

slang
06-05-2008, 05:15 AM
Yeh, I know.:mkay
Bad design coupled with almost 40 users with a wide range of PC skills will result in my aging at an accelerated rate until this requirements gathering exercise is over and we have a solution "professionally" developed.: pray2:

Thanks for the help, I am not quite done yet...