Word VBA - 如何查找包含Content Control的表格单元格?

时间:2014-08-14 18:02:51

标签: vba word-vba

目标

我在Word中有一个包含2列的表。第一个是下拉内容控制。

我想用文本填充第二列,其值取决于所选的选项。

问题

是否可以获取包含点击内容控件的单元格引用?我计划使用它来定位包含内容的下一列。我想到这样的事情:

    Dim oCell As Cell
    oCel = 'some way to get cell reference containing the ContentControl here
    Dim curCellRow, curCellCol, targetCellRow, targetCellCol As Integer
    curCellRow = oCell.Row
    curCellCol = oCell.Column
    targetCellRow = curCellRow
    targetCellCol = curCellCol + 1

    Dim NewCellContents As String
    NewCellContents = "Sample Content for this cell"

    ActiveDocument.Tables(1).Cell(targetCellRow, targetCellCol).Range.Text = NewCellContents

2 个答案:

答案 0 :(得分:0)

我把它解决了 - 这就是我如何做到的:

    Dim RowNum As Long, ColNum As Long
    If Selection.Information(wdWithInTable) Then
    RowNum = Selection.Cells(1).RowIndex
    ColNum = Selection.Cells(1).ColumnIndex
    MsgBox "Row = " & RowNum & vbCr & _
    "Column = " & ColNum
    Else
    MsgBox "Not in table"
    End If

答案 1 :(得分:0)

我也有类似的需求,并在ThisDocument的ContentControlOnExit事件中使用了以下代码。

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim TableNum As Long, RowNum As Long, ColNum As Long
If ContentControl.Range.Information(wdWithInTable) Then
    TableNum = Me.Range(0, ContentControl.Range.End).Tables.Count
    RowNum = ContentControl.Range.Information(wdStartOfRangeRowNumber)
    ColNum = ContentControl.Range.Information(wdStartOfRangeColumnNumber)
End If
' more code
End Sub