vbams-wordcopycontentcontrol

Copy a ContentControl from one table cell to another table cell in the same table in Word using VBA


I am trying to copy a ContentControl from a cell in a word table to another cell in the same word table. I know some Excel VBA, but very little Word VBA. The code below is supposed to copy the ContentControl that is in Cell 2,4 to Cell 44,4 in the same table. The code below gives the following error "Run-time error '5941', The requested member of the collection does not exist" on the last line.

Any help would be appreciated.

Sub CopyContentControl()
Dim OCC As ContentControl
Set OCC = ActiveDocument.Tables(1).Cell(2, 4).Range.ContentControls(1)
ActiveDocument.Tables(1).Cell(44, 4).Range.ContentControls(1) = OCC
End Sub

Solution

  • To replicate the entire cell, you could use:

    Sub CopyContentControl()
    Dim Rng As Range
    With ActiveDocument.Tables(1)
      Set Rng = .Cell(2, 4).Range: Rng.End = Rng.End - 1
      .Cell(44, 4).Range.FormattedText = Rng.FormattedText
    End With
    End Sub
    

    If all you want is the content control (e.g. not other cell content), you could use:

    Sub CopyContentControl()
    With ActiveDocument.Tables(1)
      .Cell(44, 4).Range.FormattedText = .Cell(2, 4).Range.ContentControls(1).Range.FormattedText
    End With
    End Sub