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:
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.Tag = "My Tag";
// Exit Design Mode
// enable design mode if it is not already enabled
// set the placeholder text to be empty
null, null, String.Empty);
// Re-enter Design Mode
// Error here attempting to turn Design Mode on:
// "Word cannot turn off design mode..." COMException here
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 theShowPlaceholderTextproperty." However, I do not see this property on the ContentControl class and the closest equivalent I have found, ShowingPlaceHolderText,is read-only and itspurpose 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.