Consulting

Results 1 to 18 of 18

Thread: Word Optionbutton Calculations

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location

    Word Optionbutton Calculations

    Hi,

    I'm very new to this ms word vba. i'm using ms word 2010. I have 4 optionbuttons. I'm trying to get the Score = selected value * 25
    i really need some examples step by step. i wont mind if someone use another number besides 25 for examples and steps. im having a very hard time doing this and i need help.

    i rename my OptionButtons1 to 4 to:
    optionbutton_1_3 (optionbutton1)
    optionbutton_1_2 (optionbutton2)
    optionbutton_1_1 (optionbutton3)
    optionbutton_1_0 (optionbutton4)

    i want 0 to be set as default when selecting the optionbutton 0

    i want the numbers to appear on the top where it say score and it is set to 0. (SEE IMAGE BELOW)


    Dim lngX As Long
    Private Sub OptionButton_1_3_Click()
    If OptionButton_1_3.Value = True Then
    ActiveDocument.Result = 75
    End If
    ActiveDocument.Fields.Update
    End Sub
    Private Sub OptionButton_1_2_Click()
    If OptionButton_1_2.Value = True Then
    ActiveDocument.Result= 50
    End If
    ActiveDocument.Fields.Update
    End Sub
    Private Sub OptionButton_1_1_Click()
    If OptionButton_1_1.Value = True Then
    ActiveDocument.Result = 25
    End If
    ActiveDocument.Fields.Update
    End Sub
    Private Sub OptionButton_1_0_Click()
    If OptionButton_1_0.Value = True Then
    ActiveDocument.Result = 0
    End If
    ActiveDocument.Fields.Update
    End Sub

    Capture411.jpg

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Why not use a simple formfield dropdown with options 0-3 and use field coding to multiply whichever option is chosen by 25? Alternatively, use a content control dropdown and an on-exit macro to multiply whichever option is chosen by 25 and output the result to another content control?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    One of Paul's suggestions, but if you really want to use option buttons then something like this:

    Private Sub OptionButton0_Click()
      If OptionButton0 Then
        Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 0"
      End If
    End Sub
    Private Sub OptionButton1_Click()
      If OptionButton1 Then
        Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 25"
      End If
    End Sub
    Private Sub OptionButton2_Click()
      If OptionButton2 Then
        Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 50"
      End If
    End Sub
    Private Sub OptionButton3_Click()
      If OptionButton3 Then
        Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 75"
      End If
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Quote Originally Posted by gmaxey View Post
    One of Paul's suggestions, but if you really want to use option buttons then something like this:

    Private Sub OptionButton0_Click()
      If OptionButton0 Then
        Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 0"
      End If
    End Sub
    Private Sub OptionButton1_Click()
      If OptionButton1 Then
        Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 25"
      End If
    End Sub
    Private Sub OptionButton2_Click()
      If OptionButton2 Then
        Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 50"
      End If
    End Sub
    Private Sub OptionButton3_Click()
      If OptionButton3 Then
        Selection.Tables(1).Cell(1, 1).Range.Text = "Scorre = 75"
      End If
    End Sub

    Yes thanks. what if i add one more table and rows and columns to this table and a different number say like 20. will it be such as:
    Private Sub OptionButton3_Click()
    If OptionButton3 Then
    Selection.Tables(2).Cell(1, 1).Range.Text = "Scorre = 20"
    End If
    End Sub


    like Selection.Table (2). Cell(2,2) = "Score = 20"

    and last but not least what to do if i want to get the total of both tables at the bottom of the tables.

    thank u so much this helps me out a whole lot
    Last edited by Ladyj205; 04-04-2018 at 07:52 AM.

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    I can't really guess what you are wanting to do. Show the table structure you want to use. If you want to sum two "results" value the you should put each one in by itself in a definable range (e.g., a cell, bookmark, content control).
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Quote Originally Posted by gmaxey View Post
    I can't really guess what you are wanting to do. Show the table structure you want to use. If you want to sum two "results" value the you should put each one in by itself in a definable range (e.g., a cell, bookmark, content control).
    sorry for the confusion. i just want to put the two values total for a grand total.
    Attached Images Attached Images

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    So you have 1 table. See attached for one basic process.
    Attached Files Attached Files
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    yes thats what im going for.

  9. #9
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Quote Originally Posted by gmaxey View Post
    So you have 1 table. See attached for one basic process.
    if i wanted to add text boxes for the scores. what would i put for the textboxes for each score and grandtotal. i been pulling my hair out trying to figure this out as well.

    so i would do something like:

    ActiveDocument.InlineShapes(1).OLEFormat.Object.Value

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Why would you want to put inline text boxes in the tables? Isn't it working in the demo I provided?
    Greg

    Visit my website: http://gregmaxey.com

  11. #11
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Quote Originally Posted by gmaxey View Post
    Why would you want to put inline text boxes in the tables? Isn't it working in the demo I provided?
    Yes everything is working. I was told that i needed to put the textboxes for the scores and grandtotal. Just making my life more complicated

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Greg -- typo in your attachment in #7


    Function fncCellText(oCell As Cell)
    should be

    Function fcnCellText(oCell As Cell)    ' fcn not fnc

    Sub CalcDisplay()
    Dim oTbl As Table
    Dim arrParts() As Variant
      Set oTbl = ActiveDocument.Tables(1)
      With oTbl.Cell(1, 1).Range
         Select Case True
           Case opt_25_3: .Text = "75"
           Case opt_25_2: .Text = "50"
           Case opt_25_1: .Text = "25"
          Case opt_25_0:  .Text = "0"
        End Select
      End With
      With oTbl.Cell(6, 1).Range
         Select Case True
           Case opt_20_3: .Text = "60"
           Case opt_20_2: .Text = "40"
           Case opt_20_1: .Text = "20"
          Case opt_20_0:  .Text = "0"
        End Select
      End With
      oTbl.Cell(11, 2).Range.Text = CLng(fcnCellText(oTbl.Cell(1, 1))) + CLng(fcnCellText(oTbl.Cell(6, 1)))
    End Sub
    
    
    Function fncCellText(oCell As Cell)
    Dim oRng As Range
      Set oRng = oCell.Range
      oRng.End = oRng.End - 1
      fcnCellText = oRng.Text
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  13. #13
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    oops. Thanks
    Greg

    Visit my website: http://gregmaxey.com

  14. #14
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Quote Originally Posted by gmaxey View Post
    oops. Thanks
    So will add ActiveDocument.InlineShapes(1).OLEFormat.Object.Value between case and end select

    Sub CalcDisplay()
    Dim oTbl As Table
    Dim arrParts() As Variant
    Set oTbl = ActiveDocument.Tables(1)
    With oTbl.Cell(1, 1).Range
    Select Case True
    Case opt_25_3: .Text = "75"
    Case opt_25_2: .Text = "50"
    Case opt_25_1: .Text = "25"
    Case opt_25_0: .Text = "0"
    End Select
    ActiveDocument.InlineShapes(1).OLEFormat.Object.Value
    End With
    With oTbl.Cell(6, 1).Range
    Select Case True
    Case opt_20_3: .Text = "60"
    Case opt_20_2: .Text = "40"
    Case opt_20_1: .Text = "20"
    Case opt_20_0: .Text = "0"
    ActiveDocument.InlineShapes(1).OLEFormat.Object.Value
    End Select
    End With
    oTbl.Cell(11, 2).Range.Text = CLng(fcnCellText(oTbl.Cell(1, 1))) + CLng(fcnCellText(oTbl.Cell(6, 1)))
    End Sub


    Function fncCellText(oCell As Cell)
    Dim oRng As Range
    Set oRng = oCell.Range
    oRng.End = oRng.End - 1
    fcnCellText = oRng.Text
    End Function

  15. #15
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    What is your question?
    Greg

    Visit my website: http://gregmaxey.com

  16. #16
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Quote Originally Posted by gmaxey View Post
    What is your question?
    I'm trying to add text boxes for the score and grandtotal. i was asking if i add ActiveDocument.InlineShapes(1).OLEFormat.Object.Value inbetween case and end select. will that work?

  17. #17
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Come on my friend, why don't you simply try and see if it works? It won't work and why you would even imagine it would work is beyond me.
    Attached Files Attached Files
    Greg

    Visit my website: http://gregmaxey.com

  18. #18
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    it works. thank u so much.

Posting Permissions

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