0

I am trying to write Word vba code that will insert a variety of different tables with multiple content controls when a selection is made from a drop down content control. I have tried writing the code several ways, but every time I tab to exit the drop down content control for the next content control, I get the error "Run-time error '4605': This method or property is not available because the current selection partially covers a plain text content control." The only time that I do not get this error is when I use the cursor to click away and do not click on a content control, then the code runs perfectly and generates the table with all the content controls. Below is a code that 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. Thank you for any input!

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 have tried to generate the tables via using multiple If statements, Select Case statements, and referencing new sub statements. I just cannot seem to get this to run properly.

Charles Kenyon
  • 869
  • 1
  • 8
  • 19
  • Have you considered using a Building Block Gallery Content Control instead? You are likely to get what you need without writing a single line of code. – Timothy Rylatt Jun 06 '23 at 12:50
  • That is a great suggestion! I was unaware of the usefulness of Building Block Gallery Content Controls. Unfortunately, I want to protect the Document to limit editing to just content controls. In my testing in a blank document, I could not use the Building Block Gallery Content Control when editing was limited to filling in forms. – Colin Rojahn Jun 06 '23 at 14:10
  • 2
    Please use the edit link below the question to add that extra information to your question. It is vital that anyone attempting to answer your question has all relevant information. – Timothy Rylatt Jun 06 '23 at 15:30
  • 2
    Forms protection is for use of legacy form fields. When you use content controls, you should use content control _Groups_ or _Read-only protection with exceptions_ instead of Forms protection. – John Korchok Jun 06 '23 at 17:22
  • Again, do not use filling in forms protection with Content Controls. You could also save your table, with the Content Controls as a Quick Table in your template. https://www.addbalance.com/usersguide/tables.htm#QuickTables Building blocks can be inserted using vba, as well. https://www.addbalance.com/usersguide/autotextautocorrect.htm#UsingVBABuildingBlock – Charles Kenyon Jun 07 '23 at 17:02

1 Answers1

1

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

Oscar Sun
  • 1,427
  • 2
  • 8
  • 13
  • Use of `Selection` is best avoided. In most situations, including this one, it is unnecessary. – Timothy Rylatt Jun 06 '23 at 17:39
  • @TimothyRylatt NO, you are wrong,In this situation, if you without the selection setting, you will encounter the error that questioner ask. You can try his code. This is because the focus, the Insertion position, the cursor is still in the content control. – Oscar Sun Jun 06 '23 at 18:08
  • @TimothyRylatt Sometimes like using shape copy in C sharp to control the Microsoft Word. if you use the Range copy Instead of Selection copy, you will encounter an error either. – Oscar Sun Jun 06 '23 at 18:14
  • 1
    You are incorrect. It is completely unnecessary to use `Selection` in this case. I suggest you look at the [documentation](https://learn.microsoft.com/en-us/office/vba/api/word.contentcontrols.add). All that is required is to specify the range where the control is to be inserted. Yes, there *are* certain circumstances where it is necessary to use `Selection`, but they are rare. – Timothy Rylatt Jun 06 '23 at 18:49
  • @TimothyRylatt Did you run his code to test? If you are right, how would you explain the exception? And how would you explain my correction working? The documentations just for reference. AS they also were written by people and perhaps made mistakes too. I've been corrected a little bit of them. Anyway, thank you for you provide the document for me to refer. – Oscar Sun Jun 07 '23 at 01:05
  • @TimothyRylatt *If the insertion point or current selection is inside a content control of a different type, this method raises an error. In this case, you can either move the insertion point or use the Range parameter to specify a location within the document.* Yes answer is just in The documentation you provide. Thanks a lot! So in this case,Moving insertion point is necessary though. Or we must rewrite the code in the other way. – Oscar Sun Jun 07 '23 at 01:18
  • @TimothyRylatt And you are right, I agree with you that If there can be used by Range object,then we Should be used them not use the Selection object. see my post here – Oscar Sun Jun 07 '23 at 01:24
  • This answer solutions is when I read the error message, It includes the selection keyword to make me got a flash thought to write in this way to take a shot, and it made it! – Oscar Sun Jun 07 '23 at 01:31
  • 1
    Thank you very much! Adding .Select worked perfectly! – Colin Rojahn Jun 07 '23 at 14:54