Word VBA >> Content Control Placeholder Text formatting

by S1dhcm5lcg » Wed, 09 Dec 2009 23:30:01 GMT

Using VBA in Word 2007.
I'm trying to add a content control into a form. I can get the
formatting(Arial, 15) for the placeholder text set correctly unless the user
enters some text and then deletes that text so the placeholder text gets
re-shown. The placeholder text always goes back to the default formatting
(Calibri, 11).
This is a sample code that demonstrates this behavior. Can someone please
tell me where my thinking is wrong?
Sub test()
Dim text As String
text = "This is a test CC: "
Dim ccText As String
ccText = "enter the testCC text"
Dim cc As ContentControl

Selection.Range = text
Selection.Range.Font.Name = "Arial"
Selection.Range.Font.Size = 15
Selection.EndKey unit:=wdLine
Set cc = Selection.Range.ContentControls.Add(wdContentControlText)
cc.SetPlaceholderText text:=ccText
cc.Title = ""
Selection.MoveLeft unit:=wdCharacter, Count:=1
Selection.MoveRight unit:=wdWord, Count:=2, Extend:=wdExtend
Selection.Range.Font.Name = "Arial"
Selection.Range.Font.Size = 15
Selection.Range.Font.Bold = True
End Sub


Word VBA >> Content Control Placeholder Text formatting

by Jay Freedman » Thu, 10 Dec 2009 00:42:08 GMT

It's kind of clunky, but you could put a ContentControlOnExit event handler
in the ThisDocument module of the template to force the formatting after the
placeholder is redisplayed, like this:

Private Sub Document_ContentControlOnExit(ByVal cc As ContentControl, _
Cancel As Boolean)
If cc.ShowingPlaceholderText Then
With cc.Range.Font
.Name = "Arial"
.Size = 15
.Bold = True
End With
End If
End Sub

In the code you showed, it would also be a good idea to replace the
Selection stuff with similar direct manipulation of the .Range.Font.

Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Similar Threads

1. Content Control text formatting - Word VBA

2. content control loses track of data format

i have searched and asked this, thus I think it is a general problem: I have 
a customized content type in sharepoint and an infopath 2007 form in which I 
use this content type (column as money-type) for a document information 
When I now go and take the quick part with the money field in my word 2007 
document, the field loses all formatting which it has a) in sharepoint and b) 
in the infopath form. I cannot bring it back to the right money formatting. 
Example: Having '10,00 $' becomes a '10,000000000000'. 
It would be great if word 2007 would not lose the formatting of in this case 
best regards, 

3. Change Date Content Control to UK format - Word Document management

4. Conditional formatting using content controls

I'm investigating the potential to use Content Controls and bound custom XML 
for a server side document generation solution.

Is it possible to use the contents of a Content Control (i.e. data from the 
bound custom XML) to condition formatting?

e.g. how do I conditionally hide a paragraph if Content Control 'MyText' = 

5. Formatting Text in Rich Text Box Control - VB.Net

6. Displaying formatted text in a text box control.

"Parul" < XXXX@XXXXX.COM > wrote in message
news:161c01c3847f$b7b8cb00$ XXXX@XXXXX.COM ...
> There is some formatted text I need to display in a text
> box. It appears very unreadable when seen in a regular
> text box control. However when copied into a word document
> it is very readable.
> Any pointers on if I need to change a setting for the text
> box control. Will using the rich text box help fix this.

Yes, it sounds like you need to use a RichTextBox.  The standard textbox
doesn't support formatting.


7. Placeholder content check

8. Problem with insert mulitline in text content control.

In a userdefined dialogbox there are textboxs. Textbox with one linje
is working fine, but not with multilines.
I first clean-up the text in the mulitiline textbox and replace vbCr
with vbCrLf:

Dim objCC As ContentControls
Dim txt1 As String
Dim txt2 As String
Dim posTab As Integer

txt1 = Trim(.TextBoxADRESS.Text)
txt2 = ""
posTab = 1
Do While 0 < posTab
   posTab = InStr(1, txt1, vbCr)
   If 0 < posTab Then
      txt2 = txt2 & Left(txt1, posTab - 1) & vbCrLf
      txt1 = Mid(txt1, posTab + 2)
      txt2 = txt2 & txt1
   End If

And then the text is inserting in a text content control:

Set objCC = ActiveDocument.SelectContentControlsByTag("Adress")
objCC(1).Range.Text = txt2

The error is in the last line

What do I wrong?

(Thanks for the answer)