Word VBA - How to locate table cell containing Content Control?

你离开我真会死。 提交于 2019-12-25 07:04:23

问题


Objective

I have a table in Word with 2 columns. The first has a drop down Content Control.

I would like to populate the second column with text, the value of which depends on the option selected.

Question

Is it possible to get the cell reference that contains the clicked content control? I plan to use that to target the next column with the content. I'm thinking of something like this:

    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

回答1:


I worked it out - here's how I did it:

    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


来源:https://stackoverflow.com/questions/25314641/word-vba-how-to-locate-table-cell-containing-content-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!