Share via

Put text after a content control in the same cell (Word table)

Anonymous
2019-02-23T10:40:13+00:00

I have this cell in a Word table which contains a content control (which contains some text). I would like select the cell, put the text insertion point *after* the content control item, and add some text into the cell (leaving the content control item and the text it contains unchanged). I cannot figure out a way to have VBA put the text insertion point after the content control object in the cell Thanks for your help!

Ste

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

8 answers

Sort by: Most helpful
  1. Anonymous
    2019-02-23T12:30:23+00:00

    Manually?

    Put the cursor in the content control and use the right arrow key to move out of it. That will be indicated by the title where present disappearing. Add the text.

    By macro?

    Dim oCC As ContentControl

    Dim oRng As Range

        Set oCC = Selection.Cells(1).Range.ContentControls(1)

        Set oRng = oCC.Range

        oRng.End = oRng.End + 1

        oRng.Collapse 0

        oRng.Select

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2019-02-24T05:04:38+00:00

    Try using the code I actually posted and replace

    oRng.Select

    with

    oRng.Text = "text after content control"

    If you are running the code from another application then change

    Dim oCC As ContentControl

    Dim oRng As Range

    to

    Dim oCC As Object

    Dim oRng As Object

    0 comments No comments
  3. Anonymous
    2019-02-23T19:19:46+00:00

    Thank you for your reply

    I used:

    owrd.ActiveDocument.ContentControls(x).Range.Text = "text1" 

    Selection.Cells(1).Range.Select

    Selection.End = Selection.End - 1

    Selection.Collapse wdCollapseEnd

    owrd.Selection.Range.Text = "text2"

    but this puts  the string "text2" inside the content control (in other words, "text2" gets prepended to "text1", inside the content control (where the string "text2 text1" is thus placed, whereas "text2" should be put outside of the content control and after "text1".  ?

    Thanks a lot

    Ste

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 322.9K Reputation points MVP Volunteer Moderator
    2019-02-23T19:03:34+00:00

    Use

    Selection.Cells(1).Range.Select

    Selection.End = Selection.End - 1

    Selection.Collapse wdCollapseEnd

    0 comments No comments
  5. Anonymous
    2019-02-23T17:41:14+00:00

    Hi

    Thanks a lot for your reply.

    I am trying to do that with a vbs script (not manually). Here is section of code I wrote using the example you provided:

    .....

    owrd.ActiveDocument.Tables(7).Rows(y).Cells(1).Select 'this selects the cell that contains the content control

    owrd.ActiveDocument.ContentControls(x).Range.Text = "text inside content control" 'this works!

    owrd.Selection.Range.End + 1

    owrd.Selection.Range.Collapse 0

    owrd.Selection.Range.Select

    owrd.Selection.Range.Text = "text after content control" 

    The code I wrote puts the string "text after content control"  at the beginning of the text *inside* the content control whereas I need it to be put *outside* of the content control and after the text it contains, but still inside the selected cell.

    Thank you so much for your help on this!

    Ste

    0 comments No comments