none
Setting Empty Placeholder Text for a Content Control

    Question

  • I'm working on a managed code, Word 2007 (application-level) Add-In using Visual Studio 2008. As part of this application, I am dynamically creating a plain text content control that will eventually be XML-mapped to some customXML data. I understand that when the content control is not XML-mapped and it contains no content, that it will show its placeholdeer text, which is "Click here to enter text." by default.

     

    I would like to be able to eliminate this placeholder text altogether. I have been able to manipulate the this placeholder text using the SetPlaceholderText method of the (Microsoft.Office.Interop.Word) ContentControl class. In fact, by "omitting" all of the parameters to this method, I am able to create the content control without any placeholder text. The problem is that when there exists a content control created in this manner with empty placeholder text, I am unable to toggle the document out of "Design Mode" programmatically or manually (via Word Ribbon > Developer > Controls > Design Mode). In both cases I am presented with an error message stating the following:

     

    "Word cannot turn off design mode because placeholder text in a content control contains invalid items. Placeholder text cannot contain items such as floating objects, revision marks, or content controls. Remove these items from the placeholder text and try again."

     

    I have included some sample code (C#) that generates this error below:

    Code Snippet

    private void CreateContentControl()

    {

    Application app = Globals.ThisAddIn.Application;

    Document doc = app.ActiveDocument;

     

    Object defaultRange = System.Type.Missing;

    // create the content control

    ContentControl contentCtrl = this.Application.ActiveDocument.ContentControls.Add(WdContentControlType.wdContentControlText, ref defaultRange);

     

    // set content control's properties

    contentCtrl.Title = "My Title";

    contentCtrl.Tag = "My Tag";

     

    // Exit Design Mode

    if (doc.FormsDesign)

    {

    // enable design mode if it is not already enabled

    doc.ToggleFormsDesign();

    }

     

    // set the placeholder text to be empty

    contentCtrl.SetPlaceholderText(null, null, String.Empty);

     

    // Re-enter Design Mode

    if (!doc.FormsDesign)

    {

    // Error here attempting to turn Design Mode on:

    // "Word cannot turn off design mode..." COMException here

    doc.ToggleFormsDesign();

    }

    }

     

     

    I have also tried setting the placeholder text to null instead of an empty string; in this case the following error occurs when setting the placeholder text:

     

    "Value does not fall within the expected range."

     

    This MSDN article suggests, "You can set placeholder text to appear or not appear by using the ShowPlaceholderText property." However, I do not see this property on the ContentControl class and the closest equivalent I have found, ShowingPlaceHolderText, is read-only and its purpose seems to be different than the need here.

     

    Is there a better or recommended way to eliminate a plain text content control's placeholder text? If not, is there a way to  workaround the "Word cannot turn off design mode..." error? I would like the user of the application to have the freedom to be able to turn design mode on and off as they are designing the document. Ideally I do not want any placeholder text when the document prints or even when design mode is off.

     

    Any thoughts or advice on this would be greatly appreciated.

     

    Thanks,
    Harry Sauers

    Friday, September 12, 2008 2:35 PM

Answers

  • Hi Harry

     

    Mmmm. That article was originally written during the Office 2007 beta (in 2006). I suspect what happened is that this part was missed when the article was updated at RTM.

     

    Content controls base on a lot of the same functionality as XML nodes. The XML nodes do have that property, and I suspect that Content Controls originally did, as well. I'm guessing that they ran into problems with content controls during the beta and decided to remove it. It makes sense, in a way, as how will a user ever find a Content Control that has no content and no placeholder?

     

    What you could do is change the attributes of the Placholder Text style. This is the default font formatting for an "empty" content control. Set the font attribute "Hidden" to True and, unless the user has changed the default Print options, the content control will not print out. It will only be visible on-screen if the Display option to show hidden font is activated.

     

    Friday, September 12, 2008 3:57 PM
    Moderator

All replies

  • Hi Harry

     

    Mmmm. That article was originally written during the Office 2007 beta (in 2006). I suspect what happened is that this part was missed when the article was updated at RTM.

     

    Content controls base on a lot of the same functionality as XML nodes. The XML nodes do have that property, and I suspect that Content Controls originally did, as well. I'm guessing that they ran into problems with content controls during the beta and decided to remove it. It makes sense, in a way, as how will a user ever find a Content Control that has no content and no placeholder?

     

    What you could do is change the attributes of the Placholder Text style. This is the default font formatting for an "empty" content control. Set the font attribute "Hidden" to True and, unless the user has changed the default Print options, the content control will not print out. It will only be visible on-screen if the Display option to show hidden font is activated.

     

    Friday, September 12, 2008 3:57 PM
    Moderator
  • Thanks again Cindy!

     

    It appears that that will get us what we need. I just added:

     

    contentCtrl.Range.Font.Hidden = 1; // 1 is true

     

    to the code I showed previously and that did the trick. The placeholder text is of course still visible in the document but it does not print (by default) which I believe will be good enough.

     

    I appreciate your help.

     

    Thanks,

    Harry

    Friday, September 12, 2008 9:05 PM
  • <<The placeholder text is of course still visible in the document but it does not print (by default) which I believe will be good enough.>>

     

    OK :-) And remember: if the client says the CC should also not be visible on-screen, then it's just a matter of changing a Display option for the application.

    Sunday, September 14, 2008 7:19 AM
    Moderator