White Paladin

I'm converting a Word template solution to a VSTO C# addin. In the template I was able to intercept Word commands by using the suggestions found here:

Below is a listing of the VBA code that I store in a global template. I would like to move this code into my VSTO C# addin. Is there a way to intercept the Word commands from C#   It appears from the answer above that there is not a way to intercept the Word commands.  Is this going to change for VSTO

If it is not possible to intercept the Word commands, how can I at least move the code in the ProtectIt() method below into the VSTO C# addin and call it from the template

Thank you for your help,

'         Module:   Protection
Option Explicit

Sub ProtectForm()
    ProtectIt (False)
End Sub

Public Sub ToolsProtectUnprotectDocument()
    ProtectIt (True)
End Sub

'   Protected the document the same way it is normally protected,
'   but also enable/disable our controls
Private Sub ProtectIt(bUseDialog As Boolean)
    Dim bIsUnprotected As Boolean
    bIsUnprotected = (ActiveDocument.ProtectionType = wdNoProtection)
    If bIsUnprotected Then
        If bUseDialog Then
            ActiveDocument.Protect wdAllowOnlyFormFields, True
        End If
        ActiveDocument.Unprotect Password:=""
    End If
End Sub

Re: Visual Studio Tools for Office Intercepting commands in Word 2003

Cindy Meister

Hi Brett

There is no exact equivalent for intercepting the built-in commands using Word 2003. Also, there are no events directly connected to document protection. What you could do is intercept the WindowSelectionChange event at the application level, testing whether the protection has been changed. Here's a sample to illustrate the principle using VBA:

'Code in a normal module to initialize the class with the events
Public x As clsTest

Sub StartClass()
Set x = New clsTest
Set x.app = Word.Application
Set x.ManagedDoc = ActiveDocument
x.ProtectionStatus = ActiveDocument.ProtectionType
End Sub

'Code in the class module
Public WithEvents app As Word.Application

'Properties to manage the document and the protection
Private mProtectionStatus As Long
Private mDoc As Word.Document

Property Set ManagedDoc(doc As Word.Document)
Set mDoc = doc
End Property

Property Let ProtectionStatus(protected As Long)
mProtectionStatus = protected
End Property

Property Get ProtectionStatus() As Long
ProtectionStatus = mProtectionStatus
End Property

Private Sub app_WindowSelectionChange(ByVal Sel As Selection)
'Tests whether protection has been changed
If mProtectionStatus <> mDoc.ProtectionType Then
MsgBox "Protection status changed"
mProtectionStatus = mDoc.ProtectionType
End If
End Sub