简体   繁体   English

MS Word VBA:计算 ContentControl 输入的平均值并插入表中的书签

[英]MS Word VBA: Calculate average of ContentControl inputs and insert to bookmark in table

I have an MS Word document where I require users to pick a numerical value from 1-5, in various ContentControls for several questions.我有一个 MS Word 文档,我要求用户在各种 ContentControls 中为几个问题选择一个 1-5 的数值。

I have inserted at the top of the doc a button named Calculate.我在文档顶部插入了一个名为“计算”的按钮。 When users click this button I want to take an average of their inputs and enter the result in to a bookmarked cell in a table.当用户单击此按钮时,我想对他们的输入取平均值并将结果输入到表格中带有书签的单元格中。 The user input content controls are named "RTi", with i running from 1 to 20.用户输入内容控件被命名为“RTi”,i 从 1 到 20。

Private Sub Calculate_Click()
    Dim TotalRating As Double
    Dim OrgRating As Double
    Dim TeamRating As Double
    Dim StratRating As Double
    Dim PandPRating As Double
    Dim EvidenceRating As Double
    Dim ESGRating As Double
    Dim ODDRatnig As String
    
   ' Average of RT 1 to 4,input to bookmark "TotalRating" as Double
   ' Average of RT 5 to 7, input to bookmark "OrgRating" as Double
   ' Average of RT 8 to 10, input to bookmark "StratRating" as Double
   ' Average of RT 11 to 14, input to bookmark "PandPRating" as Double
   ' Average of RT 15 to 18, input to bookmark "EvidenceRating" as Double
   ' Value of RT 19, input to bookmark "ESGRating" as Double
   ' Value of RT 20, input to bookmark "ODDRating" as String
   

End Sub

I am having some trouble getting started, but have put the steps above that I wish to execute.我在开始时遇到了一些麻烦,但已将我希望执行的步骤放在上面。 If someone would kindly help me with an example for the first commented out section above, showing a code that will perform an average or RT1, RT2, RT3 and RT4 and enter the result in bookmark TotalRating.如果有人愿意帮助我为上面第一个注释掉的部分提供一个示例,显示一个将执行平均或 RT1、RT2、RT3 和 RT4 的代码,并将结果输入书签 TotalRating。

For example, assuming you have a cell bookmarked as "TotalRating" .例如,假设您有一个标记"TotalRating"的单元格。 But you will need to deal with all the possible error conditions (eg, they don't enter a number, the numbers aren't correctly recognized by Word, there are no CCs with the relevant Titles, there is no bookmark with the correct name, there is more than one paragraph in the cell, etc. etc.).但是您需要处理所有可能的错误情况(例如,他们没有输入数字、Word 无法正确识别数字、没有具有相关标题的 CC、没有具有正确名称的书签,单元格中有多个段落等)。 If you have questions about any of those things please don't ask follow-up questions here.如果您对这些事情有任何疑问,请不要在此处提出后续问题。 The thing to do is research them, then if you cannot work out an answer, ask a new Question and reference this one.要做的是研究它们,然后如果您无法找到答案,请提出一个新问题并参考这个问题。

' Average of RT 1 to 4,input to bookmark "TotalRating" as Double
Call updateDoubleTotal(ActiveDocument,"RT",1,4,"TotalRating")

Sub updateDoubleTotal(doc As Word.Document, CCTitlePrefix As String, StartNum As Integer, EndNum As Integer, CellName As String)
Dim i As Integer
Dim Total As Double
Total = 0
With doc
  For i = StartNum To EndNum
    Total = Total + CDbl(.SelectContentControlsByTitle(CCTitlePrefix & CStr(i))(1).Range.Text)
  Next
  .Bookmarks(CellName).Range.Paragraphs(1).Range.Text = CStr(Total / (1 + (EndNum - StartNum)))
End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM