简体   繁体   中英

VBA to Add Multiple Content Controls to a Single Table Cell in Word

I need to add multiple content controls and additional text into a single table cell in word using VBA. Here is an example of what I need:

<CC1>Moby Dick</CC1> has been read by <CC2>2</CC2> people who have given it an average score of <CC3>3</CC3> out of 5

I know I can add a single content controls with the following syntax:

With rng.ContentControls.Add(wdContentControlRichText)
 .title = "Book Name"
 .Tag = "title"
End With

If my range is set to the cell, how would I make the following pseudo code work:

Set rng = objDoc.Tables(1).Cell(2, 1).Range

With rng.ContentControls.Add(wdContentControlRichText)
 .title = "Book Name"
 .Tag = "title"
End With

" has been read by "

With rng.ContentControls.Add(wdContentControlRichText)
 .title = "People Count"
 .Tag = "count"
End With

" people who have given it an average score of "

With rng.ContentControls.Add(wdContentControlRichText)
 .title = "Score"
 .Tag = "score"
End With

" out of 5"

Here is some actual code. It is putting the third insertafter and content control between the Second insertafter and the second content control

With rng.ContentControls.Add(wdContentControlRichText)
    .title = "Asset ID"
    .Tag = "asset_id"
End With
rng.InsertAfter " | Rev. "
rng.Collapse Direction:=wdCollapseEnd
rng.Move wdCharacter, -1
With rng.ContentControls.Add(wdContentControlRichText)
    .title = "Revison Number"
    .Tag = "revision_num"
End With
rng.InsertAfter " | Effective Date: "
rng.Collapse Direction:=wdCollapseEnd
rng.Move wdCharacter, -1
With rng.ContentControls.Add(wdContentControlRichText)
    .title = "Effective Date"
    .Tag = "effective_date"
End With

在此处输入图像描述

Try this:

   Set rng = objDoc.Tables(1).Cell(2, 1).Range
   rng.Collapse wdCollapseStart
   With rng.ContentControls.Add(wdContentControlRichText)
      .Title = "Effective Date"
      .Tag = "effective_date"
      .SetPlaceholderText Text:="Effective date"
   End With
   rng.Text = " | Effective Date: "
   rng.Collapse wdCollapseStart
   With rng.ContentControls.Add(wdContentControlRichText)
      .Title = "Revison Number"
      .Tag = "revision_num"
      .SetPlaceholderText Text:="Rev Num"
   End With
   rng.Text = " | Rev. "
   rng.Collapse wdCollapseStart
   With rng.ContentControls.Add(wdContentControlRichText)
      .Title = "Asset ID"
      .Tag = "asset_id"
      .SetPlaceholderText Text:="Asset ID"
   End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM