Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-28-2015, 06:59 AM
Attila Attila is offline Selected items in Combobox remain the same when closing and reopen document Windows XP Selected items in Combobox remain the same when closing and reopen document Office 2010 64bit
Novice
Selected items in Combobox remain the same when closing and reopen document
 
Join Date: Jan 2015
Posts: 7
Attila is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 01-28-2015, 07:20 AM
gmayor's Avatar
gmayor gmayor is offline Selected items in Combobox remain the same when closing and reopen document Windows 7 64bit Selected items in Combobox remain the same when closing and reopen document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,105
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 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
  #3  
Old 01-28-2015, 08:15 AM
gmaxey gmaxey is offline Selected items in Combobox remain the same when closing and reopen document Windows 7 32bit Selected items in Combobox remain the same when closing and reopen document Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

More on the technique Graham has provided is included here:
http://gregmaxey.com/word_tip_pages/...userforms.html
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 01-29-2015, 04:08 AM
Attila Attila is offline Selected items in Combobox remain the same when closing and reopen document Windows XP Selected items in Combobox remain the same when closing and reopen document Office 2010 64bit
Novice
Selected items in Combobox remain the same when closing and reopen document
 
Join Date: Jan 2015
Posts: 7
Attila is on a distinguished road
Thumbs up Thank you

Gmayor thank you very much for your answer.
This helped me a lot.
You are my hero
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Selected items in Combobox remain the same when closing and reopen document 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
Selected items in Combobox remain the same when closing and reopen document How can I style individual items of a drop down or combobox? supremegrandruler Word 1 12-09-2014 03:17 PM
Selected items in Combobox remain the same when closing and reopen document 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:48 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft