I need to ask the user to save an attachment (to an email) to a folder before editing it. For this I thought I would set up the events and go but it doesn't work. It *never* executes "ProtectedViewWindowBeforeEdit" event (which is where we tell the user to save the document before editing). Any ideas highly appreciated. Thanks.
Dim WithEvents oApp As Application
Dim WithEvents appWrd as Word.Application
Private Sub Document_Open()
If appWrd Is Nothing Then
Set oApp = Application
Set appWrd = Word.Application
If Not Application.ActiveProtectedViewWindow Is Nothing Then
' It displays this message when a 'protected document' is enabled for editing
MsgBox ("Protected Mode")
Else
' do some other stuff before opening the document for editing
End If
End If
End Sub
Private Sub oApp_ProtectedViewWindowBeforeEdit(ByVal Window As ProtectedViewWindow, Cancel As Boolean)
' It *NEVER* gets here
MsgBox ("Please save the attachment to a folder before editing it")
Cancel = True
End Sub
Word disables macros in protected view. So the Document_Open event procedure is not executed and hence oApp is not initialized.
The code would already have to be running before the user opens the document in protected view. But that clearly isn't possible if you send the document by e-mail.
BTW, why do you have TWO variables of type Application?
HansV wrote:Word disables macros in protected view. So the Document_Open event procedure is not executed and hence oApp is not initialized.
The code would already have to be running before the user opens the document in protected view. But that clearly isn't possible if you send the document by e-mail.
BTW, why do you have TWO variables of type Application?
Why I have two variable of type application? Doh. Should only be one.
Document_Open is called when I click 'Enable Editing' & I get the message displayed on the screen as a confirmation. oApp is initialised. It just doesn't trigger"oApp_ProtectedViewWindowBeforeEdit". I put in another event "oApp_ProtectedViewWindowDeactivate" for testing & this event *is* triggered (this event is triggered when we click Enable Editing).
Of course not. Once again: when you open a document in protected view, macros are disabled. As a consequence, the Document_Open event procedure does not run. If you then enable document content, and enable macros, the document is already open. So the Document_Open event procedure still doesn't run. And hence the various ProtectedViewWindow... event procedures are never activated.
You simply cannot do what you want. Instead, warn the recipient that they should not open the attachment directly, but save it to disk first, then open the saved version.
HansV wrote:Of course not. Once again: when you open a document in protected view, macros are disabled. As a consequence, the Document_Open event procedure does not run. If you then enable document content, and enable macros, the document is already open. So the Document_Open event procedure still doesn't run. And hence the various ProtectedViewWindow... event procedures are never activated.
You simply cannot do what you want. Instead, warn the recipient that they should not open the attachment directly, but save it to disk first, then open the saved version.
Ok. Thank you Hans. I agree to disagree with you just this one :0) as I know it works.
HansV wrote:So how did you get it to work for the recipient of the e-mail? I know that you can get it to work on your own PC.
Aplogies for delay in replying, I was away for a while.
I just sent him a document to open and it came up with Enable Editing. When Enable Editing is clicked, it ran the code.
In any case, the company has decided to abandon the 'Enable Editing' feature for time-being - phew.