Chester S.
Chester S.

Reputation: 83

Referencing Text Content Controls

I am trying to reference and paste a specified string into a specific Text Content Controls and have been unable to do this properly.

Basically I have gone through and tried a few different things, first being this;

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

Dim ccs As ContentControls, cc As ContentControl
Set ccs = ActiveDocument.ContentControls
For Each cc In ccs
  If cc.Title = "ComboBox1" And cc.Range.Text = "Choose an item." Then
  SelectContentControlsByTitle("TextBox1").Add.SetPlaceholderText , , "Please make a drop down selection or manually fill out if not applicable"

The above does not work, as every time I exit my content controller combo box it actually recreates the "placeholdertext" multiple times. I need this to only fill the "TextBox1" Content control.

I have also tried doing something like this,

Dim ccs As ContentControls, cc As ContentControl
Set ccs = ActiveDocument.ContentControls
Set CB1 = SelectContentControlsByTitle("TextBox1")
For Each cc In ccs
  If cc.Title = "ComboBox1" And cc.Range.Text = "Choose an item." Then
  CB1.Value = "Please make a drop down selection or manually fill out if not applicable"

Due to the type, value is not able to be used like this. This does not work either.
Below is the original way I was doing what I wanted with the Active-X TextBox which does work;

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

Dim ccs As ContentControls, cc As ContentControl
Set ccs = ActiveDocument.ContentControls
For Each cc In ccs
  If cc.Title = "ComboBox2" And cc.Range.Text = "Choose an item." Then
  TextBox2.Value = "Please make a drop down selection or manually fill out if not applicable"

  ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "TMS backup" Then
  TextBox2.Value = "The TMS installation directory, settings directory and the database was backed up before the update was performed"

  ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "TMS installation" Then
  TextBox2.Value = "Installation of version 1.17.X.19XXX performed"

  ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "TMS update" Then
  TextBox2.Value = "Update from version 1.16.X.XXXXX to version 1.17.X.19XXX performed"

  ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "Tool presetter update" Then
  TextBox2.Value = "Update from version 1.16.X.XXXXX to version 1.17.X.19XXX performed"

  ElseIf cc.Title = "ComboBox2" And cc.Range.Text = "Database generated" Then
  TextBox2.Value = "Database structure created with version 1.17.0"

How can I do the above, while using the Text Content Controls?

Upvotes: 0

Views: 454

Answers (2)

macropod
macropod

Reputation: 13490

A better approach would be:

Private Sub Document_ContentControlOnExit(ByVal Ctrl As ContentControl, Cancel As Boolean)
Dim i As Long, StrDetails As String
With Ctrl
  If .Title = "ComboBox1" Then
  If ShowingPlaceholderText = True Then
    StrDetails = ""
  Else
    For i = 1 To .DropdownListEntries.Count
      If .DropdownListEntries(i).Text = .Range.Text Then
        StrDetails = .DropdownListEntries(i).Value
        Exit For
      End If
    Next
  End If
  ActiveDocument.SelectContentControlsByTitle("TextBox1")(1).Range.Text = StrDetails
  End If
End With
End Sub

To make this work, simply add your 'conditional' text to each entry's 'Value' property, and make the "TextBox1" content control's placeholder text whatever you want its prompt to be. This way, you don't have to hard-code either the dropdown option or the 'conditional' text in your VBA code. For a practical demonstration, see: https://www.msofficeforums.com/word-vba/16498-multiple-entries-dropdown-lists.html#post46903

PS: You really should get away from the default ActiveX naming conventions and give your content controls meaningful titles.

Upvotes: 1

John Korchok
John Korchok

Reputation: 4913

Try this for setting the text box text:

Sub SetContentcontrolText()
    Dim oRange As Range
    Dim cc As ContentControl
    For Each cc In ActiveDocument.ContentControls
        If cc.Title = "ComboBox1" And cc.Range.Text = "Choose an item." Then
            Set oRange = ActiveDocument.SelectContentControlsByTitle("TextBox1")(1).Range
            oRange.Text = "Please make a drop down selection or manually fill out if not applicable"
        End If
    Next
End Sub

Upvotes: 1

Related Questions