Results 1 to 7 of 7

Thread: Preventing workbooks from opening into same Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Location
    Pittsburgh, PA
    Posts
    3

    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

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    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:
    1. [color="#0000A0"]Private[/color] [color="#0000A0"]Sub[/color] CommandButton1_Click()
    2.     CommandBars("Worksheet Menu Bar").Enabled = [color="#0000A0"]False[/color]
    3. [color="#0000A0"]End[/color] [color="#0000A0"]Sub[/color]
    4.  
    5. [color="#0000A0"]Private[/color] [color="#0000A0"]Sub[/color] CommandButton2_Click()
    6.     CommandBars("Worksheet Menu Bar").Enabled = [color="#0000A0"]True[/color]
    7. [color="#0000A0"]End[/color] [color="#0000A0"]Sub[/color]

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Location
    Pittsburgh, PA
    Posts
    3
    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

  4. #4
    Junior Member
    Join Date
    Aug 2000
    Location
    CA,USA
    Posts
    17
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Location
    Pittsburgh, PA
    Posts
    3
    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

  6. #6
    New Member
    Join Date
    May 2018
    Posts
    1

    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...

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
  •  



Click Here to Expand Forum to Full Width