plain text content control enabled/disabled depending on option button enabled

noobprogrammergirl

New Member
Joined
May 8, 2017
Messages
10
I have been trying to figure out how to make this work for probably 25 hours over the past 3 days, googling and testing, reading and implementing, but I cannot for the life of me figure this out.

I am trying to create a form in Word that we can use to keep track of experiments that we do in our oncology lab.
I would expect you can do the following:

I added two option buttons that are activated mutually exclusively (you can click one or the other).
Below each option, I put a plain text content control in which scientist will describe some aspect of the chosen option.
I *WANT* to activate the plain text content control under the option button above it, if and only if the option button is activated. Or, another way about it, once a particular option button is chosen, I only want the plain text content control below that button to be available for typing and I want the other one to be inactive (cannot type inside because you didn't choose its 'associated' option).

I think I found ways to do this if I create the plain text content control *from* code in VBA. I also think I found a way to do this if, instead of plain text content control, I use a text box (just called textbox1, for example). BUT I really want to use the plain text content control rather than a text box. I figure I should be able to use code to inactivate that plain text content control that I inserted using the UI on the developer tab. However, I cannot figure out how to identify (call? designate? point to?) the UI-inserted plain text content control in VBA code so that I can say:

if optionbutton1.enabled = true then
'that plain textCC*'.enabled = true
else
'same plain textCC as above'.enabled = false

* the particular plain text content control that I added via the UI... it has a tag (i.e. "plaintext1", but cant figure out how to call it by its tag either.

I am such a noob that I really dont understand any of the error messages at all... I am always getting compile errors and variable not defined and Identifier under cursor is not recognized because I cant seem to use the right code to refer to that plain text content control.

Can I do what I am asking or is it impossible and I wasted all these hours trying to do something that cant be done and I need to kill the OCD craziness inside me and force myself to use a text box or code the plain text content control directly from VBA?

Thank you for humoring me. Cheers!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Without knowing how your option buttons are being enabled/disabled or how you're then accessing the document, and assuming you've applied 'plaintext1' & 'plaintext2' titles to the content controls, the best that can be offered is:
Code:
With ActiveDocument
  .SelectContentControlsByTitle("plaintext1")(1).LockContents = OptionButton1.Enabled
  .SelectContentControlsByTitle("plaintext2")(1).LockContents = OptionButton2.Enabled
End With
 
Upvote 0
I dont really even know how to tell you that I am enabling/disabling my option buttons, accessing the document, or applying titles according to how you expect. I will just post here exactly what I did with screenshots and you will see that the contentcontrolplaintext doesnt get identified in the document automatically like the option button does. I dont know how to get the plain text into the vba as an object that I can tell to do things... Thank you for the suggestions so far!!


.... actually I cant even figure out how to paste my screenshots into here :(

I tried using html generator addin, but that meant I had to use excel. When I insert a picture into excel and drag across the cells where the picture is and generate html, it doesnt recognize the picture... only the empty cells. How could I show my screenshots here? Upload them to a picture sharing website and link to it?
 
Last edited by a moderator:
Upvote 0
Nothing in any of the links you posted suggests either of the option buttons is being enabled or disabled. At most, all you're doing is activating one. Adding the code I posted to the Document_New event is pointless. Try:
Code:
Private Sub Document_Open()
Call ControlToggle
End Sub

Private Sub OptionButton1_Click()
Call ControlToggle
End Sub

Private Sub OptionButton2_Click()
Call ControlToggle
End Sub

Sub ControlToggle()
With ActiveDocument
  .SelectContentControlsByTitle("plaintext1")(1).LockContents = Not OptionButton1.Value
  .SelectContentControlsByTitle("plaintext2")(1).LockContents = Not OptionButton2.Value
End With
End Sub
 
Upvote 0
oh my knight in shining armor!!!!!!!!!!
It didnt work at first (Run-time error '5941' The requested member of the collection does not exist), but realized it was just because I only put in a tag for the plain texts rather than a title so I changed the SelectContentControlsByTitle to SelectContentControlsByTag and it worked beautifully!!!!!!!!
Tell me how the control toggle works if you have time please!!!!!! What's that (1) for? How did you learn how which parts go where in that line?! Could you tell me the name for each of the parts? Like is one of them an object? a variable? a range? a method? a constant?

Regardless, THANK YOU THANK YOU THANK YOU!!!!!!!!!!!!! I just could not for the life of me get the "SelectContentControlsByTitle" function (?or whatever that part is called if its not an actual 'function?') to work!!! I tried it so many different ways!! I definitely did not have "Not OptionButton1.Value" in any of my attempts... it was more like "If OptionButton1.Enabled = True Then" but always running into trouble trying to "call" or "identify" the plain text content control by tag or title. ughhhh I'm never going to learn how to program!!
 
Upvote 0
In the line:
.SelectContentControlsByTitle("plaintext1")(1).LockContents = Not OptionButton1.Value
• SelectContentControlsByTitle is a Method.
• "plaintext1" is, fairly obviously, the content control's Title. One could use it's Tag instead, as you appear to have done.
• (1) tells Word which of the content controls having that Title (or Tag) to use - you might have many with the same Title (or Tag). A more obvious implementation would be to use .Item(1). Item is a Method.
• Not OptionButton1.Value tells Word to negate whatever the value of OptionButton1 is. That value can be True or False and, perhaps counter-intuitively, we want the opposite of whatever OptionButton1 is set to.
The Enabled property you were using refers to whether the ActiveX control is enabled/disabled - which in a different context one might do to an ActiveX control that only becomes accessible if certain other conditions are met; it has nothing to do with whether an option button has been clicked (disable it by setting OptionButton1.Enabled = False and you can't use it!).
 
Upvote 0
I am back, unfortunately. I cannot for the life of me get the code into a "portable" format... as an add-in or module or macro that I can send along as a separate file and tell the destination computers to install as an add-in which can be activated upon loading the original file. And if I just send the original file as it is, the code does not come along with it, so the macro doesn't work and they can't click the items that I have "locked if"... Help me please?

Here is the code in its current state:


Private Sub Document_New()
End Sub
Private Sub Document_Open()
Call ControlToggle
End Sub


Private Sub OptionButton1_Click()
Call ControlToggle
End Sub
Private Sub OptionButton2_Click()
Call ControlToggle
End Sub


Sub ControlToggle()
With ActiveDocument
.SelectContentControlsByTag("Incucyteplaintext1")(1).LockContents = Not OptionButton1.Value
.SelectContentControlsByTag("Incucyteplaintext2")(1).LockContents = Not OptionButton1.Value
.SelectContentControlsByTag("check1")(1).LockContents = Not OptionButton1.Value
.SelectContentControlsByTag("check2")(1).LockContents = Not OptionButton1.Value
.SelectContentControlsByTag("check3")(1).LockContents = Not OptionButton1.Value
.SelectContentControlsByTag("platelightplaintext1")(1).LockContents = Not OptionButton2.Value
.SelectContentControlsByTag("platelightplaintext2")(1).LockContents = Not OptionButton2.Value
.SelectContentControlsByTag("platelightcombo1")(1).LockContents = Not OptionButton2.Value


End With


End Sub




Private Sub OptionButton5_Click()


End Sub


Private Sub OptionButton6_Click()


End Sub


Private Sub TextBox1_Change()


End Sub
 
Upvote 0
You need to ensure the macro code is saved in the document's own 'ThisDocument' code module (i.e. not in its template's 'ThisDocument' code module) and that the document is saved in the .docm format. The macro will then travel with the document itself and be usable as-is. The only thing a recipient might need to do is to enable the macro when prompted upon opening the document.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top