Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-20-2020, 07:32 AM
Steve2081 Steve2081 is offline Radio buttons. Windows 7 64bit Radio buttons. Office 2016
Advanced Beginner
Radio buttons.
 
Join Date: Apr 2020
Posts: 44
Steve2081 is on a distinguished road
Default Radio buttons.

Does anyone have VBA code/macro which will atomically set all radio buttons on a word doc to 'No'?
Reply With Quote
  #2  
Old 07-20-2020, 09:27 AM
gmaxey gmaxey is offline Radio buttons. Windows 10 Radio buttons. Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

The only type of "radio" buttons that a Word document has is ActiveX optionbuttons. Is that what you are talking about?


Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFld As Field
  For Each oFld In ThisDocument.Fields
    Select Case oFld.OLEFormat.ClassType
      Case Is = "Forms.OptionButton.1"
        oFld.OLEFormat.Object.Value = False
    End Select
  Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-21-2020, 12:09 AM
Steve2081 Steve2081 is offline Radio buttons. Windows 7 64bit Radio buttons. Office 2016
Advanced Beginner
Radio buttons.
 
Join Date: Apr 2020
Posts: 44
Steve2081 is on a distinguished road
Default

Hi Greg
Thanks for the reply. Yes the ActiveX option buttons.
Basically we have a word doc/questionnaire with basic 'yes', 'no' or 'N/A' answers. Instead of having to click each individual answer to 'no' I was wondering if there was a macro that would enter all the 'no' options. I would believe this could be linked to a command button at the end of document. Do you have any suggestions?
Thanks
Reply With Quote
  #4  
Old 07-21-2020, 02:38 AM
gmayor's Avatar
gmayor gmayor is offline Radio buttons. Windows 10 Radio buttons. Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

For this type of application, I would use content control list boxes rather than Active X radio buttons. It would then be relatively simple to collate the values, if that is what you wish, at the end. e.g. the following macro will list the various answers. Title the content controls "Answer 1", Answer 2" etc.
I would recommend using https://www.gmayor.com/insert_content_control_addin.htm to insert the first dropdown list control then copy and paste it as often as required, then use the same tool to edit the controls to change the names.



Code:
Sub Macro1()
Dim oCC As ContentControl
Dim i As Integer
Dim sYes As String, sNo As String, sNA As String
Dim collYes As Collection, CollNo As Collection, CollNA As Collection
    For Each oCC In ActiveDocument.ContentControls
        If oCC.ShowingPlaceholderText = True Then
            oCC.Range.Select
            MsgBox "Complete " & oCC.TITLE & "!"
            GoTo lbl_Exit
        End If
    Next oCC
    Set collYes = New Collection
    Set CollNo = New Collection
    Set CollNA = New Collection
    For Each oCC In ActiveDocument.ContentControls
        If oCC.Type = wdContentControlDropdownList And oCC.TITLE Like "Answer*" Then
            Select Case UCase(oCC.Range.Text)
                Case Is = "YES"
                    collYes.Add oCC.TITLE
                Case Is = "NO"
                    CollNo.Add oCC.TITLE
                Case Else
                    CollNA.Add oCC.TITLE
            End Select
        End If
    Next oCC
    For i = 1 To collYes.Count
        sYes = sYes & collYes.Item(i)
        If i < collYes.Count Then sYes = sYes & vbCr
    Next i
    For i = 1 To CollNo.Count
        sNo = sNo & CollNo.Item(i)
        If i < collYes.Count Then sNo = sNo & vbCr
    Next i
    For i = 1 To CollNA.Count
        sNA = sNA & CollNA.Item(i)
        If i < CollNA.Count Then sNA = sNA & vbCr
    Next i


    MsgBox "The following " & collYes.Count & " questions, were answered 'Yes'" & vbCr & sYes & vbCr & vbCr & _
           "The following " & CollNo.Count & " questions, were answered 'No'" & vbCr & sNo & vbCr & vbCr & _
           "The following " & CollNA.Count & " questions, were answered 'N/A'" & vbCr & sNA
lbl_Exit:
    Set collYes = Nothing
    Set CollNo = Nothing
    Set CollNA = Nothing
    Set oCC = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 07-21-2020, 10:23 AM
gmaxey gmaxey is offline Radio buttons. Windows 10 Radio buttons. Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

If you opt to stick with the ActiveX controls (which I don't recommend) and if the option buttons are in fact captioned "Yes", "No" and "NA" then:


Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFld As Field
  For Each oFld In ThisDocument.Fields
    Select Case oFld.OLEFormat.ClassType
      Case Is = "Forms.OptionButton.1"
        If oFld.OLEFormat.Object.Caption = "No" Then oFld.OLEFormat.Object.Value = True
    End Select
  Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with userform and radio buttons Fissure Word VBA 4 04-27-2018 04:10 AM
Radio Buttons in Word Document in MAC Office 2016 kim88 Word 3 12-19-2016 03:36 PM
check box issue (problem with radio buttons from web page) Word 2013 cQQlgirl Word 6 03-19-2015 07:23 PM
Radio buttons. Performance problem ActiveX radio buttons NobodysPerfect Word VBA 8 05-31-2014 03:51 AM
example of radio buttons in for data entry? derohanes Excel 1 03-05-2011 09:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:02 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft