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
 
Try removing any startup files in either of your XLSTART folders and see if that helps.
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
There was no file location in the field under Settings | Advanced | General.

Where are the XLSTART folders located for Excel 13?

I've looked here, but couldn't find anything:
C:\Program Files\Microsoft Office\Office15

Regards
Martin
 
Upvote 0
I found it.
I typed ?Application.StartupPath in the Immediate field in VB... Gave me the following:

C:\Users\*my user name*\AppData\Roaming\Microsoft\Excel\XLSTART

This folder was empty.

Is there another location to check?
 
Upvote 0
Are you using 32 or 64 bit Office? If it's 32 bit, you need to look in Program Files (x86) rather than Program Files.
 
Last edited:
Upvote 0
C:\Program Files (x86)\Microsoft Office\Office15\XLSTART
C:\Users\*my user name*\AppData\Roaming\Microsoft\Excel\XLSTART

Both of these folders were empty.
:(
 
Upvote 0
Can you try the ScreenUpdating API alternative and see if it works :

Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

Private Const WM_SETREDRAW = &HB

Private Property Let API_ScreenUpdating(ByVal Update As Boolean)
    SendMessage Application.hwnd, WM_SETREDRAW, Update, 0
End Property


Private Sub Workbook_Open()

    Dim ws As Worksheet
    
    On Error GoTo errHandler
    
[B][COLOR=#0000ff]    API_ScreenUpdating = False[/COLOR][/B]
    
    With Home
        .Visible = xlSheetVisible
    End With
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> Home.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
    
errHandler:
[COLOR=#0000ff][B]    API_ScreenUpdating = True[/B][/COLOR]
    If Err <> 0 Then Err.Raise Err.Number

End Sub
 
Upvote 0
I've noticed that the above code doesn't update the screen tabs after it is executed so try the following code instead :
Code:
Option Explicit

[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    Private Declare PtrSafe Function InvalidateRect Lib "user32" (ByVal hwnd As LongPtr, ByVal lpRect As Long, ByVal bErase As Long) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As Long, ByVal bErase As Long) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

Private Const WM_SETREDRAW = &HB

Private Property Let API_ScreenUpdating(ByVal Update As Boolean)
    SendMessage Application.hwnd, WM_SETREDRAW, Update, 0
    If Update Then Call InvalidateRect(0, 0, 0)
End Property


Private Sub Workbook_Open()

    Dim ws As Worksheet
    
    On Error GoTo errHandler
    
    [B][COLOR=#0000ff]API_ScreenUpdating = False[/COLOR][/B]
    
    With Home
        .Visible = xlSheetVisible
    End With
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> Home.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"
         
errHandler:
    [COLOR=#0000ff][B]API_ScreenUpdating = True[/B][/COLOR]
    If Err <> 0 Then Err.Raise Err.Number

End Sub
 
Last edited:
Upvote 0
Hi Jaafar, and thank you for your help.
I tried the code you suggested above, but when opening from protected view, my screen still flickers 50 times, obviously setting all sheets in the workbook to xlSheetVeryHidden.

Any other suggestions?

Also, Rory.
No .xlb files.

Regards
Martin
 
Upvote 0
Have found this (old) thread in the Trending Topics list on the bottom of the forum's page.
May be this?
Rich (BB code):
' Put all the below code into ThisWorkbook module
Option Explicit
 
Private Sub Workbook_Open()
  Application.OnTime Now, Me.CodeName & ".Workbook_Open_Delayed"
End Sub
 
Private Sub Workbook_Open_Delayed()
  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
 
Upvote 0

Forum statistics

Threads
1,223,981
Messages
6,175,771
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