PDA

View Full Version : prevent multiple instances of excel



BrianMH
06-14-2011, 06:55 AM
Is there any way to prevent multiple instances of excel? So that if I click an excel shortcut instead of opening a new instance of the exe it just brings my already open excel application to the front?

Thanks

av8tordude
06-14-2011, 10:06 AM
I found this code and it has help me with a similar problem. You will need to adjust the code to fit your needs.

Option Explicit
Public WithEvents xlApp As Excel.Application

Private Sub Workbook_Open()
Set xlApp = Application
If xlApp.Workbooks.Count > 1 Then
MsgBox "This workbook cannot be opened if other " _
& "workbooks are already opened"
Me.Close savechanges:=False
End If
End Sub

Private Sub Workbook_Close()
Set xlApp = Nothing
End Sub

Private Sub xlApp_NewWorkbook(ByVal Wb As Workbook)
If xlApp.Workbooks.Count > 1 Then
Wb.Close savechanges:=False
MsgBox "Can't create a new workbook now!"
End If
End Sub

Private Sub xlApp_WorkbookOpen(ByVal Wb As Workbook)
If xlApp.Workbooks.Count > 1 Then
Wb.Close savechanges:=False
MsgBox "Can't open a new workbook now!"
End If
End Sub

BrianMH
06-14-2011, 11:23 AM
Thanks for the info. Although this looks more like it would prevent multiple workbooks in the same application instance not prevent multiple instances of an application.