Enabling/disabling content control text boxes in word 2016 - vba

I am trying to create a form that requires text boxes to be disabled until a checkbox is ticked. Previously I have been able to use activeX and Legacy tools in the document but after a software upgrade to a 3rd party application the manufacture no longer supports these. I am therefore having to re-learn how to use content control boxes and their VBA controls.
This is my attempt based at varying google responses. "Lowers" is the checkbox. "Long3""Long4" "Lat2""Vert2" are the textboxes I am trying to control.
I had managed to disable the textboxes but this now seems to have been lost.
Any suggestions would be greatly appreciated.
Private Lowers_onChange()
Dim oCC_Lowers As ContentControl
Set oCC_Lowers = ActiveDocument.SelectContentControlsByTag("Lowers").Item(1)
Dim oCC_Long3 As ContentControl
Set oCC_Long3 = ActiveDocument.SelectContentControlsByTag("Long3").Item(2)
Dim oCC_Long4 As ContentControl
Set oCC_Long4 = ActiveDocument.SelectContentControlsByTag("Long4").Item(3)
Dim oCC_Lat2 As ContentControl
Set oCC_Lat2 = ActiveDocument.SelectContentControlsByTag("Lat2").Item(4)
Dim oCC_Vert2 As ContentControl
Set oCC_Vert2 = ActiveDocument.SelectContentControlsByTag("Vert2").Item(5)
If oCC_Lowers.LockcontentControl = True Then
oCC_Long3.LockContents = False
oCC_Long4.LockContents = False
oCC_Lat2.LockContents = False
oCC_Vert2.LockContents = False
Else
oCC_Long3.LockContents = True
oCC_Long4.LockContents = True
oCC_Lat2.LockContents = True
oCC_Vert2.LockContents = True
End If
End Sub

Firstly a link to a good resource on working with Content Controls: Greg Maxey's Word Content Controls page
As your code is an event handler responding to a built-in event, it needs to be in the ThisDocument module. As there isn't a Change event you need to use the OnExit event. You can find this by navigating to the ThisDocument module in the VBE and selecting Document in the left hand drop-down. In the right hand drop-down select ContentControlOnExit. An empty event handler is then created with the correct syntax.
You will note that this event handler will fire for every content control in the document, so you need to test the value of ContentControl.Tag to see if it is the one that you need to work with.
Once you have verified that you have the correct content control you can then use the value of its Checked property to set the value of the LockContents property of each of the other content controls. Your completed code should then look like this:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If ContentControl.Tag = "Lowers" Then
Dim oCC_Long3 As ContentControl
Set oCC_Long3 = ActiveDocument.SelectContentControlsByTag("Long3").Item(1)
oCC_Long3.LockContents = Not ContentControl.Checked
Dim oCC_Long4 As ContentControl
Set oCC_Long4 = ActiveDocument.SelectContentControlsByTag("Long4").Item(1)
oCC_Long4.LockContents = Not ContentControl.Checked
Dim oCC_Lat2 As ContentControl
Set oCC_Lat2 = ActiveDocument.SelectContentControlsByTag("Lat2").Item(1)
oCC_Lat2.LockContents = Not ContentControl.Checked
Dim oCC_Vert2 As ContentControl
Set oCC_Vert2 = ActiveDocument.SelectContentControlsByTag("Vert2").Item(1)
oCC_Vert2.LockContents = Not ContentControl.Checked
End If
End Sub

Related

is there a way of changing the Font properties of a text box in access VBA on a continuous form?

I am writing an app that send reports to a word document, this is done by the usage of data that is displayed on a continuous form, one form has the data that display's the selected headers that will be printed onto the report, the user can then change the font style by the usage of the windows font window.
This all works fine, what I want to do now is then update the text box on the continuous form with the font styles that have been stored in the data table, so that the user can see the font and style they have selected.
I have tried various approaches to no success, the last method I have tried I will post below in code.
dim i as integer
Private Sub Form_Load()
i = 0
End Sub
Private Sub Form_Current()
i = i + 1
Me.txtchapterName.Tag = "ctrl" & i
End sub
Function SetFieldProperties()
Dim rst As Recordset
Dim ctrl As TextBox
Set rst = Me.Recordset
Set ctrl = Me.ActiveControl
If rst.RecordCount > 0 Then
If ctrl.Tag = Me.txtchapterName.Tag Then
ctrl.ForeColor = Nz(Me![TextFontColour], 0)
ctrl.FontName = Nz(Me![TextFontName], "Calibri")
ctrl.FontSize = Nz(Me![TextFontSize], 14)
ctrl.FontUnderline = Nz(Me![TextFontAlign], 0)
ctrl.FontBold = Nz(Me![TextFontBold], False)
End If
End If
End Function
Private Sub txtchapterName_AfterUpdate()
SetFieldProperties
End Sub
To some extent this works, but it will update all of the controls on the form with the font style.
For clarity my question is in the title of the post...
Thank you all in advance.
Mark.

