Any events before Workbook_open event?

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
I am using the following code to basically catch any workbooks opened while my main workbook is opened. And it converts it to read only then closes it and starts it under its own instance of excel. It helps to keep my workbook seperate from all other ones. The problem is if the workbook being opened has the read only message option enabled then it asks "The author would like you to open "tracker.xls" as read only unless you need to make changes. Open as read-only?". I want to stop it from asking that until the workbook as been opened in its own instance. The issue is the App_WorkbookOpen(ByVal Wb As Workbook) event happens after the other workbooks message event happens. My code is set up as a class as shown below:

Code:
'Creating seperate instances using events
Public WithEvents App As Application




'Create a seperate instances for any excel files trying to open while this is open
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    MsgBox "test"
    Dim xlApp As Application


    If Wb Is ThisWorkbook Then
    
    Exit Sub
    
    End If
    
    If Wb.ReadOnly <> True Then
        Application.DisplayAlerts = False
        Wb.ChangeFileAccess xlReadOnly
        Application.DisplayAlerts = True
    End If
    
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Workbooks.Open Wb.FullName
    xlApp.Visible = True
    Wb.Close False
    'Just for this program since it seems to leave window back open again
    ThisWorkbook.Application.Visible = False
End Sub

Thisworkbook
Code:
Public Sub Workbook_Open()
    
    On Error GoTo ErrorHandler
    'Speed up vba code
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    'Icon Update
    Call SetIcon(ThisWorkbook.Path & "\Images\LOGO.ico", 0)
    
    'Seperate instance classes and GUIClass    
    Public ExcelGUIUpdates As New ExcelGUIClass


    'Seperate Instance Declaration
    Set ExcelGUIUpdates.App = Application
    
    'Delete itself from history
    Call ExcelGUIUpdates.DeleteRecentlyOpened
    
Exit Sub


ErrorHandler:
Debug.Print "Error Number: " & Err.Number & ", Error Message: " & Err.Description & ", Last DLL Error: " & Err.LastDllError


End Sub
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
On data tab, edit links, startup prompt and select "don't display the alert and update links.

Otherwise through VBA, if you want to keep let the user the opportunity to refuse when he opens,
Code:
application.displayalerts=false
and put back to true at the end of the macro should do the trick.
 
Last edited:
Upvote 0
Thanks for your solution but the Application.DisplayAlerts = False does not seem to work. I actually use that in the beginning of workbook_open in the thisworkbook module as you can see. So displayalerts has been set to false from the beginning from when i opened my main workbook.
 
Upvote 0
just for clarity, there is a line of code that would set alerts back on:
Code:
        Application.DisplayAlerts = False
        Wb.ChangeFileAccess xlReadOnly
        Application.DisplayAlerts = True
 
Upvote 0
Try this modification : (changes in blue)
Code:
'Creating seperate instances using eventsPublic WithEvents App As Application


'Create a seperate instances for any excel files trying to open while this is open
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    MsgBox "test"
    Dim xlApp As Application

    If Wb Is ThisWorkbook Then
    
    Exit Sub
    
    End If
    
    If Wb.ReadOnly <> True Then
        Application.DisplayAlerts = False
        Wb.ChangeFileAccess xlReadOnly
        Application.DisplayAlerts = True
    End If
    
    [COLOR=#0000ff][B]Set xlApp = CreateObject("Excel.Application")
    xlApp.DisplayAlerts = False
    xlApp.Workbooks.Open(Wb.FullName, IgnoreReadOnlyRecommended:=True).ChangeFileAccess xlReadOnly
    xlApp.Visible = True
    xlApp.DisplayAlerts = True[/B][/COLOR]
    Wb.Close False
    'Just for this program since it seems to leave window back open again
    ThisWorkbook.Application.Visible = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top