Export (0) Print
Expand All
Expand Minimize
3 out of 3 rated this helpful - Rate this topic

Locking Word 2007 Content Controls

Office 2007
Office Visual How To

Applies to: 2007 Microsoft Office System, Microsoft Office Word 2007

Overview

By using content controls, you can create interactive forms within Word documents. But sometimes you might want to restrict how a user works with a content control.

Code It

The following example sets the locking properties for a rich text textbox content control. The locking properties are based on the value that a user selects from two drop-down list content controls.

private void LockContentControl()
{
  // String variable for the
  // template path and filename.
  object filename = "c:\\customtemplate.dotx";

  // Document object variable for a new
  // document that uses the template.
  wordDoc = Application.Documents.Add(
    ref filename, ref missing,
    ref missing, ref missing);

  // Declare ContentControlOnExit event handler.
  wordDoc.ContentControlOnEnter += 
    new Microsoft.Office.Interop.Word
    .DocumentEvents2_ContentControlOnEnterEventHandler
    (ContentControl_Enter);
}

protected void ContentControl_Enter(
  Word.ContentControl ContentControl)
{
  // Create object variable for the current
  // content control.
  Word.ContentControl currentCC = ContentControl;

  // Switch statement for rules to follow depending
  // on which content control is being exited.
  switch (currentCC.Tag)
  {
    // This is the Rich Text textbox.
    case "text":
      #region Lock Content Control Code
      // Create a variable for the first drop-down
      // content control and get the value.
      Word.ContentControl lockCC = GetCC("lock");
      string vallock = lockCC.Range.Text;

      // If the value of the content control 
      // is lock, lock the content control
      // from future editing.
      switch(vallock)
      {
        case "Lock Contents":
        currentCC.LockContents = true;
        break;

        case "Unlock Contents":
          currentCC.LockContents = false;
          break;

        default:
          currentCC.LockContents = false;
          break;
      }
      #endregion

      #region Prohibit Content Control Code
      // Create a variable for the second drop-down
      // content control and get the value.
      Word.ContentControl prohibitCC = GetCC("prohibit");
      string valprohibit = prohibitCC.Range.Text;

      // If the value of the content control is 
      // prohibit, stop the user from deleting
      // the content control.
      switch (valprohibit)
      {
        case "Prohibit Delete":
          currentCC.LockContentControl = true;
          break;

        case "Allow Delete":
          currentCC.LockContentControl = false;
          break;

        default:
          currentCC.LockContentControl = false;
          break;
      }
      #endregion

      break;

    default:
      break;
  }
}

private Word.ContentControl GetCC(string Tag)
{
  foreach (Word.ContentControl cc in Application
    .ActiveDocument.ContentControls)
  {
    if (cc.Tag == Tag)
    {
      return cc;
    }
    }

  return null;
}
Read It

Word allows two restrictions for content controls. To set these restrictions, you use the LockContents and LockContentControl properties.

With the LockContents property, you can stop a user from changing the value of a content control. With the LockContentControl property, you can prohibit a user from deleting a content control.

See It

Locking Word 2007 Content Controls

Watch the Video

Video Length: 00:03:36

File Size: 4.7 MB WMV

Explore It

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.