0

我需要你的帮助。我有一个小代码总结了 10 个表单域的值。此表单域的格式设置为 1000 分隔符。我遇到的问题是 VBA 现在将 1.000 之类的值返回为 1。换句话说:1+10+100+1.000=112。如何调整代码以便在计算这些表单域时忽略 1000 分隔符?任何帮助深表感谢!

ActiveDocument.FormFields("voattot").Result = Val(ActiveDocument.FormFields("betvoat1").Result) _
+ Val(ActiveDocument.FormFields("betvoat2").Result) + Val(ActiveDocument.FormFields("betvoat3").Result) _
+ Val(ActiveDocument.FormFields("betvoat4").Result) + Val(ActiveDocument.FormFields("betvoat5").Result) _
+ Val(ActiveDocument.FormFields("betvoat6").Result) + Val(ActiveDocument.FormFields("betvoat7").Result) _
+ Val(ActiveDocument.FormFields("betvoat8").Result) + Val(ActiveDocument.FormFields("betvoat9").Result) _
+ Val(ActiveDocument.FormFields("betvoat10").Result)
4

1 回答 1

2

我将创建一个单独的函数来解析表单字段中的结果字符串

Function GetFieldValue(FieldName as string)
Dim FF As FormField 
Dim Result As String
Dim Value As Double


Set FF = ActiveDocument.Formfields(FieldName)'Find the Form Field
Result = FF.Result                           'Get the Text from the Form Field
Result = Replace(Result, ",", "")            'Remove any commas
If Trim(Result) ="" Then
    GetFieldValue= 0                         'Return the numeric valu
ElseIf Isnumeric(Result) Then
    Value = CDbl(Result)                         'Convert to numeric value
    GetFieldValue= Value                          'Return the numeric value
Else
    Debug.Print "Not IsNumeric(""" & Result & """)"
    GetFieldValue= 0
End If
End Function

然后你可以把你的陈述写成

ActiveDocument.FormFields("voattot").Result = GetFieldValue("betvoat1") _
+ GetFieldValue("betvoat2") + GetFieldValue("betvoat3") _
+ GetFieldValue("betvoat4") + GetFieldValue("betvoat5") _
+ GetFieldValue("betvoat6") + GetFieldValue("betvoat7") _
+ GetFieldValue("betvoat8") + GetFieldValue("betvoat9") _
+ GetFieldValue("betvoat10")

这也意味着您可以将错误捕获放入您的函数中以处理空值或非数值或缺少表单字段。是否值得打扰取决于您

于 2014-08-21T23:02:18.020 回答