Align the Caption of an Option Button with the Control

The following example demonstrates the Alignment property used with several OptionButton controls. In this example, the user can change the alignment by clicking a ToggleButton.

To use this example, copy this sample code to the Script Editor of a form. To run the code you need to open the form so the Open event will activate. Make sure that the form contains the following controls:

  • Two OptionButton controls named OptionButton1 and OptionButton2.

  • A ToggleButton named ToggleButton1.

Dim OptionButton1 
Dim OptionButton2 
Dim ToggleButton1 
 
Sub Item_Open() 
 Set OptionButton1 = Item.GetInspector.ModifiedFormPages.Item("P.2").Controls("OptionButton1") 
 Set OptionButton2 = Item.GetInspector.ModifiedFormPages.Item("P.2").Controls("OptionButton2") 
 Set ToggleButton1 = Item.GetInspector.ModifiedFormPages.Item("P.2").Controls("ToggleButton1") 
 
 OptionButton1.Alignment = 0 'fmAlignmentLeft 
 OptionButton2.Alignment = 0 'fmAlignmentLeft 
 
 OptionButton1.Caption = "Alignment with AutoSize" 
 OptionButton2.Caption = "Choice 2" 
 OptionButton1.AutoSize = True 
 OptionButton2.AutoSize = True 
 
 ToggleButton1.Caption = "Left Align" 
 ToggleButton1.WordWrap = True 
 ToggleButton1.Value = True 
End Sub 
 
Sub ToggleButton1_Click() 
 If ToggleButton1.Value = True Then 
 ToggleButton1.Caption = "Left Align" 
 OptionButton1.Alignment = 0 'fmAlignmentLeft 
 OptionButton2.Alignment = 0 'fmAlignmentLeft 
 Else 
 ToggleButton1.Caption = "Right Align" 
 OptionButton1.Alignment = 1 'fmAlignmentRight 
 OptionButton2.Alignment = 1 'fmAlignmentRight 
 End If 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.