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:
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.
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.