View Single Post
 
Old 08-29-2021, 04:19 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

In the userform code


Code:
Option Explicit

Private Sub btnSave_Click()
Dim oRng As Range
    If cmbColour.ListIndex >= 0 Then
        With ActiveDocument
            On Error GoTo lbl_Exit
            If .Bookmarks.Exists("My_Colour") = True Then
                Set oRng = .Bookmarks("My_Colour").Range
                oRng.Text = cmbColour.Value
                oRng.Bookmarks.Add "My_Colour"
            End If
        End With
    Else
        MsgBox "Select Colour", vbCritical
    End If
    Unload Me
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub

Private Sub UserForm_Initialize()
Dim i As Integer
Dim sColour As String
    With ActiveDocument
        If .Bookmarks.Exists("My_Colour") = True Then
            sColour = .Bookmarks("My_Colour").Range.Text
        End If
    End With
    With cmbColour
        .AddItem "Red"
        .AddItem "Green"
        .AddItem "Blue"
        For i = 0 To .ListCount - 1
            If sColour = .List(i) Then
                .ListIndex = i
                Exit For
            End If
        Next i
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote