VBA需要根据文本框结果来检查特定复选框。

huangapple go评论70阅读模式
英文:

VBA needed for checking a specific check box based on text box result

问题

在Word 2010中工作,我需要一个用于表单的VBA代码,根据填充到ActiveX文本字段的文本来检查两个ActiveX复选框中的一个(男性或女性)。我是一个新手,已经尝试了以下代码的各种变体:

Sub copyMaleFemale()
Dim ff As String
ff = CurrentFormField.Result

If ff = ("Male") Then 
ActiveDocument.FormFields("Check1").Result = Checked 
ActiveDocument.FormFields("Check2").Result = Unchecked

ElseIf ff = ("Female") Then
ActiveDocument.FormFields("Check1").Result = Checked
ActiveDocument.FormFields("Check2").Result = Unchecked

End If

End Sub

我感谢任何建议。

英文:

Working in Word 2010, I need VBA code for a form that will check one of two ActiveX check boxes based on text set to populate into an ActiveX text field. (Male or Female) I'm a novice and have tried various variations of the code below:

Sub copyMaleFemale()
Dim ff As String
ff = CurrentFormField.Result

If ff = ("Male") Then 
ActiveDocument.FormFields("Check1").Result = Checked 
ActiveDocument.FormFields("Check2").Result = Unchecked

ElseIf ff = ("Female") Then
ActiveDocument.FormFields("Check1").Result = Checked
ActiveDocument.FormFields("Check2").Result = Unchecked

End If

End Sub

I appreciate any suggestions.

答案1

得分: 0

你已经相当接近了。不需要在变量值周围添加括号。需要记住的是匹配必须精确,所以我添加了一个部分,如果文本字段稍微不同,就将两个复选框都设置为false。

始终使用With/End With是消除额外代码并使宏运行稍快的好方法:

Sub copyMaleFemale()
    Dim ff As String
    ff = ActiveDocument.FormFields("Text1").Result
    
    If ff = "male" Then
        With ActiveDocument
            .FormFields("Check1").CheckBox.Value = True
            .FormFields("Check2").CheckBox.Value = False
        End With
    ElseIf ff = "female" Then
        With ActiveDocument
            .FormFields("Check1").CheckBox.Value = True
            .FormFields("Check2").CheckBox.Value = False
        End With
    Else
        With ActiveDocument
            .FormFields("Check1").CheckBox.Value = False
            .FormFields("Check2").CheckBox.Value = False
        End With
    End If
End Sub
英文:

You're pretty close. No brackets needed around the variable values. Something to keep in mind is that the match has to be exact, so I added a section to set both checkboxes to false if the text field is anything slightly different.

Using With/End With is always a good way to eliminate extra code and make your macro run just a little faster:

Sub copyMaleFemale()
    Dim ff As String
    ff = ActiveDocument.FormFields("Text1").result
    
    If ff = "male" Then
        With ActiveDocument
            .FormFields("Check1").CheckBox.value = True
            .FormFields("Check2").CheckBox.value = False
        End With
    ElseIf ff = "female" Then
        With ActiveDocument
            .FormFields("Check1").CheckBox.value = True
            .FormFields("Check2").CheckBox.value = False
        End With
    Else
        With ActiveDocument
            .FormFields("Check1").CheckBox.value = False
            .FormFields("Check2").CheckBox.value = False
        End With
    End If
End Sub

huangapple
  • 本文由 发表于 2020年1月7日 02:29:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定