View Single Post
 
Old 01-28-2015, 07:20 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,142
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

You need to read back the value of the variable and compare it with the items in the list, then set the list index. e.g.

Code:
Option Explicit
Private oVars As Variables
Private oVar As Variable
Private i As Long

Private Sub UserForm_Initialize()
Dim listEntries1(3, 1) As Variant

    Set oVars = ActiveDocument.Variables
    With Me
        .ComboBox1.ColumnCount = 2
        .ComboBox1.ColumnWidths = "50;20"
        listEntries1(0, 0) = "This"
        listEntries1(0, 1) = 1
        listEntries1(1, 0) = "is"
        listEntries1(1, 1) = 2
        listEntries1(2, 0) = "a"
        listEntries1(2, 1) = 3
        listEntries1(3, 0) = "Test"
        listEntries1(3, 1) = 4
        .ComboBox1.List = listEntries1

        .ComboBox1 = ComboBox1.List(0)
        'Read back the value of the variable (if present)
        For Each oVar In oVars
            If oVar.Name = "var1" Then
                For i = 0 To .ComboBox1.ListCount - 1
                    'Compare the value with the list items
                    If Trim(.ComboBox1.List(i)) = Trim(oVar.Value) Then
                        'Set the listindex to the value of i
                        .ComboBox1.ListIndex = i
                        Exit For
                    End If
                Next i
                Exit For
            End If
        Next oVar
    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