Runtime error 91 only occurs after Protected view request

290 views
Skip to first unread message

tskogstrom

unread,
Sep 14, 2014, 2:40:02 PM9/14/14
to exce...@googlegroups.com

An error I find hard to analyse.

 

I got a xlsm file with a lot of code, too much to describe here. If I open it from window's explorer, or from inside excel, I get no error. 

When I send it by mail and the file automatically opens with the "protected view" question and I accept "editing", I get runtime error 91. (Error 91 = "Object variable or With block variable not set)


No alarm from VBA editor's debug compile VBAProject function.



Any idea why runtime error 91 only occur after protected view request?


/Happy for any answer

tskogstrom

unread,
Sep 15, 2014, 4:48:51 AM9/15/14
to exce...@googlegroups.com
My solution:

InThisWorkbook:
Option Explicit
Dim WBprotected As Boolean

Private Sub Workbook_Open()
    If Application.ProtectedViewWindows.Count > 0 Then
    Debug.Print "ProtectedViewWindows.Count > 0"
        WBprotected = True
    Debug.Print "WBprotected = True"
        Processing = True
    Debug.Print "Processing = True"
    End If
End Sub

Private Sub Workbook_Activate()
    If WBprotected = True Then
        Processing = False      'WkBk now enabled
        Debug.Print "Processing = False -> Auto_Open will not automatically run, so force it to run"
        Call Auto_open          'Auto_Open will not automatically run, so force it to run.
    Else
    Debug.Print "Auto_Open will automatically run"
    End If
End Sub
Private Sub Auto_open()
Debug.Print "#START Auto_open"
' [Code you want to run on opening]
End Sub

If you have SelectionChange events in workbooks you need to do more
1. In any module you add:
     Public Processing As Boolean

2. In the worksheets needed, you add:
     Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     Debug.Print "SelectionChange Processing = " & Processing
     If Processing = True Then Exit Sub


/Good luck!
tskogstrom

------------------------------------------------
Reply all
Reply to author
Forward
0 new messages