|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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. |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
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. |
#4
|
|||
|
|||
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 |
#5
|
|||
|
|||
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!
|
#6
|
|||
|
|||
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 |
#7
|
|||
|
|||
@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! |
#8
|
||||
|
||||
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 or even Code:
Case Is = wdContentControlDropdownList oCC.DropdownListEntries.Item(2).Select
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Content Controls - Dependent Dropdown | someazguy | Word VBA | 14 | 02-05-2023 08:01 PM |
Clear Button For Specific Content Control | shaun0406 | Word VBA | 6 | 06-14-2021 10:55 AM |
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 |
Add new row to protected form including content controls via Command Button | tbrookes | Word VBA | 5 | 06-20-2016 02:48 AM |