PDA

View Full Version : Automatically summarizing content of (e.g.) "fields"



Vazrael
03-16-2018, 04:43 AM
Hi there,

I'd like to create a template.
They can choose from various tables that are saved in the "BuildingBlock Gallery" (these are different questions for exams/tests).

In each table, one can find a "field" where the maximum points for this task/question have to be inserted.
Let's say one creates 5 questions and the maximum points per task like "4,3,4,5,2".

Can I now create a field/space/something at any point of the test (for example at the beginning), so the sum of theses maximum points are summarized (= 18 in this case) ?

In EXCEL it's clear to me ( =SUM(...) ), I'm sure in WORD there's a similar thing :)

Thanks in advance & sorry for the bad english

gmaxey
03-16-2018, 05:30 AM
Word has a field that you can use to calculate values but it could be very difficult to build your document in such a way that you could make it work. If you define your maxscore field in each table e.g., using a content control then you could do something like this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 3/16/2018
Dim oCC As ContentControl
Dim lngScore As Long
For Each oCC In ActiveDocument.ContentControls
If oCC.Title = "MaxScore" Then lngScore = lngScore + oCC.Range.Text
Next
MsgBox lngScore
lbl_Exit:
Exit Sub
End Sub

Vazrael
03-16-2018, 05:55 AM
Hi Greg,

thank you very much. Alright - could it be easier, if I use "DropDown"-bars instead of fields?
So I can insert, let's say, 20 numeric values (1, 2, 3, 4, ...) and then by using a macro I can summarize all numeric values that have been chosen in the placed dropdown bars? If yes, is the macro text easy (I'm not very familiar with these things, have to get into it first :mkay)

gmaxey
03-16-2018, 06:08 AM
If each of your table building blocks had a dropdown CC titled "MaxScore" with entries 1 through 20, then the code posted will work.

Vazrael
03-16-2018, 06:36 AM
Oh, that's really great, I managed to do it!

One last thing: Is it possible, to "connect" a field/textblock/whatever at the beginning of my word file with this macro, so that this field/textblock/whatever
a) automatically fills in what the macro would give as a result
(or, if not possible: b) fills in what the macro gives as a result after I executed the macro)
?

gmaxey
03-16-2018, 07:01 AM
In you completed test document, insert a CC titled "Sum_of_MaxScores"


Private Sub Document_Open()
Dim oCC As ContentControl
Dim lngScore As Long
For Each oCC In ActiveDocument.ContentControls
If oCC.Title = "MaxScore" Then lngScore = lngScore + oCC.Range.Text
Next
ActiveDocument.SelectContentControlsByTitle("Sum_of_MaxScores").Item(1).Range.Text = lngScore
lbl_Exit:
Exit Sub
End Sub
End Sub

Vazrael
03-16-2018, 07:25 AM
It says "Error":

"Only Comments may appear after End Sub, End Function or End Property".

If I delete the last "End Sub", it says:

"Run-time error 6124. You are not allowed to edit this selection because it is protected"

:think:

gmaxey
03-16-2018, 01:21 PM
Then make sure the content control titled Sum_of_MaxScores is not protected.