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.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
     
    KWarner, Dec 9, 2009
    #1
    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.

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

    KWarner wrote:
    > 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
     
    Jay Freedman, Dec 9, 2009
    #2
    1. Advertisements

  3. KWarner

    Guest

    On Wednesday, 9 December 2009 18:42:08 UTC+2, Jay Freedman wrote:
    > 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.
    >
    > KWarner wrote:
    > > 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


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

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.
Similar Threads
  1. Dave

    XML Placeholder Text - Color

    Dave, Jul 22, 2004, in forum: Word VBA
    Replies:
    0
    Views:
    100
  2. Kaj Bromose
    Replies:
    1
    Views:
    134
    Cindy M.
    Aug 10, 2006
  3. Benjamino5
    Replies:
    1
    Views:
    146
    Cindy M.
    Apr 26, 2007
  4. Gregory K. Maxey

    Content Control text formatting

    Gregory K. Maxey, Jun 1, 2008, in forum: Word VBA
    Replies:
    3
    Views:
    346
    alborg
    Jun 8, 2008
  5. nostabo

    Insert text into a content control

    nostabo, Jan 9, 2009, in forum: Word VBA
    Replies:
    9
    Views:
    117
    nostabo
    Jan 12, 2009
  6. debc
    Replies:
    2
    Views:
    232
    Jay Freedman
    Jun 2, 2009
  7. Ailish
    Replies:
    2
    Views:
    290
    Ailish
    Sep 4, 2009
  8. Loulou
    Replies:
    1
    Views:
    226
    Jay Freedman
    Nov 6, 2009
Loading...