How can I restrict MS-Word to pre-defined styles but still allow inline format changes

1k views Asked by At

I am trying to create a MS Word 2013 document-level add-in with Visual Studio Tools for Office (VSTO) that implements a restricted editing environment. Two particular requirements of the add-in are:

  1. The user is restricted to using pre-existing styles. These styles cannot be changed and new styles cannot be created.
  2. Certain inline formatting changes are allowed, for example, Bold, Italic, Underline, Superscript, Subscript.

I am having trouble creating a solution that satisfies both of these requirements. The approaches I have tried so far have all hit road-blocks:

A. Use Word's Restrict Editing feature via ThisDocument.Protect(.., enforceStyleLock=true).

  • This satisfies the first requirement, but it also disables all of the inline formatting abilites, in particular all controls on Ribbon->Home_Tab->Font_Group. In addition, the font pop-up with the same controls that usually appears when you hover over some selected text is also disabled.
  • I have tried re-enabling the affected commands using a customUI in ribbon.xml, but this appears to be ineffective when the document is protected.

B. As above, but I've re-implemented controls for the inline formatting operations that I want to allow.

  • I can use the same icons from Word and can get Font group on the ribbon that looks pretty close to Word's, but has my re-implemented code behind it.
  • There are some subtle issues, such as responding to keyboard shortcuts does not work (e.g. Ctrl-B does not do Bold because Word thinks it's disabled), and it's not always possible to reflect the current font characteristics on the re-implemented controls (e.g. if Boldness changes as a result of some operation other than clicking my Bold control, then the control does not know when to update its 'pressed' state). These issues may be solvable with more research but I'm conscious of hitting a never-ending stream of special cases, and of course it does not help with this next item..
  • No formatting controls appear when hovering over selected text. I can't find any way to re-implement that Word feature.

C. Do not use Restrict Editing, but try to emulate its functionality by modifying the UI

  • I have a custom ribbon.xml with a <commands> section that disables certain commands which nicely reflects in the built-in controls on the Ribbon
    <?xml version="1.0" encoding="UTF-8"?>
    <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
      <commands>
        <command idMso="Font" enabled="false" />
        <command idMso="FontSize" enabled="false" />
        ...
      </commands>
    </customUI>
  • This also works nicely for the pop-up Font control when hovering over selected text.
  • There are some things that I cannot figure out how to control:
    • Right-clicking items in the Styles gallery (Ribbon->Home_Tab->Style_Group) shows a context menu that I cannot control and has unwanted items (such as Modify... and Rename...)
    • The Styles pane has a New Style button at the bottom left that I cannot disable (protected mode disables it).
    <command idMso="StylesPaneNewStyle" enabled="false"/>  (ineffective)

This leads to some specific questions. Answering any of them will help me move forward:

  1. Are there any other approaches I should consider to implement my restricted editing environment in Word?

  2. Regarding approach A, is there a programmatic way to re-enable commands that have been disabled by protecting the document.

  3. Regarding approach B, is there a way to re-implement the selected-text font controls pop-up on hover?

  4. Regarding approach C, is there a way to influence the Styles context menu and the New Style button in the Styles pane? Alternatively, is there some event I could monitor to determine when style changes have been attempted, and perhaps prevent them?

0

There are 0 answers