Content Control Placeholder Text formatting


K

KWarner

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.WholeStory
Selection.Range.Font.Name = "Arial"
Selection.Range.Font.Size = 15
Selection.Collapse
Selection.EndKey unit:=wdLine
Set cc = Selection.Range.ContentControls.Add(wdContentControlText)
cc.SetPlaceholderText text:=ccText
cc.Title = ""
cc.Range.Select
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

Thanks,
Kevin
 
Ad

Advertisements

J

Jay Freedman

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.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Ad

Advertisements

I

iulian1189

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.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Your answer saved me a lot of time, thanks man.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top