Reputation: 547
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
Upvotes: 0
Views: 642
Reputation: 7860
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
Upvotes: 1