Word VBA - 如何定位包含内容控制的表格单元格?

问题描述 投票:0回答:2

目的

我在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
vba word-vba
2个回答
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

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


© www.soinside.com 2019 - 2024. All rights reserved.