Log in

View Full Version : Changing Captions of Ceckboxes in a Word doc



wolle271
06-21-2015, 05:53 AM
Cheers guys,

I'm not getting the hang of how to change the caption of an active-x checkbox via vba... would be great if one of you could help me out!

Best regards.

gmaxey
06-21-2015, 01:02 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oIls As InlineShape
'Basically open a new document, insert a ActiveX checkbox:
ActiveDocument.InlineShapes(1).OLEFormat.Object.Caption = "Checkbox"
'More advanced"
For Each oIls In ActiveDocument.InlineShapes
If oIls.Type = wdInlineShapeOLEControlObject Then
If oIls.OLEFormat.ClassType = "Forms.CheckBox.1" Then
oIls.OLEFormat.Object.Caption = InputBox("Current caption is: " & oIls.OLEFormat.Object.Caption _
& vbCr + vbCr & "Type a new caption", "Caption", oIls.OLEFormat.Object.Caption)
End If
End If
Next
lbl_Exit:
Exit Sub
End Sub

Jay Freedman
06-21-2015, 01:17 PM
There are two ways to handle this, depending on whether the caption should depend on the checked/unchecked state of the checkbox or on something else (maybe the click of a different control, the setting of a dropdown, etc.).

If you just want it to reflect whether or not the box is checked, then it's simple. With the Design Mode button turned on, right-click the checkbox and choose View Code. That opens the macro editor to the document's ThisDocument code and automatically inserts the first and last lines of the box's Click event handler. The first line includes whatever Name you assigned to the checkbox in its Properties dialog (by default, the word "Checkbox" followed by a sequence number). Between those lines you can insert the code to assign a string to the .Caption member of the checkbox, something like this:

Private Sub cbxCheckMe_Click()
If cbxCheckMe.Value = True Then
cbxCheckMe.Caption = "checked"
Else
cbxCheckMe.Caption = "unchecked"
End If
End Sub


If you need to work with the box's caption from an ordinary module instead of from an event handler in ThisDocument, then things get a little messy. Assuming that the ActiveX control was inserted in-line with text, then it's a member of the document's InlineShapes collection -- but you won't know which index to use to address it. For that, you can use this helper function to search the collection for an ActiveX control whose name matches (non-case-sensitive) the name that you pass in the argument.

Function GetActiveXControl(cbName As String) As InlineShape
Dim ils As InlineShape
Dim found As Boolean
For Each ils In ActiveDocument.InlineShapes
If ils.Type = wdInlineShapeOLEControlObject And _
LCase(ils.OLEFormat.Object.Name) = LCase(cbName) Then
found = True
Exit For
End If
Next

If found Then
Set GetActiveXControl = ils
Else
Set GetActiveXControl = Nothing
End If
End Function
To use that function, you can use code like this (run from inside VBA or from a button on the Quick Access Toolbar).

Sub test()
Dim cbox As InlineShape
Set cbox = GetActiveXControl("cbxCheckMe")
If Not cbox Is Nothing Then
With cbox.OLEFormat.Object
If .Value = True Then
.Caption = "checked"
Else
.Caption = "unchecked"
End If
End With
End If
End Sub

wolle271
06-29-2015, 03:14 AM
Hey guys, thx for your answers!

im addings those checkboxes via this line:


Set checkBox = objDoc.InlineShapes.AddOLEControl(ClassType:="Forms.Checkbox.1", Range:=table.Cell(iterator, 2).Range)

I was hoping I could access the checkbox's attributes by "checkbox.attribute" but I couldnt assign any values, although the editor told me there's a ".caption" attribute.
I forgot to mention, that I dont want the checkbox to show any caption, I only need the checkbox.

Jay Freedman
06-29-2015, 09:08 AM
When you add the checkbox with the AddOLEControl method, the variable "checkbox" gets an InlineShape object. Then, as in the last piece of code in my previous post, you have to drill down to the checkbox.OLEFormat.Object.Caption property. To remove the caption, set that property to the empty string:


CheckBox.OLEFormat.Object.Caption = ""