Disable TextInput Object

I have a checkbox and if the checkbox.Value = False I want to disable my TextInput-Object. On the internet there where some suggestions, but the methods which were used are not working for me, because the methods are not found.
I tried it with the .Valid-method:
Dim tf As TextInput
Dim checkbox As CheckBox
Sub checkbox_click()
Set checkbox = ActiveDocument.FormFields("checkbox").CheckBox
Set tf = ActiveDocument.FormFields("textfield").TextInput
If checkbox.Value = False
tf.Valid = False
End If
End Sub
But that doesn't work for some reason. I found tf.Enabled = False on the internet, but this method is unknown in my case.
You need something more like this:
Dim ff As FormField
Dim checkbox As CheckBox
.
.
Set checkbox = ActiveDocument.FormFields("checkbox").CheckBox
Set ff = ActiveDocument.FormFields("textfield")
If checkbox.Value = False
ff.Enabled = False
End If
With legacy FormField objects, some of the properties you need are associated with the FormField itself, and others are associated with child objects of the FormField such as the FormField.Checkbox
So the problem here is that tf is a FormField.TextInput object, but .Enabled is a property of the FormField object.
Not relevant to your question, but just as an observation, FormFields do not have Word events associated with them in the normal sense of VBA Events. The settings of each field tell Word to run a named Sub "on entry" and/or "on exit" - that's it. No actual click events. No problem with using names that makes those things look like events but I just thought I'd mention it.
Whilst the internet can be helpful you should also use the Object Browser in the VBE along with Help. 5 seconds spent doing a search for FormField would have given you the answer.
Dim tf As TextInput
Dim checkbox As CheckBox
Sub checkbox_click()
Set checkbox = ActiveDocument.FormFields("checkbox").CheckBox
If checkbox.Value = False
ActiveDocument.FormFields("textfield").Enabled = False
End If
End Sub

How do I efficiently pair Toggle Buttons and Textboxes in Access Form?

I know the title is a bit confusing so let me make myself as clear as possible.
In an Access form (2010), I have a set of text fields meant to contain dates. Those fields are all optional, the user can fill in any number of dates.
To make it more user-friendly, I want to associate each field to a toggle button. Then I want 2 things to happen :
On CURRENT event :
If a text field has a value, then it is visible and the toggle button
associated with it is pressed.
If a text field is empty, then it is not visible and the toggle button isn't pressed.
On CLICK of a toggle button :
If the text field associated with it has a value, then this field gets cleared (and invisible) and the toggle button gets de-pressed ;
If the text field associated with it is empty, then the focus is set on it and the toggle button gets pressed (and stay that way if a value is entered in the text field, otherwise everything gets back the way it was, invisible and unpressed).
So far I've achieved the first step by setting two collections of controls based on some examples I found online.
So, when the form is loaded, I call this :
Private mcolGroupTxToggle As New Collection
Private mcolGroupBuToggle As New Collection
Private Sub InitializeCollections()
Dim ctl As Control
If mcolGroupTxToggle.Count = 0 Then
For Each ctl In Me.Controls
If ctl.Tag = "txtoggle" Then
mcolGroupTxToggle.Add ctl, ctl.Name
End If
Next ctl
Set ctl = Nothing
End If
If mcolGroupBuToggle.Count = 0 Then
For Each ctl In Me.Controls
If ctl.Tag = "butoggle" Then
mcolGroupBuToggle.Add ctl, ctl.Name
End If
Next ctl
Set ctl = Nothing
End If
End Sub
And on Form_Current event, I call that :
Private Sub OnLoadToggles(mcol As Collection, pcol As Collection)
Dim ctl As Control
Dim btn As Control
Dim strBtn As String
For Each ctl In mcol
'Every button has the same name than the textbox + "b"
strBtn = ctl.Name & "b"
For Each btn In pcol
If btn.Name = strBtn Then
If IsNull(ctl) Then
ctl.Visible = False
btn.Value = False
Else
ctl.Visible = True
btn.Value = True
End If
End If
Next btn
Next ctl
Set ctl = Nothing
End Sub
Everything works well so far, but I'm not sure that's the best way to do it and I figured I would need to repeat some lines in step 2.
Making the distinction between text boxes and buttons in the procedure seems weird, I feel like it should be done prior so that I don't have to do it in every procedure. I also feel like it would be better to loop through each pair of controls (text + button) instead of each control in both collections.
Basically, I'm wondering if it would be (1) better and (2) possible to have something as simple as this :
Private Sub OnLoadToggles(Collection of pairs)
for each [pair of txt and btn]
if isnull(txt) Then
txt.visible = false
btn.value = false
else
...
end if
...
My guess is that I would need to make a public sub where I set a collection of pairs of buttons and text fields based on their tags (there are other controls in my form that need to be left alone) and names, but I'm not sure how, I'm a beginner in VBA.
Any suggestions please ?
-- Edit step 2 --
Thanks to Andre's answer the second part was easier than I thought. I've updated my sample database. So on click events I call this :
Private Sub ClickToggles()
Dim ctl As Control
Dim btn As Control
Dim strBtn As String
Dim strCtl As String
strBtn = Me.ActiveControl.Name
strCtl = Left(strBtn, Len(strBtn) - 1)
Set ctl = Me(strCtl)
Set btn = Me(strBtn)
If IsNull(ctl) Then
btn.Value = True
ctl.Visible = True
ctl.Enabled = True
ctl.SetFocus
Else
ctl.Value = ""
btn.Value = False
ctl.Visible = False
End If
End Sub
It's not perfect but it works. Probably not a good idea to clear the data at this point because misclicks may happen. It would be better to loop through the textboxes right before saving the form and clear values of invisible and/or disabled controls from the collection. I might to that later.
I had to add the .enabled property next to the .visible one because on lostfocus events I was getting an error saying the control was still active so it couldn't make it not visible.
Right now I'm more concerned about the amount of click and lost focus events. I'd rather have some public functions and event handlers dealing with it but it's getting too complicated for me. I'll get back to it when I know more about... everything.
Suggestions are still welcome anyway =) !
Since your control pairs are "paired" by their name anyway, you don't need any fancy constructs, or even the second collection. Just address the matching control directly by its name.
Instead of using If/Else for setting boolean properties, it is usually easier to assign a variable to the property.
Private Sub OnLoadToggles(mcol As Collection)
Dim ctl As Control
Dim btn As Control
Dim strBtn As String
Dim bShowIt As Boolean
For Each ctl In mcol
'Every button has the same name than the textbox + "b"
strBtn = ctl.Name & "b"
' If you have the control name, you can get the control directly:
Set btn = Me(strBtn)
' Using a variable you don't need If/Else
bShowIt = Not IsNull(ctl.Value)
ctl.Visible = bShowIt
btn.Value = bShowIt
Next ctl
' Note: this is not necessary for local variables - they go automatically out of scope
Set ctl = Nothing
End Sub

