如何在没有奇怪滚动的情况下计算旧表单字段的总和? [英] How can I calculate the sum of Legacy Form Fields without werid scrolling?

查看:40
本文介绍了如何在没有奇怪滚动的情况下计算旧表单字段的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Word 文档,其中包含一个表格,其中包含商品的价格、订单金额和商品总额,其中金额和商品总额是旧表单字段:

I have a Word document with a table that contains the price of an item, the order amount and the item sum, where amount and item sum are Legacy Form Fields:

您可以通过复选框(还有旧表单字段)选择其他项目作为选项:

There are other items available as options that you can chose via checkbox (also Legacy Form Fields):

现在我已经创建了一个计算项目总和的函数

Now I've created a function that calculates the item sum

Sub order_amount_Click()
    Dim amount As Integer
    Dim valueFromCell As String
    Dim list
    Dim value As Double

    amount = ActiveDocument.FormFields("order_amount").Result
    If amount > 0 Then
        valueFromCell = ThisDocument.Tables(1).Cell(5, 3).Range.Text
        list = Split(valueFromCell, " ")
        value = list(0)
        subsum = value * amount
        ActiveDocument.FormFields("order_amount_sum").Result = subsum
    Else
        ActiveDocument.FormFields("order_amount_sum").Result = 0
    End If

    CalculateSum
End Sub

以及另一个计算附加商品的商品总和(如果复选框已激活)乘以订单金额的函数:

and another function that calculates the item sum for additional items (if the checkbox is activated) multiplied by the order amount:

Sub optional_item_Click()
    Dim checkbox As Integer
    Dim valueFromCell As String
    Dim list
    Dim value As Double
    Dim amount As Integer
    Dim subsum As Double

    checkbox = ActiveDocument.FormFields("optional_item1").Result
    amount = ActiveDocument.FormFields("order_amount").Result

    If checkbox = 1 And amount > 0 Then
        valueFromCell = ThisDocument.Tables(1).Cell(7, 3).Range.Text
        list = Split(valueFromCell, " ")
        value = list(0)
        subsum = value * amount
        ActiveDocument.FormFields("optional_item1_sum").Result = subsum
    Else
        ActiveDocument.FormFields("optional_item1_sum").Result = 0
    End If

    CalculateSum
End Sub

(Click() 函数和 optional_item 字段一样多,总共 17 个 - 我还没有概括该函数.)

(There are as many Click() functions as there are optional_item fields, 17 in sum - I haven't generalized the function yet.)

Sub 中的最后一行是函数调用,用于计算净额、增值税和最终总和

The last line within both Subs is a function call, that calculates the net sum, the VAT and the final total sum

Function CalculateSum()
    Dim net As Double
    Dim vat As Double
    Dim total_sum As Double

    net = CDbl(ActiveDocument.FormFields("order_amount_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item1_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item2_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item3_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item4_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item5_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item6_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item7_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item8_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item9_sum").Result)

    ' Cannot compile when line too long, so splitting into two statements
    net = net + CDbl(ActiveDocument.FormFields("optional_item10_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item11_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item12_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item13_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item14_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item15_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item16_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item17_sum").Result)
    'MsgBox "net " & net
    ActiveDocument.FormFields("net_sum").Result = net

    vat = net * 0.19
    'MsgBox "vat " & vat
    ActiveDocument.FormFields("vat_sum").Result = vat

    total_sum = net + vat
    'MsgBox "total " & total_sum
    ActiveDocument.FormFields("total_sum").Result = total_sum
End Function

问题

代码本身运行良好,所有元素都计算正确.但是有两个主要问题使整个文档几乎无法使用(用户的所有操作都仅限于在文档顶部输入他们的姓名,选择金额并取消/激活复选框;无法访问总和字段):

Problem

The code itself works fine, all elements are calculated correctly. But there are two major problems that make the whole document almost unusable (all actions for users are restricted to enter their name on top of the document, chose the amount and de-/activate the checkboxes; sum fields aren't accessible):

  1. 当我执行一个操作(例如输入一个金额)时,CalculateSum() 函数明显地循环遍历所有 sum 字段(跨越三页)并奇怪地滚动sum 字段中的文档.
  2. 当我点击第 2 或第 3 页上的复选框时,文档会向上滚动到第一页,然后我可以输入我想要订购的件数.
  1. When I perform an action (e.g. enter an amount) the CalculateSum() function visibly loops over all sum fields (spanned over three pages) and weirdly scrolls on the document along the sum fields.
  2. When I click on a checkbox on the 2nd or 3rd page, the document scrolls up to the first page to the place where I can enter the amount of pieces I want to order.

现在我怎样才能抑制在 sum 字段上循环并奇怪地滚动?以及如何防止文档向上滚动到第一页?

Now how can I supress all that looping over the sum fields and weirdly scrolling around? And how can I prevent the document from scrolling up to the 1st page?

任何帮助表示赞赏!(我也很感谢对我的代码的旁注,因为我是 VBA 新手.)

Any help appreciated! (I'm also thankful for side comments on my code, as I'm new to VBA.)

我添加了一个截屏视频显示错误.

基本问题似乎是宏正在滚动到我在脚本中所指的位置,例如如果我正在使用

The basic problem seems to be that the macro is scrolling to the position I am referring to in my script, e.g. if I'm using

ActiveDocument.FormFields("total_sum").Result = total_sum

它正在滚动到 total_sum 字段.

it's scrolling to the total_sum field.

推荐答案

作为 AdamsTips 在别处发现,替换此

ActiveDocument.FormFields("myField").Result

由此

ActiveDocument.Bookmarks("myField").Range.Fields(1).Result

允许在不聚焦的情况下访问该字段.

allows accessing the field without focusing it.

这篇关于如何在没有奇怪滚动的情况下计算旧表单字段的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