Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2022, 12:29 PM
Janet D Janet D is offline Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Windows XP Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Office 2019
Novice
Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form
 
Join Date: Jul 2022
Posts: 3
Janet D is on a distinguished road
Default Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form


Hello all.
First timer here. I'm a novice at VBA. I know what it is, and have copied/pasted and modified code but have never written from the start. I need some help. I have a Word form that has text boxes, drop downs and option buttons. I want to put a command button to clear all contents of the controls. I've found VbA codes that may help but either its for the content control text boxes OR the option button values to false but NOT all. I don't know how to write the code to clear everything. As well as protect the form again after clearing. Any help is very much appreciated.
Reply With Quote
  #2  
Old 07-14-2022, 02:50 AM
gmaxey gmaxey is offline Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Windows 10 Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
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

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oCtl As Object
Dim oCC As ContentControl
On Error Resume Next
  For Each oCtl In ActiveDocument.InlineShapes
    If oCtl.OLEFormat.ProgID = "Forms.OptionButton.1" Then
      oCtl.OLEFormat.Object.Value = False
    End If
  Next oCtl
  For Each oCC In ActiveDocument.Range.ContentControls
    Select Case oCC.Type
      Case Is = wdContentControlDropdownList
        oCC.Type = 1
        oCC.Range.Text = vbNullString
        oCC.Type = wdContentControlDropdownList
      Case Is = wdContentControlComboBox
        oCC.Type = 1
        oCC.Range.Text = vbNullString
        oCC.Type = wdContentControlComboBox
      Case Else
        oCC.Range.Text = vbNullString
    End Select
  Next oCC
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-14-2022, 07:06 AM
Janet D Janet D is offline Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Windows XP Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Office 2019
Novice
Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form
 
Join Date: Jul 2022
Posts: 3
Janet D is on a distinguished road
Default

Thank you for your response Greg! - i have a few questions.

for this "Forms.OptionButton.1" - do i need to change it to the name of the option buttons? I have a few. OR is this for all of them? And since I have a text box, would i just add the following?
Case Is = wdContentControlText
oCC.Type = 1
oCC.Range.Text = vbNullString
oCC.Type = wdContentControlText

Thanks.
Reply With Quote
  #4  
Old 07-14-2022, 11:07 AM
gmaxey gmaxey is offline Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Windows 10 Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
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 code will loop through all of the ActiveX option buttons as is.


The content control text boxes are handled by the Case Else statement.


Note for the content control comboboxes and dropdownlist, they are first converted to a plain text control (e.g., type = 1), reset, then converted back to the original type.


Best Regards,
Greg Maxey

Be who you are and say what you mean. Because those who mind don't matter and those who matter don't mind. ~ Dr. Seuss
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 07-27-2022, 07:02 AM
Janet D Janet D is offline Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Windows XP Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Office 2019
Novice
Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form
 
Join Date: Jul 2022
Posts: 3
Janet D is on a distinguished road
Default

Greg - THANK YOU THANK YOU - this is amazing. I have one more question that I hope you can help. When the Option Button's value = FALSE, I want the forecolor to change to RED in order to alert the user they have to put information in the text content control to describe the issue. I have Yes/No option buttons set to several questions and for most of the questions, if they select NO then they have to input a comment to detail what the exception is. I want them to see the visual reminder if its RED then they have to comment. Can you help with that as well? - PS - you are my hero!
Reply With Quote
  #6  
Old 07-27-2022, 10:25 AM
gmaxey gmaxey is offline Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Windows 10 Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
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

Modify the first macro as such:
If oCtl.OLEFormat.ProgID = "Forms.OptionButton.1" Then
oCtl.OLEFormat.Object.Value = False
oCtl.OLEFormat.Object.ForeColor = wdColorRed
End If

For each option button control, add a change event like this:

Private Sub OptionButton1_Change()
If OptionButton1.Value = False Then
OptionButton1.ForeColor = wdColorRed
Else
OptionButton1.ForeColor = wdColorBlack
End If
End Sub

Private Sub OptionButton2_Change()
If OptionButton2.Value = False Then
OptionButton2.ForeColor = wdColorRed
Else
OptionButton2.ForeColor = wdColorBlack
End If
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 01-13-2023, 03:14 PM
Jrod9190 Jrod9190 is offline Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Windows 11 Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Office 2019
Novice
 
Join Date: Jan 2023
Posts: 7
Jrod9190 is on a distinguished road
Default

@gmaxey

This has been a serious help in my unit. I was wondering if you could help me out with a little tweak. Instead of the drop boxes becoming nulled; is there a way to change all boxes to the first option in the drop down? I appreciate all the help you’ve always given!
Reply With Quote
  #8  
Old 01-14-2023, 02:36 AM
gmayor's Avatar
gmayor gmayor is offline Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Windows 10 Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
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

The first option in a dropdown list is normally the placeholder text and if you set the value to "" or vbNullString as in Greg's macro the placeholder text should be displayed. You can however select any item in the list using the following -
Code:
Case Is = wdContentControlDropdownList
        oCC.Type = 1
        oCC.Range.Text = vbNullString
        oCC.Type = wdContentControlDropdownList
         oCC.DropdownListEntries.Item(2).Select
- here the item after the placeholder text.
or even
Code:
Case Is = wdContentControlDropdownList
        oCC.DropdownListEntries.Item(2).Select
Have you considered saving the form as a template from which new documents are created? Then as each new document matches the template there should be no need to clear the form.
__________________
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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Content Controls - Dependent Dropdown someazguy Word VBA 14 02-05-2023 08:01 PM
Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Clear Button For Specific Content Control shaun0406 Word VBA 6 06-14-2021 10:55 AM
Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Content Controls - Dependent Dropdown & Text Mightymegs Word VBA 6 05-18-2020 05:34 AM
Clear Values from All Controls on Form ScottyBee Word VBA 2 04-02-2019 09:55 AM
Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Add new row to protected form including content controls via Command Button tbrookes Word VBA 5 06-20-2016 02:48 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:49 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