はここにあるチェックボックスをカウントしたり、テーブルのチェックボックスにチェックする方法の例をいくつかだContentControls
オブジェクトを返し、それを
を利用するためにRange
オブジェクトのContentControls
プロパティを使用するか、またはそれの単一の列に:
Option Explicit
Sub main()
With ActiveDocument
MsgBox CountCheckBoxes(.Tables(1)) & " CheckBox instances were found"
MsgBox CountCheckedCheckBoxes(.Tables(1)) & " checked CheckBox instances were found"
MsgBox CountCheckBoxesInColumn(.Tables(1), 1) & " CheckBox instances were found in column 1"
MsgBox CountCheckedCheckBoxesInColumn(.Tables(1), 1) & " checked CheckBox instances were found in column 1"
End With
End Sub
Private Function CountCheckBoxes(table As table, Optional col As Variant) As Long
Dim cc As ContentControl
With table
For Each cc In .Range.ContentControls
If cc.Type = wdContentControlCheckBox Then CountCheckBoxes = CountCheckBoxes + 1
Next cc
End With
End Function
Private Function CountCheckedCheckBoxes(table As table) As Long
Dim cc As ContentControl
With table
For Each cc In .Range.ContentControls
If cc.Type = wdContentControlCheckBox Then If cc.Checked Then CountCheckedCheckBoxes = CountCheckedCheckBoxes + 1
Next cc
End With
End Function
Private Function CountCheckBoxesInColumn(table As table, col As Long) As Long
Dim i As Long
With table
For i = 1 To .Rows.count
CountCheckBoxesInColumn = CountCheckBoxesInColumn + .Cell(i, col).Range.ContentControls.count
Next i
End With
End Function
Private Function CountCheckedCheckBoxesInColumn(table As table, col As Long) As Long
Dim i As Long
With table
For i = 1 To .Rows.count
CountCheckedCheckBoxesInColumn = CountCheckedCheckBoxesInColumn + CountCheckBoxesCheked(.Cell(i, col).Range)
Next i
End With
End Function
Function CountCheckBoxesCheked(rng As Range) As Long
Dim cc As ContentControl
With rng
For Each cc In .ContentControls
If cc.Type = wdContentControlCheckBox Then If cc.Checked Then CountCheckBoxesCheked = CountCheckBoxesCheked + 1
Next cc
End With
End Function
これは本当に素晴らしいです!どうもありがとう! – Snoopy
ようこそ。私の答えを受け入れたものとしてマークすることができます。ありがとうございました – user3598756