Consulting

Results 1 to 8 of 8

Thread: Automatically summarizing content of (e.g.) "fields"

  1. #1
    VBAX Regular
    Joined
    Mar 2018
    Posts
    6
    Location

    Automatically summarizing content of (e.g.) "fields"

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Mar 2018
    Posts
    6
    Location
    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 )

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    If each of your table building blocks had a dropdown CC titled "MaxScore" with entries 1 through 20, then the code posted will work.
    Last edited by gmaxey; 03-16-2018 at 06:56 AM.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Mar 2018
    Posts
    6
    Location
    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)
    ?

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular
    Joined
    Mar 2018
    Posts
    6
    Location
    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"


  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Then make sure the content control titled Sum_of_MaxScores is not protected.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •