Single instance of excel

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
I have the following class event such that if another workbook opens up then it opens up a new instance in excel. However, some of the documents we use have a notification that asks the user if they want to open as read only or not on open. The problem is when one of these documents is open my code still allows the users messages to go through to ask fi they want read only or not and then once they click then my code executes to open new instance then close old one. However, in this time it then asks the users again if they want to do read only or not. I dont want the user to even know this since instance subroutine is happening so asking twice is kind of obvious. Anyways around this such that my event executes before the message?

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

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Do you mean workbooks with ReadOnlyRecommended ?
 
Last edited:
Upvote 0
Try this (change in red)
Code:
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)      
    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, [COLOR=#ff0000][B]IgnoreReadOnlyRecommended:=True[/B][/COLOR]
    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
 
Upvote 0
So settting this to readonlytrue on the 2nd instance opening takes away the option for the user to be asked. I still need it to ask that, but not twice. The first instance of it opening and asking needs to be stopped but the second instance opening should allow the message. Does that make sense?
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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