Word VBA: Accessing clicked ActiveX Checkbox - vba

I have multiple ActiveX checkboxes in my document in a table. They're all calling the same function.
With Selection.Cells(1).RowIndex and Selection.Cells(1).ColumnIndex I can find out the table cell of the checkbox.
Is it possible that I can get the value of the clicked checkbox aswell?
I could only find this code: ActiveDocument.Shapes(1).OLEFormat.Object.Value. But this code is referencing an individual checkbox.
I need to reference the checkbox that was just clicked.
Is that possible and if yes how?

Try something like:
Dim c As Cell
Set c = Selection.Cells(1)
Debug.Print c.Range.InlineShapes(1).OLEFormat.Object.Value

Related

How to select an ActiveX Option/Radio Buttons in Form Controls

I have a Form Control with ActiveX Radio/Option Buttons in it.
The Form Control name is Side and contains Option/Radio Buttons with names xOption, oOption, and randomSide.
How would I be able to create a Macro that would allow me to set the radio buttons to a certain value upon opening the workBook. Recording a Macro of me clicking options results in a blank Macro. I've Already tried:
ActiveSheet.Shapes.Range(Array("Side")).Select
ActiveSheet.Shapes.Range("xOption").OLEFormat.Object.Value = 1
But this gives me error 1004 and other codes give me error 91. I'm really new to VBA so if I look stupid you know why.
Try something like this, using Worksheets instead of ActiveSheet:
Private Sub Workbook_Open()
Worksheets("your sheet name here").OLEObjects("xOption").Object.Value = 1
End Sub
As you want it to be selected after opening the sheet. Place this on ThisWorkbook.
You may try something like this...
ActiveSheet.OLEObjects("xOption").Object.Value = 1

Select linked cell, cell next to checkbox, or get the checkbox name

I have a document full of Checkboxes and I dont want to write specific VBA code for each checkbox because the file size needs to stay relatively small. What I am trying to do with my code is when the checkbox is checked, it automatically selects the cells next to it(not hard coded in using "Range") and then perform the rest of the programed VBA function.
How do I either get the name of the checkbox, select the linked cell, or select the cell next to the checkbox using some kind of "offset" property? I am completely stumped!
Thanks for your help in advance.
Use Form Controls instead of ActiveX Controls for Check Box.
Following code will not be work with check box from ActiveX Controls. Also, you need to assign macro to the checkbox, simply trying to run this code from VBEditor will give error.
Assuming all the checkboxes are on same sheet, select all your checkboxes and assign them same macro, something like this
Sub checkBoxHandler()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
MsgBox shp.Name 'Name
MsgBox shp.TopLeftCell.Offset(1).Address ' 1 Rows below checkbox
ActiveSheet.Range(shp.ControlFormat.LinkedCell).Select ' Select linked cell.
Set shp = Nothing
End Sub
here Application.Caller helps VBA to identify which checkbox is being clicked.

give a combobox in Excel a default selected value with VBA

I have a combobox in Excel created as the picture suggests, and it has a range associated to it.
How can I give it a default value from this range, through VBA?
ActiveSheet.Shapes("DropDown1").ControlFormat.Value = "Germany"
I tried the above code, but it doesn't work. I believe the syntax is not correct. Can you please help?
Thank you!
You can use the DropDown type, it doesn't show up in the intellisence when you type.
Dim DropDown1 As DropDown
Set DropDown1 = ActiveSheet.DropDowns("DropDown1")
DropDown1.Value = 1
The value is the index of the dropdown, so 1 is the first in the list etc. Use 0 to have no value selected. Also make sure the name "DropDown1" is the correct name for the control, if you right click the control in the excel sheet you'll see the name for the control to the left of the function bar.

Get dropdown value in VBA and get the name of the dropdown...nowhere to be found?

I created a dropdown by dragging the combo box onto my sheet from the UserForm toolbar. I assigned some values to it from some cells in the book. Now I want some VBA code to access the selected dropdown item's value in the form of a string.
My dropdown contains only text.
Also how do I find the name of this newly created dropdown (it's nowhere in the properties!)?
Dim dd As DropDown
Set dd = ActiveSheet.DropDowns("Drop Down 6")
Set r = Sheet2.Range(dd.ListFillRange)
Set ddValue = r(dd.Value)
NOTES:
DropDown is not a visible class. You
just use it and it works.
To find the name of the dropdown
CONTROL (not userform) just look at
the name box in the top left corner of your screen just above column A.
It says the name of the control when
you right click on your control.-
Sheet2 is where the dropdown list is
populated. So wherever your list data
is.
Hope that helps you all.
Here's how you get the String without needing to know the name:
Dim DD As Shape
Set DD = ActiveSheet.Shapes(Application.Caller)
MsgBox DD.ControlFormat.List(DD.ControlFormat.ListIndex)
This is a clunky way of doing it but it should work:
Dim o As Object
For Each o In Worksheets("Sheet1").Shapes
MsgBox o.Name
Next o
There is also a hidden DropDowns collection member of the Worksheet object that you could iterate over. This will find items inserted from the Forms toolbar but won't find items inserted from the Control Toolbox toolbar
Lance Roberts was almost there. If you don't know the name of the drop down that calls the sub, use this:
Dim dd as DropDown
Set dd=ActiveSheet.Shapes(Application.Caller).OLEFOrmat.Object
Dim ddVal as String
ddVal=dd.List(dd.ListIndex)
I used this to create a generic sub for a form with many drop downs.

How to get selected text in VBA

I have a macro that changes the selected text, and I have it assigned to a button.
It works perfectly when i run it directly from visual basic, but when I click the button, the button gets the focus and my text is no longer selected so the macro change the selected element to (button).
How can I select the text and run the macro by clicking on the button and still have the text selected?
The way to do this is to set the set the TakeFocusOnClick property of the CommandButton to False. Here are is the code I use.
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
Is the button embedded in the document? You may need to put it on a form that loads on top of the Word window or in a menu/toolbar, so that clicking it does not affect the selection in the document itself.
Edit:
I think you can use Application.Selection.Previous to get at what you need. You could use this to restore the selection after the click event, or to act upon that section of the document, or both.
I assume that this is available in previous versions of Word, but have only confirmed its presence in 2007.
You need to change TakeFocusOnClick to "False" in the Button Preferences.