2016-11-05 1 views
0

다음 코드는 양식 필드의 체크 된 확인란의 수를 계산하는 데 사용됩니다. 양식 필드가 아닌 일반 테이블에서 같은 개체를 계산하도록 어떻게 수정합니까?이 코드를 수정하여 일반 Microsoft Word 테이블에서 같은 개체를 계산할 수있는 방법

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