2016-11-05 2 views
0

以下のコードは、フォームフィールドのチェックボックスの数を数えます。フォームフィールドではなく、通常のテーブル内の同じオブジェクトをカウントするにはどうすれば変更できますか?通常のマイクロソフトワードテーブルで同じオブジェクトを数えるためにこのコードを変更するには

Private Sub CommandButton2_Click() 
Dim i As Long, j As Long, k As Long 
k = 0 
With ActiveDocument 
    With .Tables(1) 
     j = 3 
     For i = 1 To .Rows.Count 
      If .Cell(i, j).Range.FormFields(1).CheckBox.Value = True Then 
       k = k + 1 
      End If 
     Next i 
     i = .Rows.Count 
    End With 
End With 
    MsgBox k & " instances were found" 
End Sub 

答えて

1

はここにあるチェックボックスをカウントしたり、テーブルのチェックボックスにチェックする方法の例をいくつかだ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 
+0

これは本当に素晴らしいです!どうもありがとう! – Snoopy

+0

ようこそ。私の答えを受け入れたものとしてマークすることができます。ありがとうございました – user3598756

関連する問題