Error When Open From Protected View

Marhier

Board Regular
Joined
Feb 21, 2017
Messages
128
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Good afternoon :)
I've written a bit of code that when my workbook is opened, it makes sure a particular sheet is visible and then activated/selected.
After it has done this, it makes sure all inactive sheets are marked as xlSheetVeryHidden.

The code works fine when opened up from 'My Documents'...
The issue I'm having is, all other users will be viewing a read only version, which is linked from an internal website.
Opening from there has Excel open in protected view... When users click 'Enable Content', they get the following error message:

Run-time error '1004':
Method 'Activate' of object'_Worksheet' failed



It doesn't stop my sheet from opening, and it seems to open at the correct page... Just it fails to bring up an important message box I want users to see when the document is opened.

My code in the 'ThisWorkbook' module and is as follows:
Code:
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim ws As Worksheet
With Home
    .Visible = xlSheetVisible
        .Activate
End With
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> ThisWorkbook.ActiveSheet.Name Then
ws.Visible = xlSheetVeryHidden
End If
Next ws
MsgBox "Please do not save this tool locally. Always open from Nexus to make sure you're using the most up to date prices"
Application.ScreenUpdating = True
End Sub


If someone could provide a solution to this problem, it would be greatly appreciated.
Thank you.
Regards
Martin
 
Hey ZVI and thank you for having a look.
I tried the code you suggested, but when opening from protected view, it gives me the following error:

Run-time error '1004'
Method 'OnTime' of object '_Application' failed

Kind regards
Martin
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
The code of the post 30 always works good on my side.
But theoretically it can depend on some factors.
Try this modification of that code:
Rich (BB code):
' Put all the below code into ThisWorkbook module
Option Explicit
 
Private Sub Workbook_Open()
  Application.CalculateUntilAsyncQueriesDone  ' <-- added
  Application.OnTime Now, Me.CodeName & ".Workbook_Open_Delayed"
End Sub
 
Private Sub Workbook_Open_Delayed()
  Application.CalculateUntilAsyncQueriesDone  ' <-- added (here is just for the testing)
  Application.ScreenUpdating = False
  Dim ws As Worksheet
  With Home
    .Visible = xlSheetVisible
    .Select
  End With
  For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> ThisWorkbook.ActiveSheet.Name Then
      ws.Visible = xlSheetVeryHidden
    End If
  Next ws
  MsgBox "Please do not save this tool locally. Always open from Nexus to make sure you're using the most up to date prices"
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
There is also another approach:
Rich (BB code):
' Put all the below code into ThisWorkbook module
Option Explicit
 
Dim IsInOpening As Boolean
 
Private Sub Workbook_Open()
  IsInOpening = True
End Sub
 
Private Sub Workbook_Activate()
  If IsInOpening Then
    IsInOpening = False
    If Val(Application.Version) >= 14 Then Application.CalculateUntilAsyncQueriesDone
    Application.OnTime Now, "'" & Me.FullName & "'!" & Me.CodeName & ".Workbook_Open_Delayed"
  End If
End Sub
 
Private Sub Workbook_Open_Delayed()
  ' Your code is here
  Application.ScreenUpdating = False
  Dim ws As Worksheet
  With Home
    .Visible = xlSheetVisible
    .Select
  End With
  For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> ThisWorkbook.ActiveSheet.Name Then
      ws.Visible = xlSheetVeryHidden
    End If
  Next
  MsgBox "Please do not save this tool locally. Always open from Nexus to make sure you're using the most up to date prices"
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,981
Messages
6,175,768
Members
452,668
Latest member
mrider123

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