none
How do I insert a tab between ContentControls? RRS feed

  • Question

  • I'm trying to write a Word add-in that will programmatically add some ContentControls to the document at run time. However, I am having some trouble trying to insert two ContentControls on the same line that are separated by a tab. I am using the following code:

    PlainTextContentControl idcontrol 
        = myExtDoc.Controls.AddPlainTextContentControl(tc.Id + "_id");
    idcontrol.Text = "id";
    myWord.Selection.MoveRight(Word.WdUnits.wdCharacter, idcontrol.Text.Count() + 1);
                            
    myWord.Selection.TypeText("\t");
                            
    PlainTextContentControl titlecontrol 
        = myExtDoc.Controls.AddPlainTextContentControl(tc.Id + "_title");
    titlecontrol.Text = "title";
    myWord.Selection.MoveRight(Word.WdUnits.wdCharacter, titlecontrol.Text.Count()+1);

    The two ContentControls get added, but for some reason the tab never gets typed so the ContentControls appear right next to each other. I tried using VBA macro recording to do the same thing, but the tab still gets ignored when I playback the macro:

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
        Selection.Range.ContentControls.Add (wdContentControlText)
        Selection.TypeText Text:="id"
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.TypeText Text:=vbTab
        Selection.Range.ContentControls.Add (wdContentControlText)
        Selection.TypeText Text:="title"
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.TypeParagraph
    End Sub

    Can anybody help?

    Thanks,

    -Paul

    Sunday, September 9, 2012 12:13 AM

Answers

  • Hi Paul

    When working with Selection and Range there is always more than one  way to reach a goal. For your requirement, my personal preference would be:

    Sub AddTabAfterCC()
        Dim cc As word.ContentControl
        Dim Rng As word.Range
        
        Set cc = ActiveDocument.Contentcontrols.Add
        Set Rng = cc.Range
        Rng.Text = "text in the content control"
        Rng.Collapse wdCollapseEnd
        Rng.MoveStart wdCharacter, 1
        Rng.Text = vbTab ' " after the content control..."
        Rng.Collapse wdCollapseEnd
        Set cc = Rng.Contentcontrols.Add
        cc.Range.Text = "text in second content control"
    End Sub



    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, September 9, 2012 2:58 PM
    Moderator

All replies

  • Hi Paul

    When working with Selection and Range there is always more than one  way to reach a goal. For your requirement, my personal preference would be:

    Sub AddTabAfterCC()
        Dim cc As word.ContentControl
        Dim Rng As word.Range
        
        Set cc = ActiveDocument.Contentcontrols.Add
        Set Rng = cc.Range
        Rng.Text = "text in the content control"
        Rng.Collapse wdCollapseEnd
        Rng.MoveStart wdCharacter, 1
        Rng.Text = vbTab ' " after the content control..."
        Rng.Collapse wdCollapseEnd
        Set cc = Rng.Contentcontrols.Add
        cc.Range.Text = "text in second content control"
    End Sub



    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, September 9, 2012 2:58 PM
    Moderator
  • Hi Paul,

    Thanks for posting in the MSDN Forum.

    I will mark Cindy's reply as answer. Please feel free unmark it if you don't think so.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 17, 2012 7:27 AM
    Moderator