Hopefully an easy one.
I've got a Word document littered with Checkboxes. Is there any way to access these checkboxes as a collection I can loop through and check/set properties such as ID and Value?
For reference, I've already tried the ActiveDocument.ContentControls collection; it's empty.
I've also tried ActiveDocument.Fields. This actually contains the same number of checkboxes as there are on the page, but I can't cast it to a Checkbox to access the properties I need.
One final question, is it possible to dynamically assign an event handler to the click?
Thank you in advance
The collection of all ActiveX fields embedded in your document (let's say its name is ThisDocument) is the collection ThisDocument.Fields. The items of this collection are objects of class Field.
To query the value of the objects in this collection, you'd use something along the lines of:
ThisDocument.Fields.Item(1).OLEFormat.Object.Value
To add code to the event handlers, just right-click on the ActiveX control, and then select "View Code" from the context menu. You'll get the Click event handler:
Private Sub CheckBox1_Click()
' empty if event handler not set, VBA code otherwise '
End Sub
To insert code dynamically, you need to manipulate the Code Module directly, and add the event handlers line by line. This might be tricky but, basically, you'd do something like:
ThisDocument _
.VBProject _
.VBComponents(ThisDocument.CodeName) _
.CodeModule _
.InsertLines(<<Line>>, <<String>>)
where <<Line>> is where you want to insert (number), and <<String>> is the text that you want to insert. Be careful not to insert in the middle of an existing Sub, Function or custom type definition (obviously).
Related
I'm trying to display a listbox in a UserForm from a separate sheet called "Fields". The problem is, the list will not display. It shows as a drop down arrow next to the cell, but not in the userform like I'm wanting.
Private Sub UserForm_Activate2()
On Error Resume Next
Me.ListBox2.Clear
For Each element In gFieldsListArr
Me.ListBox2.AddItem element
Next element
UserForm_initialize2
End Sub
Private Sub UserForm_initialize2()
For Each element In Split(gCellCurrVal2, ",")
For ii = 0 To ListBox2.ListCount - 1
If element = Me.ListBox2.List(ii) Then
Me.ListBox2.Selected(ii) = True
End If
Next ii
Next element
End Sub
TL;DR: You can't rename event handlers or change their member signature in any way*, because the correct member definition is defined by the event, not its handlers.
Watch the dropdowns at the top of the editor as you navigate between procedure scopes:
Whenever the left-side dropdown says (General), you're not inside an event handler.
Contrast with:
The left-side dropdown is listing all available interfaces and event sources; to handle the events of a form, you must pick UserForm from that dropdown, and then pick a member from the right-side dropdown.
When you do this, the VBE creates the procedure stubs for you, with the correct name and signature every time.
Whenever you navigate to what's intended to be an event handler and the left-side dropdown says (General), you're looking at dead code that isn't responding to any events.
* You may change the accessibility from Private to Public, but invoking an event handler directly is a design smell so there shouldn't be a need to do that. You may change the parameter names, but not their type; renaming handler parameters is a rather surprising thing to do though, and best avoided too. So yeah, best not change these member signatures in any way.
I need a Userform to call another Userform that fills text boxes and checkboxes that reads from approx 10 cells from workbook. Basically it is recalling properties of a product that the user can edit, some are text based and some are yes/no (true/false). How do I connect those Userform properties?
You can use Userforms like a class, and declare and instantiate them with New UserFormName. However, they're also a class that always has an instance in the workbook, that you can treat as though it were already declared with New. If you want to pull live information that is already inside it, you'll want to use that one.
For example, if the userform is called "Bacon", and contains some methods that do what you want(Solve_Everything_Click), and some internal data that has the information you need declared at the top(Public MyName As String, TheAnswer As Boolean), you can call it from another sub/function, in userform or out, as long as the code is in the same workbook, or references the book with the userform.
You can access the global instance of it, in the same workbook, by using its name, and accessing the things inside of it with ., keep an eye on the context help that comes up when you press ., which will tell you other methods and variables you might pull from it.
Sub PokeAtIt()
Bacon.TheAnswer = True
MsgBox Bacon.MyName & " Has the answer: " & Bacon.TheAnswer
Bacon.Solve_Everything
End Sub
Back in the old days of VBA, we used to be able to directly access controls on a form by name. I'm a little perplexed by how to do this in VBA 2010 (or if it's even possible any longer).
Say I have a ribbon control with a dropdown list called "channelList", and elsewhere on the ribbon I have a textbox called "labelText". Each of these items has a unique callback function - when I type something into labelText, its callback fires, and when I select an item from the channelList listbox, its callback fires with the list item passed as an argument.
Where I'm stumped by is how I would update the labelText textbox contents with 'something' from within the channelList callback.
Is there a way to directly access the textbox control from within the listbox callback, or must I generate some sort of event? What method or function would I use to set the text value for the control? (Do I need to cast the IRibbonControl object to something?)
The solution was a combination of an answer and the comments, so here goes:
Add a "getText" callback subroutine name to the Ribbon XML, specific to the "labelText" editbox.
editBox id="labelText" label="Text:" sizeString="xxxxxxxxxx"
onChange="TextboxCallback" getText="UpdateTextBoxText"
screentip="Channel label"
supertip="Limited to 10 characters. Press Enter once finished typing."
In the combo box callback function, set the desired editbox text to a global and call Ribbon.InvalidateControl with "labelText" as the argument.
MyRibbon.InvalidateControl "labelText"
Implement the editbox callback, passing a handle to the ribbon and another variable ByRef to contain the 'stuff' to be updated. Use the global to update the control's text via the ByRef argument.
Sub UpdateTextBoxText(control As IRibbonControl, ByRef returnedVal)
Select Case (control.id)
Case "labelText"
returnedVal = LabelText
End Select
End Sub
I guess this is "progress". labelText.Value = "blah" is far too simple.
Take a look at the Ribbon.Invalidate method. When you "invalidate" the ribbon you basically reset it, and in that moment you can set the properties of controls on the ribbon based on whatever you can track in that moment. In order to do so, you need to have Control.GetEnabled, GetVisible, etc., subroutines in your VBA.
You can also invalidate individual controls with Ribbon.InvalidateControl.
Here's a link that might help: http://sourcedaddy.com/ms-excel/resetting-controls.html
What I am trying to achieve is for a combo box (Combo_sf) selection to dictate the form in the subform control (sf_record) I have about 10 forms, their names are in the combo box data. I am new to VBA and am not sure if my approach is right:
Private Sub Combo_sf_AfterUpdate()
Dim strLoadTable As String
strLoadTable = "Form." & Me.Combo_sf.Value
MsgBox strLoadTable
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
End Sub
I have placed this in the combobox's after update event but when I make my selection nothing happens in the form. Am I approaching this right or would another way work better?
Your approach should work. I put a combo box named cbxSubform on my main form and added one line of code to its AfterUpdate() event handler...
Private Sub cbxSubform_AfterUpdate()
Me.mySubform.SourceObject = Me.cbxSubform.Value
End Sub
...and changing the selection in the combo box switches the subforms immediately. Are you sure that the AfterUpdate() code for your combo box is actually firing? (You could add a MsgBox or a Debug.Print to check.)
It could be this line which is tripping you up:
strLoadTable = "Form." & Me.Combo_sf.Value
What is your form object called? If your form is called Form.myTableName it could be the . that is throwing it out, try setting it to a form without a dot in its name.
In this line, it seems the code attempts to change the SourceObject property of a Form object.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
However, SourceObject is a property of a subform control, not the form contained in that control. So if the subform control is named sf_record, do it this way.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.SourceObject = strLoadTable
Also, if the after update procedure runs from [frm_Mnu_Manage Configuration Settings], you can use Me to refer to the form.
Me!sf_record.SourceObject = strLoadTable
Finally, if Me.Combo_sf.Value is the name of a form, you don't need to prefix its name with "Form.". It worked either way in my test, but I would just leave off "Form.".
strLoadTable = Me.Combo_sf.Value
I am trying to create a number of checkboxes on a UserForm after reading all the non-empty rows in an excel sheet. That means these checkboxes have to be created in run-time. I also want to put a CommandButton on the UserForm. What I want is that once the user presses this CommandButton, the code should be able to send to a subroutine the information on which checkboxes are checked and what their names are.
Could anyone help me with problem.
Instead of trying to dynamically create checkboxes on a userform (which I'm not even sure is possible) consider using a listbox with a ListStyle of fmListStyleOption and with MultiSelect turned on with fmMultiSelectMulti
Populate the Listbox using the AddItem Method
For i = 0 to 9
Me.lbxDivisions.AddItem
Me.lbxDivisions.List(i) = "Checkbox " & format(i)
Next i
And determine which items are checked via the Selected property:
For i = 0 To lbxDivisions.ListCount - 1
If lbxDivisions.Selected(i) Then
MsgBox "Item " & Format(i) & " is selected and has value " & lbxDivisions.List(i)
End If
Next i
You can programmatically add form controls (check boxes, listboxes, etc) to userforms. From within the form's code module,
Me.Controls.Add "Forms.CheckBox.1", "CheckBox1", True)
From any other code module, just reference the form by name, instead of Me, e.g.,
MyUserForm.Controls.Add "Forms.CheckBox.1", "CheckBox1", True)
Personally I would favor using a more dynamic control (like a list box or combobox) unless your task absolutely requires you to use check boxes. With dynamic controls you need to manage their size, location relative to other controls, resize the userform (if necessary), etc., and although it's kind of possible to add event handling to these controls (see here), that's really limited (e.g., if you expect you need to add 10 check boxes each of which do a different thing, you need to pre-write 10 check box subroutines. If you create 11 check boxes but only 10 pre-written routines, the last check box won't do anything. It would be easier to just create all the check boxes when designing the form, and then programmatically set them to Visible=True or Visible=False as circumstance requires.
So, I'd favor using a dynamic control like a listbox or combobox, but it is possible to add form controls like checkboxes at run-time, if you must.