AddIn processing files in Protected View

pgsmick

New Member
Joined
Apr 29, 2013
Messages
8
I am continuing to work on a 2010 AddIn that examines every file opened in Excel and password protects the worksheet and workbook if the opened file meets certain criteria.

My current issue is that if the file has come from the Internet (or certain other locations), it comes into the Excel ProtectedView sandbox and gives me problems. Here is the code:

Code:
[IN ThisWorkBook Module:]
Public Sub WorkBook_Open()  


    Application.OnTime Now, "'" & ThisWorkbook.FullName & "'!Init"
    
End Sub
--------------------


[IN modInit module:]


Option Explicit
Dim NewApp As clsApp
------------------------
Public Sub Init()


Set NewApp = Nothing
Set NewApp = New clsApp
Set NewApp.App = Application


End Sub
------------------------


[IN clsApp Class Module:]


Public WithEvents App As Application
Public CurrentBook as Workbook
Public CurrentSheet as Worksheet
-----------------------------------------
Private Sub App_WorkbookOpen(ByVal WB As Workbook)


'Code here runs immediately after user clicks on Enable Editing
'But workbook is actually still in ProtectedView sandbox.


'Hence any further code has to determine if the document is or is not in ProtectedView sandbox (a nuisance)"
'and also cannot use any commands that are unavailable in ProtectedView sandbox.


If Application.ProtectedViewWindows.Count > 0 Then
    Set CurrentBook = Application.ProtectedViewWindows(1).Workbook
    Set CurrentSheet = CurrentBook.ActiveSheet
Else
    Set CurrentBook = ActiveWorkbook
    Set CurrentSheet = ActiveWorkbook.ActiveSheet
End If



CurrentBook.Protect Password:="password"  'this command errors if the workbook is still in the sandbox



End Sub

Here is the sequence of what I see upon opening a file from the Internet,
0. Excel opens.
1. the AddIn macro's WorkBook_Open event runs,
2. the Init subroutine runs,
3. the document appears on screen with the Enable Editing prompt.
4. Macro processing pauses.
When I click on Enable Editing:
5. the prompt goes away (but the title bar still shows the workbook to be in Protected View)
6. The App_Workbook_Open event begins running.
7. error is delivered when CurrentBook.Protect command is encountered.


My meager understand of the sandbox operation is that when the user clicks on "enable editing", the workbook somehow gets transferred to a different Excel process and then the sandbox process is terminated. The current problem suggests that the macro is held until the user has clicked enable editing, but resumes while the file is still in the sandbox.

So, what does one need to do to get the workbook fully out of ProtectedView after the user has clicked Enable Editing? Or delay resumption of the macro processing until the workbook is finally out of ProtectedView?

Thanks,
Peter.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
OnTime works fine even if there is no open workbook, but if the "active" workbook is in protected view (since a protected view window can't be the active window) then OnTime fails.

You do a little test, and if it seems that a protected view window is in front, then create a new window temporarily, run the `Application.OnTime`, and close the temporary workbook.

VBA Code:
    If ActiveWindow Is Nothing And Application.Windows.Count > 0 Then
        Dim bWkbkTemp As Boolean
        bWkbkTemp = True
        Dim wbTemp As Workbook
        Set wbTemp = Workbooks.Add
    End If
    Application.OnTime Now + TimeValue("0:00:02"), "OnTimeFunction"
    If bWkbkTemp Then
        wbTemp.Close False
    End If
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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