Daniel
Daniel

Reputation: 1

Using Content Controls to select/insert text into a Word document

I'm trying to create a fluid document which will populate text based on selections from drop down lists and checkboxes.

E.g., if I check this box, then a section of text will be inserted into the document.

I've done a lot of searching online, and the closest I've come is using https://www.msofficeforums.com/word-vba/16498-multiple-entries-dropdown-lists.html#post46903

Unfortunately, I do not know VBA (or indeed any other programming language at all). I am looking for a solution which allows me to paste in a VBA solution like the one provided above:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim i As Long, StrDetails As String
With ContentControl
  If .Title = "Client" Then
  For i = 1 To .DropdownListEntries.Count
    If .DropdownListEntries(i).Text = .Range.Text Then
      StrDetails = Replace(.DropdownListEntries(i).Value, "|", Chr(11))
      Exit For
    End If
  Next
  ActiveDocument.ContentControls(2).Range.Text = StrDetails
  End If
End With
End Sub

But which will allow me to call each listbox/checkbox by its name, as figuring out which field has which ID is making me tear my hair out.

Basically, I want the above code, but instead of calling each Content Control by its ID number, I want to call it by its name, so that the listbox/checkbox selections will auto-populate different text into the document.

I am using Microsoft Home and Business Office 2016.

Thank you all in advance for your help!

Upvotes: 0

Views: 1335

Answers (1)

Timothy Rylatt
Timothy Rylatt

Reputation: 7850

For example:

ActiveDocument.SelectContentControlsByTitle(title).Items(1).Range.Text = StrDetails

Or

ActiveDocument.SelectContentControlsByTitle(title)(1).Range.Text = StrDetails

Upvotes: 1

Related Questions