Cannot grab list box using Visual Basic in a Word document - vba

My goal for what I am trying to do overall is have one box on a document that holds a database type and when someone select that database type the document will hide/show certain sections.
Well my issue right now is that I cannot seem to grab the the list box ContentControl I created.
I have a listbox ContentControl with a tag boxDB.
Currently this is all that I can get
Sub ListBox_AfterUpdate()
Dim box
Set box = ActiveDocument.SelectContentControlsByTag("boxDB")
End Sub
I have tried several things with box to try and get it to have data. My first assumption was that since it is a collection I would just find the first item by using the first index of the items..but it always says that there is no item at that index.
Is there another method I should be using to grab the value that was set?

In order to access the control and it's features by tag, read the reference documentation for SelectContentControlsByTag. This shows that the returned object is a collection of controls that match the tag, so clearly you might have several controls with the same tag string.
You will help yourself quite a lot by always using Option Explicit in all your code and by specifically declaring each variable with a type. That way you can use the Intellisense feature of the VBA Editor to reveal what methods and properties are available as you write your code.
Assuming that your ContentControl is already established with the tag (and that you only have one control with that tag
Option Explicit
Sub SetupListbox()
Dim theControls As ContentControls
Dim box As ContentControl
Set theControls = ThisDocument.SelectContentControlsByTag("boxDB")
Set box = theControls.Item(1)
box.DropdownListEntries.Add "Red"
box.DropdownListEntries.Add "Green"
box.DropdownListEntries.Add "Blue"
box.DropdownListEntries.Add "Yellow"
box.DropdownListEntries.Add "Orange"
End Sub
Sub GetSelectedItem()
Dim theControls As ContentControls
Dim box As ContentControl
Set theControls = ThisDocument.SelectContentControlsByTag("boxDB")
Set box = theControls.Item(1)
Debug.Print box.Range.Text
End Sub

Related

MS ACCESS VBA - best way to change all properties values of a form's textboxes at once

I have a form with 3 textboxs and i want to change their properties values according to some events.
My idea is to set these properties to a global form's variables in order to use them whenever i want with a function or module (this depends on your suggestion).
Assuming that i want to change all textboxs.enabled property by pressing a button
and i want to change another different property if needed in the future (like bordercolor),
my example is this:
in the form VBA,
Option Compare Database
Option Explicit
Private Const AllTxtboxes = *** all txtboxes ***
in the VBA button
Private Sub button1_Click()
AllTxtboxes.enabled = True
End sub
Thanks in advance.
You need to loop trough all controls and identify those you want to change. The problem is that each control type got their own properties, so you need to make sure you are in the right one.
And for that, there is a property common to all controls named Tag
There, you can specify a value and check it. It's really helpful to select only what you want.
I made a simple form with 6 textboxes:
Note that in design view, properties panel at right, tab others, last property is Tag (sometimes it comes as aditional info or something like it).
There, I typed 99 but only on textboxes 2, 4 and 6, because those are the textboxes I want to handle (that would be kind of your array of textboxes)
I added a command button to change forecolor of those textboxes to red when clicked. My code is:
Private Sub CMD_CHANGE_FONT_COLOR_Click()
Me.Painting = False
Dim MyControl As Control
Dim MyTxt As TextBox
For Each MyControl In Me.Controls
If MyControl.Tag = 99 Then
'we set MyTxt to MyControl so we can use Intellisense, not really needed, but it makes coding easier
Set MyTxt = MyControl
MyTxt.ForeColor = vbRed
Set MyTxt = Nothing
End If
Next MyControl
Me.Painting = True
End Sub
When the form loads, you will see:
But after click on command button, the forecolor of those 3 textboxes will be red:
There you go. So everytime you want to do something with that array of textboxes, you can use this code to loop and change the properties you want.

How to use UserForm CheckBox to change Control Content CheckBox in Word Document?

I created an UserForm with a checkbox 'cbxYes' and a Content Control checkbox 'docCbx' in the Word document. I want to be to check off the checkbox 'cbxYes' in UserForm that then changes the Content Control checkbox in the Word document.
So the input is from the UserForm checkbox and the output is the Content Control checkbox.
I have tried multiple searches on how to do this, but I could not find exactly what I needed. Most of the searches were related to Excel. And honestly, I don't know what I'm doing. Please. The correct help is greatly appreciated.
Private Sub cbxYes_Click()
Dim oCC As ContentControl
If cbxYes.value = True Then
cbxYes.value = "True"
ActiveDocument.docCbx_Yes.value = True
Else
cbxYes.value = "False"
ActiveDocument.docCbx_Yes.value = False
End If
End Sub
The error I got was:
run-time error '438': Object doesn't support this property or method.
The code shown in the question would be for ActiveX checkboxes, rather than content controls. (Just to make things really complicated, Word also has checkbox form fields that need yet another code syntax.)
There's no way to refer directly to a content control name via the Document object - it must be done over the ContentControls collection. Content controls can be assigned a Title and/or a Tag in the Properties dialog box.
More than one content control can have the same name or title, which makes the code a bit complicated. Querying Document.ContentControls("Title") returns a collection (all the content controls with that title).
If you know which one, then it's possible to pick it up directly (rather than working through a collection) using the Item method, specifying that it's the n content control (the index in the order of content controls). This is usually used when one knows there's only the one.
Note, please, also, that in order to "check" or "uncheck" a content control checkbox the Checked property should be use. So:
Private Sub cbxYes_Click()
Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("docCbx").Item(1)
If cbxYes.value = True Then
'cbxYes.value = "True" not needed
occ.Checked = True
Else
'cbxYes.value = "False" not needed
oCC.Checked = False
End If
End Sub
Assuming that "docCbx" is the title of the content control you can replace
ActiveDocument.docCbx_Yes.value = True
by
For Each oCC In ActiveDocument.SelectContentControlsByTitle("docCbx")
If (oCC.Type = wdContentControlCheckBox) Then
oCC.Checked = False
End If
Next
and the equivalent, but using False, for the other branch of your code. The above code will update all checkbox type content controls with that name (the names do not have to be unique within a document), except controls that have been marked as "Contents cannot be edited" - they will remain unchanged. It deals with the situation where there aren't any content controls with that name by doing nothing.
If "docCbx" is the value of the Tag, you would need the following instead:
For Each oCC In ActiveDocument.SelectContentControlsByTag("docCbx")
If (oCC.Type = wdContentControlCheckBox) Then
oCC.Checked = False
End If
Next
If docCbx is something else, I would suggest that instead you give Titles and/or Tags to your content controls and use the above approach. Otherwise, you should modify your question to state precisely how you are naming the control "docCbx".
You get the error because adding content controls to a document does not create new members of the document object, whereas adding a form control to a user form does create new members of the Form object. (Documents do in fact have a mechanism that works more like the Form object but it has been deprecated for a long time.)

Populating a dynamically added listbox from dynamically added combobox selection

The userform has 3 columns, a combobox (list of vendors), and listbox (list of vendors' product / service) and a textbox with the price of the selected product / service.
Example of menu (3rd column item not added)
The user presses a button to add a row, which populates within a scrollable frame a new row of the 3 columns. My goal is to have the listbox's options change according to the selection in the combobox on that row.
I have a listener (see my previous post on combobox(number)_change() to see how this works) which is the following ...
Private Sub ControlHandlerCollection_ComboBoxChange(ComboBox As MSForms.ComboBox)
MsgBox ComboBox.Name
End Sub
I wrote this to test if the change sub would work for ComboBox #87 for example. However, I don't know how to tell the same numbered listbox to change its list options according to the selection of ComboBox#
If I had a listener specifically for ComboBox1_Change() then I'd simply write the change for ListBox1 within it, but I'm writing this for a dynamic 'infinite' amount of ComboBoxes.
My current plan of attack would be to write within the sub in the code box above a block of code to parse the result of ComboBox.Name to grab the number as the result would be for example "ComboBox15" I could write a variable assigned to "ListBox" + (ComboBox.Name - "ComboBox") to call ListBox(number) however I'm not sure I can actually then call a variably based name of a listbox.
OK my father helped me come to this solution. It's a little static, but it works perfectly for what I need.
Had to edit the vendor data into multiple sheets and used the Define Name tool under Data to name the range. The listbox changes as per the selection of combobox.
Dim listBoxName As String
listBoxName = "myList" & ComboBox.Tag
Dim rangeName As String
rangeName = "company_1"
Select Case ComboBox.value
Case "Company 1"
rangeName = "company_1"
Case "Company 2"
rangeName = "company_2"
End Select
Dim listBox As Control
Set listBox = Me.Controls(listBoxName)
listBox.RowSource = rangeName
The above code was added to the private sub mentioned in the original thread.

Word 2013 - Macro Reference in .docx file

I have a pretty large template (in terms of macros) and I've created a new module that is called when the user does a specified shortcut on the keyboard.
The Macro does nothing more but call "ThisDocument.HideComboBoxes".
"HideComboBoxes" is a Sub in ThisDocument, which does the obvious: hide all combo boxes in the document.
The code works fine as long as I'm working with the template.
If I render a new document out of it (a docx file) the shortcut does not work anymore (which is kinda obvious because the ThisDocument is now empty for the document).
How do I access the template's ThisDocument from within the document or how can I hide the combo boxes from within the docx?
Here's the code for hiding the comboboxes in the template's ThisDocument:
Sub HideComboBoxes()
Me.ComboBoxConfi.Width = 1
Me.ComboBoxConfi.Height = 1
Me.ComboBoxState.Width = 1
Me.ComboBoxState.Height = 1
End Sub
Thanks in advance
You need to put the code in a "normal" module. This will require changing the calls to the ActiveX controls, which is a pain, but works.
Assuming the ActiveX controls are managed on the document surface as InlineShapes a call would look like this:
Dim cbConfi as Object 'or as MsForms.ComboBox to get Intellisense
Set cbConfi = ActiveDocument.InlineShapes([indexvalue]).OLEFormat.Object
cbConfi.Width = 1
If you don't want to rely on the index value (position of the control in the InlineShapes collection) you can select the ActiveX control and assign a bookmark to it. Or you can loop the collection of ActiveX controls, check the Type and, if it's a OLE object, check whether the Class type is MSForms.Combobox and then via OLEFormat.Object.Name whether its the right one.

Programmatically access controls on GroupBoxes that are on multiple Tab Pages

How can you programmatically access controls on Tab Pages?
My code has many properties that I want to allow the user to initially set when the program starts. These properties will be used to define an Excel chart like title name, font colors, font size, chart position and size, X & Y series and many others properties. The program produces many charts each of which have their own set of properties. Each chart has the same set of properties but different values.
My solution using Form1 Designer:
I Placed the TabControl1 control from the VS 2010 toolbox on Form1.
I created 19 TabPages on TabControl1
On each TabPage I created multiple GroupBoxes for organization.
Finally, I placed multiple controls like RadioButtons, TextBoxes, and Spinners in each of the GroupBoxes.
All of the above was done at design time.
Bad Example:
Dim TC1 As TabControl = TabControl1.TabPages.Item(1)
This is a bad example but I’m thinking I need to do something like this, i.e. before I can reach the actual user inputs via the RadioButtons, TextBoxes, or Spinners child controls I need to tell the complier the name of the TabControl1 and each TabPage contained in it. I’m not sure about each of the GroupBoxes that contain groups of RadioButtons and etc.
My Question: How do you programmatically access controls located on multiple GropBoxes located on each of the Tab Pages? I want to retrieve all the user input properties values on each of many TabPages and apply them when I create the Excel Charts.
A TabControl is just a fancy way to organize controls on the same form. The point is that they are on the same form! You probably know that you use Me to access the current form, then .ControlName to access the control. Consider a TabControl with two tabs and on each tab there is a TextBox. These TextBoxes are on the same form, so you still need to use
Me.TextBox1.Text = "Text 1" ' the same as TextBox1.Text = "Text 1", but I like to be explicit
Me.TextBox2.Text = "Text 2"
This you already knew, as your problem stemmed from this behavior. However, there are some tricks to help you. Consider a TabControl with two tabs, and on page 1 there is a control named tc1TextBox1 and on page 2 tc2TextBox1. I'm using the prefix to distinguish between the pages. An additional TextBox on page 1 would be named tc1TextBox2 etc., then say you wanted CheckBoxes, you could have tc1CheckBox1 and tc1CheckBox2 etc. The way I would access them is with LINQ
Define an extension method in a module
Imports System.Runtime.CompilerServices
Public Module ExtensionMethods
<Extension()> _
Public Function ChildControls(Of T As Control)(ByVal parent As Control) As List(Of T)
Dim result As New List(Of Control)
For Each ctrl As Control In parent.Controls
If TypeOf ctrl Is T Then result.Add(ctrl)
result.AddRange(ctrl.ChildControls(Of T)())
Next
Return result.ToArray().Select(Of T)(Function(arg1) CType(arg1, T)).ToList()
End Function
End Module
This method returns you all the Controls inside parent, and all Controls inside those, and so on, recursively. It also gives you an IEnumerable List(Of Control) in contrast to Control.Controls() which is not IEnumerable and only returns controls in the control, not in its containers.
Using LINQ, you can filter the results of ChildControls by Type or Name. For instance, the code
Dim textBoxes = Me.ChildControls(Of TextBox)()
returns all the TextBoxes in Me. Let's take it a step further:
Dim textBoxes = Me.ChildControls(Of TextBox)().Where(Function(tb) tb.Name.StartsWith("tc1"))
returns all the TextBoxes with the prefix "tc1". So you see you can set up the controls on your form to better group them at runtime. Here's an idea specific for Excel sheets
Dim xlBook As Excel.Workbook
For i As Integer = 1 To 20
Dim nameString = "txtSheetName" & i.ToString() ' TextBoxes named txtSheetName1, txtSheetName2, etc.
xlBook.Sheets(i).Name = Me.ChildControls(Of TextBox)().Where(Function(tb) tb.Name = nameString).First().Text
Dim enabledString = "chkEnabled" & i.ToString() ' CheckBoxes named chkEnabled1, chkEnabled2, etc.
xlBook.Sheets(i).Enabled = Me.ChildControls(Of CheckBox)().Where(Function(tb) tb.Name = enabledString).First().Checked
'etc.
Next
which will loop over your tab control indices, and set properties for each corresponding excel sheet. This code should give you a good idea of a direction you can take.