Content Control Placeholder Text formatting

Discussion in 'Word VBA' started by KWarner, Dec 9, 2009.

  1. KWarner

    KWarner Guest

    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

    KWarner, Dec 9, 2009
    1. Advertisements

  2. KWarner

    Jay Freedman Guest

    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
    Email cannot be acknowledged; please post all follow-ups to the newsgroup so
    all may benefit.
    Jay Freedman, Dec 9, 2009
    1. Advertisements

  3. KWarner


    Your answer saved me a lot of time, thanks man.
    , Apr 19, 2014
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.