Workbook_Open errors after Protected View

jpmccreary

New Member
Joined
Jul 21, 2010
Messages
11
I am using Office 2010. Opening the workbook works fine when not in Protected View. However, anytime it is in Protected View (opening from email) the user gets the following error after clicking Enable Editing:
Run-time error '91':
Object variable or With block variable not set

I ran the debugger and found that all of the code between START and END triggers the error. Is there anyway to prevent this error?

Rich (BB code):
Private Sub Workbook_Open()
 
    With Application
        .EnableEvents = True
        .ScreenUpdating = False
        .Calculation = xlCalculationAutomatic
    End With   
 
'**** START
    With ActiveWindow
        .WindowState = xlMaximized
        .LargeScroll UP:=65536, ToLeft:=256
        .DisplayWorkbookTabs = False
        .DisplayHorizontalScrollBar = True
        .DisplayVerticalScrollBar = True
    End With
 
    LockAllSheets 'code below
    If ActiveWindow.DisplayWorkbookTabs = True Then MsgBox "Tabs are visible", vbOKOnly
 
    With ActiveWorkbook
        .Protect Password:=Range("PWW_PWD"), Structure:=True, Windows:=False
        If .ProtectStructure = False Then MsgBox "Workbook is Unprotected", vbOKOnly
        .Sheets("Home").Select
        .Sheets("Home").Shapes("tbReset").Visible = False
    End With
'**** END
 
    If ActiveSheet.ProtectContents = False Then MsgBox "Worksheet is Unprotected", vbOKOnly
    Application.ScreenUpdating = True
 
End Sub
 
Sub LockAllSheets()
        
    Dim i As Integer
    Dim Sheet As Worksheet
    Dim sPWD As String
    
    sPWD = Range("PWS_PWD")
    
    i = 1
    For Each Sheet In Worksheets
        With Worksheets(i)
            .Protect Password:=sPWD, DrawingObjects:=True, Contents:=True, Scenarios:=True, UserInterFaceOnly:=True
            .EnableSelection = xlNoRestrictions
        End With
        i = i + 1
    Next Sheet
    
End Sub
 
I coded a workaround to my issue. In ThisWorkbook I have the following code:

Code:
Dim bProtected_View As Boolean
[COLOR=blue]'Workbook_Activate code should run only when opened in Protected View[/COLOR]
 
Private Sub Workbook_Open()
 
    With Application
        .EnableEvents = True
        .ScreenUpdating = False
        .Calculation = xlCalculationAutomatic
    End With
 
    If Application.ProtectedViewWindows.Count = 0 Then
        bProtected_View = False
    Else
        bProtected_View = True
    End If
 
    If bProtected_View = False Then
    [COLOR=blue]' WILL NOT RUN FROM WORKBOOK_OPEN WHILE IN PROTECTED VIEW[/COLOR]
        With ActiveWindow
            .WindowState = xlMaximized
            .LargeScroll UP:=65536, ToLeft:=256
            .DisplayWorkbookTabs = False
            .DisplayHorizontalScrollBar = True
            .DisplayVerticalScrollBar = True
        End With
        LockAllSheets
        If ActiveWindow.DisplayWorkbookTabs = True Then MsgBox "Tabs are visible", vbOKOnly
        With ActiveWorkbook
            .Protect Password:=Range("PWW_PWD"), STRUCTURE:=True, Windows:=False
            If .ProtectStructure = False Then MsgBox "Workbook is Unprotected", vbOKOnly
            .Sheets("Home").Select
            .Sheets("Home").Shapes("tbReset").Visible = False
        End With
        [COLOR=blue]'WILL NOT RUN FROM WORKBOOK_OPEN WHILE IN PROTECTED VIEW[/COLOR]
    End If
 
    If ActiveSheet.ProtectContents = False Then MsgBox "Worksheet is Unprotected", vbOKOnly
    Application.ScreenUpdating = True
 
End Sub
 
Private Sub Workbook_Activate()
 
    If bProtected_View = True Then
        bProtected_View = False
        [COLOR=blue]'WILL NOT RUN FROM WORKBOOK_OPEN WHILE IN PROTECTED VIEW[/COLOR]
        With ActiveWindow
            .WindowState = xlMaximized
            .LargeScroll UP:=65536, ToLeft:=256
            .DisplayWorkbookTabs = False
            .DisplayHorizontalScrollBar = True
            .DisplayVerticalScrollBar = True
        End With
 
        LockAllSheets
        If ActiveWindow.DisplayWorkbookTabs = True Then MsgBox "Tabs are visible", vbOKOnly
 
        With ActiveWorkbook
            .Protect Password:=Range("PWW_PWD"), STRUCTURE:=True, Windows:=False
            If .ProtectStructure = False Then MsgBox "Workbook is Unprotected", vbOKOnly
            .Sheets("Home").Select
            .Sheets("Home").Shapes("tbReset").Visible = False
        End With
        [COLOR=blue]'WILL NOT RUN FROM WORKBOOK_OPEN WHILE IN PROTECTED VIEW[/COLOR]
    End If
End Sub
 
Upvote 0

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