循环遍历不同表中的内容控件复选框
Loop trought Content Controls Checkbox inside different tables
我有包含多个表格的文档(如附图所示),所以我尝试仅验证状态在列 "complete" 中的字段,那些未被选中的字段 (False)在其中工作并继续一项一项地审查。到目前为止,我的主要问题是从这些单元格上的那些特定内容控件中检索值,因为我还没有找到 method/function 来执行此操作。
所以我的 VBA 代码到目前为止(错误的顺便说一句)是这样的,如何在选定的单元格内检索内容控件的值的任何范围...(我感觉像 inception jk 元素在一个元素内部另一个元素内部,我们需要更深入...)
Public Sub VerifyCheckBox()
Application.ScreenUpdating = False
Dim lastCC As Long, totalRows As Long, nRow As Long
Dim test1 As String
Dim nTbl As Table
Dim thisCell As Range
'lastCC = ActiveDocument.Content.ContentControls.Count
'test1 = ActiveDocument.Content.ContentControls(1).Type = wdContentControlCheckBox
'MsgBox "Found " & lastCC & " Content Controls " & " First checkbox... " & test1
For Each nTbl In ActiveDocument.Tables 'Loop Trough the tables in the document
If nTbl.Cell(nRow, 3).Range = "Complete" Then
totalRows = nTbl.Rows.Count 'Total number of rows in the selected table
For nRow = 3 To totalRows
If nTbl.Cell(nRow, 3).Content.ContentControl.Type = wdContentControlCheckBox And nTbl.Cell(nRow, 3).Content.ContentControl.Checked = False Then
thisCell = nTbl.Cell(nRow, 3).Range.Select
MsgBox "Review element: " & nTbl.Cell(nRow, 1).Range & nTbl.Cell(nRow, 2).Range
End If
Next nRow
End If
'Debug.Print nTbl.Columns.Count & " " & nTbl.Rows.Count
Next
Application.ScreenUpdating = True
End Sub
非常感谢您的回答
Zegad,你非常接近。下面您会发现您的代码得到了改进,包括一些评论和解释。
Public Sub VerifyCheckBox()
Application.ScreenUpdating = False
Dim lastCC As Long, totalRows As Long, nRow As Long
Dim test1 As String
Dim nTbl As Table
Dim thisCell As Range
For Each nTbl In ActiveDocument.Tables 'Loop Trough the tables in the document
totalRows = nTbl.Rows.Count 'Total number of rows in the selected table
'why do you start as of four if your headers are 2nd and data start as of 3rd?
For nRow = 3 To totalRows
'you don't need this if you have 'complete' column as a third one
'you would need something similar if 'complete' could be in different column
'If nTbl.Cell(nRow, 3).Range = "Complete" Then
'NEW! let's check first if there is any ContentControl in cell
If nTbl.Cell(nRow, 3).Range.ContentControls.Count > 0 Then
'I assume that you have only one ContentControl per cell
If nTbl.Cell(nRow, 3).Range.ContentControls(1).Type = wdContentControlCheckBox _
And nTbl.Cell(nRow, 3).Range.ContentControls(1).Checked = False Then
'if you want to select do it in this way
Set thisCell = nTbl.Cell(nRow, 3).Range
thisCell.Select
MsgBox "Review element: " & nTbl.Cell(nRow, 1).Range & nTbl.Cell(nRow, 2).Range
End If
End If
'End If
Next nRow
Next
Application.ScreenUpdating = True
End Sub
我有包含多个表格的文档(如附图所示),所以我尝试仅验证状态在列 "complete" 中的字段,那些未被选中的字段 (False)在其中工作并继续一项一项地审查。到目前为止,我的主要问题是从这些单元格上的那些特定内容控件中检索值,因为我还没有找到 method/function 来执行此操作。
所以我的 VBA 代码到目前为止(错误的顺便说一句)是这样的,如何在选定的单元格内检索内容控件的值的任何范围...(我感觉像 inception jk 元素在一个元素内部另一个元素内部,我们需要更深入...)
Public Sub VerifyCheckBox()
Application.ScreenUpdating = False
Dim lastCC As Long, totalRows As Long, nRow As Long
Dim test1 As String
Dim nTbl As Table
Dim thisCell As Range
'lastCC = ActiveDocument.Content.ContentControls.Count
'test1 = ActiveDocument.Content.ContentControls(1).Type = wdContentControlCheckBox
'MsgBox "Found " & lastCC & " Content Controls " & " First checkbox... " & test1
For Each nTbl In ActiveDocument.Tables 'Loop Trough the tables in the document
If nTbl.Cell(nRow, 3).Range = "Complete" Then
totalRows = nTbl.Rows.Count 'Total number of rows in the selected table
For nRow = 3 To totalRows
If nTbl.Cell(nRow, 3).Content.ContentControl.Type = wdContentControlCheckBox And nTbl.Cell(nRow, 3).Content.ContentControl.Checked = False Then
thisCell = nTbl.Cell(nRow, 3).Range.Select
MsgBox "Review element: " & nTbl.Cell(nRow, 1).Range & nTbl.Cell(nRow, 2).Range
End If
Next nRow
End If
'Debug.Print nTbl.Columns.Count & " " & nTbl.Rows.Count
Next
Application.ScreenUpdating = True
End Sub
非常感谢您的回答
Zegad,你非常接近。下面您会发现您的代码得到了改进,包括一些评论和解释。
Public Sub VerifyCheckBox()
Application.ScreenUpdating = False
Dim lastCC As Long, totalRows As Long, nRow As Long
Dim test1 As String
Dim nTbl As Table
Dim thisCell As Range
For Each nTbl In ActiveDocument.Tables 'Loop Trough the tables in the document
totalRows = nTbl.Rows.Count 'Total number of rows in the selected table
'why do you start as of four if your headers are 2nd and data start as of 3rd?
For nRow = 3 To totalRows
'you don't need this if you have 'complete' column as a third one
'you would need something similar if 'complete' could be in different column
'If nTbl.Cell(nRow, 3).Range = "Complete" Then
'NEW! let's check first if there is any ContentControl in cell
If nTbl.Cell(nRow, 3).Range.ContentControls.Count > 0 Then
'I assume that you have only one ContentControl per cell
If nTbl.Cell(nRow, 3).Range.ContentControls(1).Type = wdContentControlCheckBox _
And nTbl.Cell(nRow, 3).Range.ContentControls(1).Checked = False Then
'if you want to select do it in this way
Set thisCell = nTbl.Cell(nRow, 3).Range
thisCell.Select
MsgBox "Review element: " & nTbl.Cell(nRow, 1).Range & nTbl.Cell(nRow, 2).Range
End If
End If
'End If
Next nRow
Next
Application.ScreenUpdating = True
End Sub