MS Word Interop - Hide content control placeholder text but retain the content control

2.7k Views Asked by At

I have a table with content controls. Once I've populated my table I remove all the content controls without removing the actual contents, except for the first line which I keep so that I can use it if I ever want to repopulate the table. The problem is sometimes there are rows in the first line without values, so that a grey text is shown. The result looking like this-

enter image description here

Is it possible to hide the content control (specifically the grey text) but not completely delete it, so that I could still use it if I later require? I tried everything from setting the color of the text to hiding the content control's font, but have not achieved the results I wish for.

1

There are 1 best solutions below

0
On BEST ANSWER

The technical term for grey text is "Placeholder text". Hhere are some basic approaches you can use:

  1. There's a built-in document style named "Placeholder text". You can set its Font.Hidden property to True so that, while you'll still see it on screen it won't print.

The code to do this:

ActiveDocument.Styles("Placeholder text").Font.Hidden = True
'To change it back
'ActiveDocument.Styles("Placeholder text").Font.Hidden = False
  1. You can also set the Placeholder text to a single space, which effectively hides the content control. It's still there, but you have to know it's there in order to target it (click in it).

Sample code:

Sub TogglePlaceholderText()
  Dim cc As word.ContentControl
  Dim sAltPlaceholder As String

  'Before running this macro make sure the placeholder
  'is written to the content control's Tag property
  sAltPlaceholder = " "
  Set cc = ActiveDocument.SelectContentControlsByTitle("CC_Test").Item(1)
  If cc.ShowingPlaceholderText And cc.Tag = cc.PlaceholderText Then
    cc.SetPlaceholderText Text:=sAltPlaceholder
  Else
    cc.SetPlaceholderText Text:=cc.Tag
  End If
End Sub
  1. Technically off-topic for SO, but you can also change the placeholder manually. In the Developer Tools tab, Controls group click "Design mode". Change the text between the "markers" being careful to NOT first delete what's there. This is so that you don't lose the "Placeholder text" style formatting so that the grey color is retained. (Note that it needs at least one character, but it can be a space). Click "Design mode" again.
  2. Similar to the macro code you can also change the definition of the "Placeholder text" style directly in the UI using the style management tools.