-
Mar 28th, 2003, 02:51 PM
#1
Thread Starter
New Member
Preventing workbooks from opening into same Excel
Hey you all,
Lets say you have a worksheet running a macro loaded into one instance of Excel. While this is going on, you don't want any other workbook to open into the same instance of Excel. Can anybody give me a hint on how i could prevent this from happening by using VBA?
Any thought would be greatly appreciated.
Volker
-
Mar 31st, 2003, 02:57 AM
#2
One way around this would be to hide the standard worksheet menu commandbar - shove 2 buttons onto a sheet & add this code to see what I'm talking about here:
VB Code:
[color="#0000A0"]Private[/color] [color="#0000A0"]Sub[/color] CommandButton1_Click()
CommandBars("Worksheet Menu Bar").Enabled = [color="#0000A0"]False[/color]
[color="#0000A0"]End[/color] [color="#0000A0"]Sub[/color]
[color="#0000A0"]Private[/color] [color="#0000A0"]Sub[/color] CommandButton2_Click()
CommandBars("Worksheet Menu Bar").Enabled = [color="#0000A0"]True[/color]
[color="#0000A0"]End[/color] [color="#0000A0"]Sub[/color]
-
Mar 31st, 2003, 09:11 AM
#3
Thread Starter
New Member
ok, this prevents the user from opening up a new workbook from within excel. but how about the case that someone will doubleclick an excel file in lets say file explorer? the file activated this way will still open into the excel in which the crucial workbook is running.
is there any excel startup option one could invoke that forces each workbook to open into its own instance of excel?
Volker
-
Apr 15th, 2003, 03:28 PM
#4
Junior Member
Volker-
I think the most foolproof way of handling this problem is the following. Excel can handle application level events, but the way to access it isn't like accessing events to lower object. The code below will close any workbook that is opened in the instance that the code is running and open it in a new instance of Excel. It is triggered by the event of a workbook opening. I hope this helps.
Put this in a class module called cApp:
Public WithEvents CApp As Application
Private Sub CApp_WorkbookOpen(ByVal wb As Excel.Workbook)
' Capture opened workbook info
Dim sPathName As String
sPathName = wb.Path & "\" & wb.Name
' Close it and open it in a new instance
wb.Close
Dim NewExcel As Excel.Application
Set NewExcel = CreateObject("Excel.Application")
With NewExcel
.Visible = True
.Workbooks.Open (sPathName)
End With
End Sub
Put this in a module:
Option Explicit
Public ClassApp As New CApp
Public Sub YourSub()
' Initialize and instantiate your class object to access application events.
Set ClassApp.CApp = Excel.Application
End Sub
-
Apr 16th, 2003, 08:01 AM
#5
Thread Starter
New Member
David,
just tried out the code snippet and it does exactly what I hoped it would do. the only problem is that my customized menu bar is reset while closing and reopening the workbook into a new instance of excel. but i can work around this.
thanks a lot,
Volker
-
May 17th, 2018, 11:20 PM
#6
New Member
Re: Preventing workbooks from opening into same Excel
Hey David,
I did used your code to restrict opening new excel workbook and allow to run the existing workbook. But it didn't worked in my case after inserting the code I saved the file as macro enabled file and reopened the file and as i type Ctrl N the new excel workbook opens..
Plz guide where do i am running wrong...
-
May 18th, 2018, 05:09 AM
#7
Re: Preventing workbooks from opening into same Excel
as this is 15 years and multiple versions of excel later, anything could have changed
opening an existing workbook is different from adding a new workbook
adding a new workbook does not fire the workbook_open event, so the code will not work in that case, you should either disable the menu items, which should also disable the shortcut keys, or have code in the workbook_new event
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|