I am trying to insert a variety of different tables with multiple content controls when a selection is made from a drop down content control.
I tried several ways, but every time I tab to exit the drop down content control for the next content control, I get
Run-time error '4605': This method or property is not available because the current selection partially covers a plain text content control.
I do not get this error when I use the cursor to click away and not click on a content control, then the code generates the table with all the content controls.
Code I have been using to test different ideas. I plan on protecting this document after the code has been completed to keep people from modifying the form.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If ContentControl.Tag = "ReturnType" Then
Dim tbl As Table
Dim rng As Range
Select Case ContentControl.Range.Text
Case "Selection 1"
Set rng = ActiveDocument.Content.Paragraphs(3).Range
Set tbl = rng.Tables.Add(rng, 3, 3)
tbl.cell(1, 2).Range.ContentControls.Add wdContentControlCheckBox
Case "Selection 2"
Set rng = ActiveDocument.Content.Paragraphs(3).Range
Set tbl = rng.Tables.Add(rng, 2, 2)
Case "Selection 3"
Set rng = ActiveDocument.Content.Paragraphs(3).Range
Set tbl = rng.Tables.Add(rng, 1, 1)
End Select
End If
End Sub
I tried to generate the tables using multiple If statements, Select Case statements, and referencing new sub statements.
Just reset the Selection to where you want to, before you try to add a ContentControl, you will be fine.
tbl.cell(1, 2).Range.Select
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If ContentControl.Tag = "ReturnType" Then
Dim tbl As Table
Dim rng As Range
Select Case ContentControl.Range.Text
Case "Selection 1"
Set rng = ActiveDocument.Content.Paragraphs(3).Range
Set tbl = rng.Tables.Add(rng, 3, 3)
tbl.cell(1, 2).Range.Select
tbl.cell(1, 2).Range.ContentControls.Add wdContentControlCheckBox
Case "Selection 2"
Set rng = ActiveDocument.Content.Paragraphs(3).Range
Set tbl = rng.Tables.Add(rng, 2, 2)
Case "Selection 3"
Set rng = ActiveDocument.Content.Paragraphs(3).Range
Set tbl = rng.Tables.Add(rng, 1, 1)
End Select
End If
End Sub