|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Selected items in Combobox remain the same when closing and reopen document
Hello there..
I am not a programmer but like to program a small project with vba. I have a Userform1 which contains a ComboBox1. When I now select a value in the combobox in my userform it fills the value to the variable in my word document. When I now hide the form with "Hide Me" it hides the userform and when I then reopen the userform with the Button in the word document all the previously selected items and entered textes in textboxes remain the same. So far so good. But when I save the document close it and reopen it again all the values selected are on default again. I would like that the selected items and textboxes are the same as I entered the first time. So I may only change a little piece and not enter the hole info again. I thought about adding a new list entry in my combobox with values from the variable so I can choose between the default entries and the now entered value. So I could then initialize my ComboBox with this list number. Here is my code The text in red is what i would like to do but doesn't work Private Sub UserForm_Initialize() ComboBox1.ColumnCount = 2 ComboBox1.ColumnWidths = "50;20" Dim listEntries1(3, 1) As Variant 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 'listEntries1(4, 0) = ActiveDocument.Variables.Item(var1).Value 'listEntires1(4, 1) = ActiveDocument.Variables.Item(var1).Value Me.ComboBox1.List = listEntries1 With ComboBox1 ComboBox1 = ComboBox1.List(0) End With End Sub Private Sub OK_Click() 'Define the variable oVars Set ovars = ActiveDocument.Variables 'Assign the values of the text boxes to the variables ovars("var1").Value = Me.ComboBox1.Value ActiveDocument.Fields.Update Unload Me End Sub I searched all over the internet for a solution but didn't find one. So please can you help? If you can help me with a better solution that the previously selected items remain selected so please tell me. Thank you |
#2
|
||||
|
||||
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 |
#3
|
|||
|
|||
More on the technique Graham has provided is included here:
http://gregmaxey.com/word_tip_pages/...userforms.html |
#4
|
|||
|
|||
Thank you
Gmayor thank you very much for your answer.
This helped me a lot. You are my hero |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Moving Selected Items from a Multiselect Listbox on a userform to a bookmark in Word | marksm33 | Word VBA | 3 | 01-15-2015 07:55 PM |
How can I style individual items of a drop down or combobox? | supremegrandruler | Word | 1 | 12-09-2014 03:17 PM |
Table in Mailmerge from selected items in access | cnaab | Mail Merge | 1 | 11-01-2014 12:45 AM |
Closing space between document and vertical ruler | ToddPr | Word | 6 | 08-28-2014 12:12 PM |
Word loses image sizing when I reopen file | rbz1949 | Drawing and Graphics | 4 | 06-24-2010 11:19 AM |