You can read and set the state of dialog box controls at any time. Typically, you set initial conditions before the dialog sheet runs or use a procedure assigned to the DialogFrame object to set controls when the dialog box starts. You will usually read controls when a specific event occurs in the dialog box or when the user dismisses the dialog box.
The ChangeColor procedure runs whenever the user clicks either Test or OK. This procedure saves the current color of the selected object and then reads the option buttons to determine the new color.
Sub ChangeColor() Dim selInterior As Interior With DialogSheets("ChangeColorDialog") Set selInterior = Selection.Interior saveInteriorColor = selInterior.ColorIndex selInterior.ColorIndex = colors(GetOptionIndex(.OptionButtons)) End With End Sub
The GetOptionIndex function scans the option buttons and returns the index value of the button that's turned on (the button's ordinal value in the button tab order). This index number is used to return the correct color value from the colors array, as in the following example.
Function GetOptionIndex(opBtns As OptionButtons) For Each ob In opBtns If ob.Value = xlOn Then GetOptionIndex = ob.Index Exit Function End If Next End Function
The dialog box also has an Undo button that causes the UndoColor procedure to run. This procedure saves the current color and then resets the color to the saved value.
Sub UndoColor() With Selection.Interior temp = .ColorIndex .ColorIndex = saveInteriorColor saveInteriorColor = temp End With End Sub