[英]Replacing checkboxes in MS-Word
I have a document with a large number of checkboxes spread around the text and I would like to replace all checkboxes with characters.我有一个文档,其中包含大量散布在文本周围的复选框,我想用字符替换所有复选框。
Example:例子:
If checkbox is checked then replace it with "A"
If checkbox is not Checked then replace it with "O"
For the time being I can only replace all checkboxes with a letter regardless of their state (checked or unchecked).目前我只能用一个字母替换所有复选框,不管它们的 state(选中或未选中)。 I need to improve my macro so it recognizes the state of the checkbox and replacing it with the right litteral.
我需要改进我的宏,以便它识别复选框的 state 并将其替换为正确的文字。
Thanks in advance提前致谢
Sub Checkbox_Replacement()
Dim i As Long, Rng As Range
With ActiveDocument
For i = .FormFields.Count To 1 Step -1
With .FormFields(i)
If .Type = wdFieldFormCheckBox Then
Set Rng = .Range
.Delete
Rng.Text = "A"
End If
End With
Next
Set Rng = Nothing
End With
End Sub
Expected Result预期结果
If checkbox is checked then replace it with "A"
If checkbox is not Checked then replace it with "O"
Actual Result实际结果
All checkboxes are replaced with "A"
You need a second If..Else
block to check the condition of the checkbox:您需要第二个
If..Else
块来检查复选框的条件:
If .Type = wdFieldFormCheckBox Then
Set Rng = .Range
If .CheckBox.Value = True Then
Rng.Text = "A"
Else
Rng.Text = "O"
End If
.Delete
End If
The answer to the problem was to remove the.Delete问题的答案是删除.Delete
Sub Checkbox_Replacement()
Dim i As Long, Rng As Range
With ActiveDocument
For i = .FormFields.Count To 1 Step -1
With .FormFields(i)
If .Type = wdFieldFormCheckBox Then
Set Rng = .Range
If .CheckBox.Value = True Then
Rng.Text = "A"
Else
Rng.Text = "O"
End If
End If
End With
Next
Set Rng = Nothing
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.