Content Controls MS Word

I am new to working with VBA and Word 2010.
I have a Word document with some text fields using Content Controls (i.e. rich text control).
I want one of them named (Title) as "testbox" to be a counter of how many times the document has been printed.
I have some code from Excel that works. Is it possible to use this in MS Word? How do I communicate with the Content Control instead of a cell in Excel?
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Cancel = True
Application.EnableEvents = False
ActiveSheet.PrintOut
Range("A1").Value = Range("A1").Value + 1
Application.EnableEvents = True
End Sub
The basic approach of the code you show should work in Word, you just need to look up the appropriate names of the objects, methods and properties. Document_BeforePrint, for example, and ActiveDocument.Print.
Word doesn't have an EnableEvents property so you need to create your own method or methods for turning off the events you define at the Application level. How those methods should look and what they require is part of the discussion about how to use Application level events in Office - part of the VBA Language Reference (https://msdn.microsoft.com/en-us/library/office/ff821218.aspx).
A ContentControl can be picked up by its Title using the Document.SelectContentControlsByTitle method. This returns an array of content controls having the same Title. If you have only the one, then something like this:
Dim cc As Word.ContentControl
Dim ccs as Word.ContentControls
Set ccs = ActiveDocument.SelectContentControlsByTitle("testbox")
Set cc = ccs(1)
cc.Range.Text = Cstr(CInt(cc.Range.Text) + 1)

How do I capture a PowerPoint VSTO Text Changed Event?

I'm developing a PowerPoint C# VSTO add-in. I want to be able to capture a text changed event whenever the Title text of a slide is changed.
How can I attach a custom event handler that will fire whenever the Title text is changed?
Two things: 1) this is in VBA, but should be easily portable to C# and VSTO, 2) The "text changed" thing is a bit tricky. I can get you as far as "are you in a Title box" - the rest is more trival. It has to do with finding original state versus any changes. Probably doable, I just haven't done it.
To hook a selection change in PPT VBA, you'll need one class and one module. In the class, put this:
Public WithEvents PPTEvent As Application
Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
With Sel
If .Type = ppSelectionText Then
Dim sh As Shape: Set sh = .ShapeRange(1)
If sh.Type = msoPlaceholder Then
originalText = sh.TextFrame.Text
Dim placeHolderType As Integer
placeHolderType = sh.PlaceholderFormat.Type
If placeHolderType = ppPlaceholderTitle Then
MsgBox "this is a title placeholder"
End If
End If
End If
End With
End Sub
Name the class "clsPPTEvents". Then in any module, put the following:
Public newPPTEvents As New clsPPTEvents
Sub StartEvents()
Set newPPTEvents.PPTEvent = Application
End Sub
Sub EndEvents()
Set newPPTEvents.PPTEvent = Nothing
Set newPPTEvents = Nothing
End Sub
Press F5 on the StartEvents and that will enable the hook. Press F5 on the EndEvents to disable it.