PDA

View Full Version : Solved: Word 2007 ListBox values disappear when doc is re-opened



tuonela76
04-22-2009, 12:57 PM
I have 3 listboxes in a table in my document. One listbox is populated when the user hits the enter key after entering text. Another listbox is populated when a user makes a selection in one of several drop-down boxes. The other is populated when the user clicks a specific commandbutton after highlighting an item from each of the first two listboxes. My listboxes are fmMultiSelectSingle because my code uses the "Text" property of a selected item in the listbox to populate another listbox.

My problem is, when the user saves the document and closes it and then re-opens it later, highlighted values appear, but then after a few seconds they disappear. Other values in the listbox never appear unless they are listed above the highlighted value in the listbox (if they are listed above the highlighted value, they appear and then disappear).

I need all previously populated values to populate the listboxes when the user re-opens a document after having saved and closed the document.

Any suggestions? My code is below:


Public chkbxVal63 As Boolean

Private Sub Document_Open()
'Set a boolean value for the CheckBox63_Click event to use
If CheckBox63.Value Then
chkbxVal63 = True
Else
chkbxVal63 = False
End If

'Set the options in the drop-downs
ComboBox0.List = Array(" ", "N/A", "No", "Yes")
ComboBox1.List = Array(" ", "N/A", "No", "Yes")
ComboBox2.List = Array(" ", "N/A", "No", "Yes")
ComboBox3.List = Array(" ", "N/A", "No", "Yes")
ComboBox4.List = Array(" ", "N/A", "No", "Yes")
ComboBox5.List = Array(" ", "N/A", "No", "Yes")
ComboBox6.List = Array(" ", "N/A", "No", "Yes")
ComboBox7.List = Array(" ", "N/A", "No", "Yes")
ComboBox8.List = Array(" ", "N/A", "No", "Yes")
ComboBox10.List = Array(" ", "No", "Yes")
ComboBox11.List = Array(" ", "No", "Yes")
ComboBox12.List = Array(" ", "No", "Both", "Removals", "Adds")
ComboBox13.List = Array(" ", "No", "Yes")
ComboBox14.List = Array(" ", "No", "Both", "Downgrades", "Upgrades")
ComboBox15.List = Array(" ", "No", "Both", "Removals", "Adds")
ComboBox16.List = Array(" ", "No", "Yes")
ComboBox17.List = Array(" ", "No", "Both", "Downgrades", "Upgrades")
ComboBox18.List = Array(" ", "No", "Yes")

'Set the values of the drop-downs to blank
ComboBox0.Value = " "
ComboBox1.Value = " "
ComboBox2.Value = " "
ComboBox3.Value = " "
ComboBox4.Value = " "
ComboBox5.Value = " "
ComboBox6.Value = " "
ComboBox7.Value = " "
ComboBox10.Value = " "
ComboBox11.Value = " "
ComboBox12.Value = " "
ComboBox13.Value = " "
ComboBox14.Value = " "
ComboBox15.Value = " "
ComboBox16.Value = " "
ComboBox17.Value = " "
ComboBox18.Value = " "
ComboBox8.Value = " "
End Sub


Private Sub CheckBox63_Click()
'Check or uncheck the checkbox
CheckBox63.Value = (Not chkbxVal63)
chkbxVal63 = CheckBox63.Value

'If the box is checked, check the other two and disable them
If CheckBox63.Value Then
CheckBox61.Value = True
CheckBox62.Value = True
CheckBox61.Enabled = False
CheckBox62.Enabled = False
'Otherwise, uncheck other boxes and enable them
Else
CheckBox61.Value = False
CheckBox62.Value = False
CheckBox61.Enabled = True
CheckBox62.Enabled = True
End If
End Sub


Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'If the user hits the enter key, populate the listbox as long as the text is not blank
'and the listbox does not already have ten values
If (KeyCode = 13) Then
If Not TextBox1.Value = "" Then
If ListBox1.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
ListBox1.AddItem TextBox1.Text
TextBox1.Text = ""
End If
Else
MsgBox "You must enter a product."
End If
End If
End Sub


Private Sub ComboBox0_Change()
ComboBox1.Select
End Sub


Private Sub ComboBox1_Change()
ComboBox2.Select
End Sub


Private Sub ComboBox2_Change()
ComboBox3.Select
End Sub


Private Sub ComboBox3_Change()
ComboBox4.Select
End Sub


Private Sub ComboBox4_Change()
ComboBox5.Select
End Sub


Private Sub ComboBox5_Change()
ComboBox6.Select
End Sub


Private Sub ComboBox6_Change()
ComboBox7.Select
End Sub


Private Sub ComboBox7_Change()
ComboBox8.Select
End Sub


Private Sub ComboBox8_Change()
ComboBox10.Select
End Sub


Private Sub ComboBox10_Change()
Dim str As String
'If the user selects a particular option, populate the listbox with a particular string
'unless the listbox already has 10 values
If ComboBox10.Value = "Yes" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Outside Moves"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
ComboBox11.Select
End Sub


Private Sub ComboBox11_Change()
Dim str As String
'If the user selects a particular option, populate the listbox with a particular string
'unless the listbox already has 10 values
If ComboBox11.Value = "Yes" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "New Installs"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
ComboBox12.Select
End Sub


Private Sub ComboBox12_Change()
Dim str As String
'If the user selects a particular option, populate the listbox with a particular string
'unless the listbox already has 10 values
If ComboBox12.Value = "Both" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver Yes: Removals/Adds"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
If ComboBox12.Value = "Removals" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver Yes: Removals"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
If ComboBox12.Value = "Adds" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver Yes: Adds"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
ComboBox13.Select
End Sub


Private Sub ComboBox13_Change()
Dim str As String
'If the user selects a particular option, populate the listbox with a particular string
'unless the listbox already has 10 values
If ComboBox13.Value = "Yes" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver Yes: Changes"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
ComboBox14.Select
End Sub


Private Sub ComboBox14_Change()
Dim str As String
'If the user selects a particular option, populate the listbox with a particular string
'unless the listbox already has 10 values
If ComboBox14.Value = "Both" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver Yes: Downgrades/Upgrades"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
If ComboBox14.Value = "Downgrades" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver Yes: Downgrades"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
If ComboBox14.Value = "Upgrades" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver Yes: Upgrades"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
ComboBox15.Select
End Sub


Private Sub ComboBox15_Change()
Dim str As String
'If the user selects a particular option, populate the listbox with a particular string
'unless the listbox already has 10 values
If ComboBox15.Value = "Both" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver No: Removals/Adds"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
If ComboBox15.Value = "Removals" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver No: Removals"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
If ComboBox15.Value = "Adds" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver No: Adds"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
ComboBox16.Select
End Sub


Private Sub ComboBox16_Change()
Dim str As String
'If the user selects a particular option, populate the listbox with a particular string
'unless the listbox already has 10 values
If ComboBox16.Value = "Yes" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver No: Changes"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
ComboBox17.Select
End Sub


Private Sub ComboBox17_Change()
Dim str As String
'If the user selects a particular option, populate the listbox with a particular string
'unless the listbox already has 10 values
If ComboBox17.Value = "Both" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver No: Downgrades/Upgrades"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
If ComboBox17.Value = "Downgrades" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver No: Downgrades"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
If ComboBox17.Value = "Upgrades" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "Driver No: Upgrades"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
ComboBox18.Select
End Sub


Private Sub ComboBox18_Change()
Dim str As String
'If the user selects a particular option, populate the listbox with a particular string
'unless the listbox already has 10 values
If ComboBox18.Value = "Yes" Then
If ListBox2.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
str = "PD Orders"
ListBox2.AddItem str
ListBox2.Selected(0) = True
End If
End If
TextBox1.Select
End Sub


Private Sub cmdBtn1_Click()
'Remove the selected item and highlight the last item in the listbox, or display a message
If ListBox1.ListIndex < 0 Then
MsgBox "Nothing to remove."
TextBox1.Select
Else
ListBox1.RemoveItem ListBox1.ListIndex
ListBox1.ListIndex = ListBox1.ListCount - 1
End If
End Sub


Private Sub cmdBtn2_Click()
'Remove the selected item and highlight the last item in the listbox, or display a message
If ListBox2.ListIndex < 0 Then
MsgBox "Nothing to remove."
ComboBox10.Select
Else
ListBox2.RemoveItem ListBox2.ListIndex
ListBox2.ListIndex = ListBox2.ListCount - 1
End If
End Sub


Private Sub cmdBtn3_Click()
Dim str As String
'Add the selection from listbox1 and listbox2 as a concatenated string
'Display messages if one or both listboxes contain no highlighted(selected) item.
str = ListBox1.Text & " - " & ListBox2.Text

If ListBox3.ListCount = 10 Then
MsgBox "Only 10 Items are allowed in the list."
Else
If ListBox1.ListIndex < 0 Then
If ListBox2.ListIndex < 0 Then
MsgBox "There is nothing to add."
ComboBox10.Select
Else
MsgBox "Please enter products."
TextBox1.Select
End If
Else
If ListBox2.ListIndex < 0 Then
MsgBox "Please make a selection for Scope."
ComboBox10.Select
Else
ListBox3.AddItem str
ListBox3.Selected(0) = True
End If
End If
End If

'If the last item from both boxes is highlighted, do not select anything
If ListBox1.Selected(ListBox1.ListCount - 1) Then
If ListBox2.Selected(ListBox2.ListCount - 1) Then
ListBox1.Selected(ListBox1.ListCount - 1) = False
ListBox2.Selected(ListBox2.ListCount - 1) = False
End If
Else
ListBox1.Selected(0) = True
ListBox2.Selected(0) = True
End If
End Sub


Private Sub cmdBtn4_Click()
'Remove the selected item and highlight the last item in the listbox, or display a message
If ListBox3.ListIndex < 0 Then
MsgBox "Nothing to remove."
cmdBtn3.Select
Else
ListBox3.RemoveItem ListBox3.ListIndex
ListBox3.ListIndex = ListBox3.ListCount - 1
End If
End Sub

fumei
04-22-2009, 02:19 PM
I wish I could help, but that is a Word 2007 file.

I know it is a real pain, but I would strongly recommend that you use meaningful names for objects. Maybe for this particular project you know what ComboBox17 has as items, and what it is used for, and what it means. However, in the long run, using explicit, meaningful names for objects will help you...and anyone else who may look at your code.

I am trying to duplicate the behaviour in my version of Word, but I am not getting the same behaviour.

BTW: you may also want to consider using your array AS an array. For example:
Dim BasicListA()
BasicListA = Array(" ", "N/A", "No", "Yes")
With ComboBox1
.List = BasicListA
.ListIndex = 0
End With
With ComboBox2
.List = BasicListA
.ListIndex = 0
End With
With ComboBox3
.List = BasicListA
.ListIndex = 0
End With

ListIndex = 0 is the same as ComboBox0.Value = " " . It just does not require a separately parsed instruction.

BTW: interesting you have ComboBox0. The default is 1.

tuonela76
04-23-2009, 05:55 AM
I wish I could help, but that is a Word 2007 file.

I know it is a real pain, but I would strongly recommend that you use meaningful names for objects. Maybe for this particular project you know what ComboBox17 has as items, and what it is used for, and what it means. However, in the long run, using explicit, meaningful names for objects will help you...and anyone else who may look at your code.

I am trying to duplicate the behaviour in my version of Word, but I am not getting the same behaviour.

BTW: you may also want to consider using your array AS an array. For example:
Dim BasicListA()
BasicListA = Array(" ", "N/A", "No", "Yes")
With ComboBox1
.List = BasicListA
.ListIndex = 0
End With
With ComboBox2
.List = BasicListA
.ListIndex = 0
End With
With ComboBox3
.List = BasicListA
.ListIndex = 0
End With

ListIndex = 0 is the same as ComboBox0.Value = " " . It just does not require a separately parsed instruction.

BTW: interesting you have ComboBox0. The default is 1.

Thanks for the reply! :thumb

I do use meaningful names when I program something that is more complex than the document I am attempting to create here. Unfortunately, when you create an object in Office (using the Developer Ribbon instead of creating objects programmatically), the object is given a non-meaningful name automatically. If I were prompted for a name for the object when I create the object, it would make life more simple. Anyway... in this case, the values in the objects are not important. The properties of the objects are important, and the statements written for those objects are important.

Also, I appreciate your advice regarding the array. However, to assign an array of values to an object using your suggestion requires 4 lines of code per object plus the array declaration and the array assignment, where as the code I wrote to assign values to the array only requires one line. Granted, I could create one array for several objects and would only need to make a change one time across all objects if a change was required, but in this case, the values won't change and I preferred to have shorter code for this project. If this were a larger project, I would declare and assign an array as you suggested.

As for ComboBox0... I had created CombBox objects for each of the columns in the table on the document, and then a co-worker reminded me that I needed to address "Outside Moves". I added this column before the other columns in the table, and because I didn't want to rename all the ComboBoxes (because, as you said, it is a pain), I changed the ComboBox name to ComboBox0. It seems perfectly normal to me, but then again, I AM a Java programmer by trade.

My code is definitely not very clean... I could certainly write better functions than the ones seen here, but I'm just trying to get this done quick and dirty. This is a very small tool that I will use to perform a job that is extremely complex. Again, I appreciate your suggestions, and if they pertained to the actual problem I was having, I might follow your advice. Don't get me wrong... constructive criticism is always appreciated, and if you come up with something that might help solve the issue I am having, I would be happy to try your suggestions. :peace:

My thinking is that setting the MultiSelect property to fmMultiSelectSingle might be causing my problem, and if that is the case, I may have to resort to saving the listbox values in a textbox on the document on Document_Close(), and then populating the listbox from the textbox on Document_Open().

fumei
04-23-2009, 09:44 AM
"However, to assign an array of values to an object using your suggestion requires 4 lines of code per object plus the array declaration and the array assignment, where as the code I wrote to assign values to the array only requires one line. "

You are confusing lines of code with the parsing and execution of instructions.

ComboBox0.List = Array(" ", "N/A", "No", "Yes")
ComboBox1.List = Array(" ", "N/A", "No", "Yes")
ComboBox2.List = Array(" ", "N/A", "No", "Yes")
ComboBox3.List = Array(" ", "N/A", "No", "Yes")

The above is FOUR individual parsings (VBA parses (" ", "N/A", "No", "Yes") each time) and then FOUR instructions.

ComboBox0.Value = " "
ComboBox1.Value = " "
ComboBox2.Value = " "
ComboBox3.Value = " "

The above is FOUR executed instructions.

Total: FOUR parsings and EIGHT instructions.

Dim BasicListA()
BasicListA = Array(" ", "N/A", "No", "Yes")
The above is parsed and then instruction executed.

ONE instruction

With ComboBox0
.List = BasicListA
.ListIndex = 0
End With
With ComboBox1
.List = BasicListA
.ListIndex = 0
End With
With ComboBox2
.List = BasicListA
.ListIndex = 0
End With
With ComboBox3
.List = BasicListA
.ListIndex = 0
End With

The above is FOUR instructions.

Total: FIVE instructions.

Each With statement is ONE executed instruction. The array is parsed only once, rather than four separate times.

However, you are correct, this is a piddly little detail. And in the scheme of things, with such fast machine we use now, not a big deal. I was being persnickety in that I try to minimize actual, real, executed instructions.

I wish I could really look at your file, because as I said, I can not seem to duplicate the problem.

Nelviticus
04-27-2009, 08:34 AM
The problem is that then contents of list boxes aren't saved with the document - I've never used them but I just created a simple doc (in Word 2007) with a text box, list box and command button. The command button just added whatever was in the text box to the list box.

When I saved it and re-opened it, the text box held the value I'd last entered but the list box was empty.

I guess the solution is to store the list box contents in a table, either on save or when you click a button, and read them back into the list boxes when you open the document.

tuonela76
04-27-2009, 10:12 AM
I thought about adding the listbox contents to a textbox on the same document on save, and then loading the textbox contents back into the listbox on open. Do you think that will work?


The problem is that then contents of list boxes aren't saved with the document - I've never used them but I just created a simple doc (in Word 2007) with a text box, list box and command button. The command button just added whatever was in the text box to the list box.

When I saved it and re-opened it, the text box held the value I'd last entered but the list box was empty.

I guess the solution is to store the list box contents in a table, either on save or when you click a button, and read them back into the list boxes when you open the document.

Paul_Hossler
04-27-2009, 10:30 AM
If I were prompted for a name for the object when I create the object, it would make life more simple


If you're adding ActiveX controls in 2007, if you click 'Properties' right below 'Design Mode' it will put a seperate pane up where you can name the object, or change some of the other properties.

It stays up and if you select another object, it will then show that object's properties.

Paul

Paul_Hossler
04-27-2009, 10:38 AM
I could reproduce it

But I must be missing something: you clear the values in your Open event, so I'm not surprised they disappear

They ARE there until I enable macros (allowing the Open event to fire), and then they go away



Private Sub Document_Open()

' ........

'Set the values of the drop-downs to blank
ComboBox0.Value = " "
ComboBox1.Value = " "
ComboBox2.Value = " "
ComboBox3.Value = " "
ComboBox4.Value = " "
ComboBox5.Value = " "
ComboBox6.Value = " "
ComboBox7.Value = " "
ComboBox10.Value = " "
ComboBox11.Value = " "
ComboBox12.Value = " "
ComboBox13.Value = " "
ComboBox14.Value = " "
ComboBox15.Value = " "
ComboBox16.Value = " "
ComboBox17.Value = " "
ComboBox18.Value = " "
ComboBox8.Value = " "
End Sub



You could store the responses in a CustomDocumentProperty when exiting (e.g. = "YYYNNNYYYNNN") . Then check for the property when you open, and if it exists, fill the comboboxes with it's values, otherwise set them to blank

Paul

fumei
04-27-2009, 11:35 AM
Paul....huh? It is about a Listbox, and keeping values across a close and reopen.

You can do this by putting the items in the Listbox into a DOCVARIABLE.

Option Explicit

Private Sub CommandButton1_Click()
ListBox1.AddItem TextBox1.Text
TextBox1.Text = ""
TextBox1.Select
End Sub

Private Sub Document_Close()
Dim j As Long
If ListBox1.ListCount = 0 Then
Exit Sub
End If
For j = 0 To ListBox1.ListCount - 1
ActiveDocument.Variables("myListbox").Value = _
ActiveDocument.Variables("myListbox").Value & _
"," & ListBox1.List(j)
Next
End Sub
Private Sub Document_Open()
Dim j As Long
Dim NewList
If ActiveDocument.Variables("myListbox").Value = " " Then
Exit Sub
End If
NewList = Split(ActiveDocument.Variables("myListbox").Value, ",")
For j = 1 To UBound(NewList)
ListBox1.AddItem NewList(j)
Next
ActiveDocument.Variables("myListbox").Value = " "
End Sub

Sub ClearListbox()
ListBox1.Clear
End Sub

On Document_Close, the document variable (myListbox) checks to see if there ARE items in the listbox; if there are, it parses through the Listbox adding each item - followed by a comma - into the doc variable.

On Document_Open, it checks to see if the variable is " " - thus on previous Close there WERE no items in the Listbox. If the variable is NOT " ", then it Splits the Value of the variable, adding each item in the Split array as individual AddItems to the Listbox.

Demo attached. The commandbutton takes the text from the Textbox and puts it in the Listbox. The demo has some sample text, but you can clear it to see...well, whatever...by click "Clear Listbox" on the top toolbar.

fumei
04-27-2009, 11:36 AM
Sorry Paul...something very bizarre was happening with my screen. I could not see the rest of your post!

tuonela76
04-27-2009, 01:14 PM
fumei...
Thank you for posting the code and the file. This worked for me! I did take your advice regarding the arrays for the dropdowns... I'm a bit OCD that way (so don't tell me something else is wrong, hehe). I have attached a .doc version to this reply so that you can see the complete project. I didn't test the .doc version, but I don't think there will be compatibility issues.

**Note: Regarding navigation through the fields...
ComboBox10 - ComboBox18 are used to populate ListBox2 ("Order Type"). It is my personal preference to use the tab key to trigger adding an item to the listbox, so in each of the comboboxes (10-18), I used the KeyDown event and populated the listbox if the tab key was used.

Also, TextBox1 ("Product(s) Affected") is used to populate ListBox1 ("Product"). I used the KeyDown event and populated the listbox if the return key was used. My expectation is that one product will be typed and then using the enter key adds the text to the listbox.

I am the only person that uses this document, so navigation may not conform to a "standard". **

And... apparently the code is too long to add, so you'll just have to see the attached doc.

Thanks again for your help!!


Paul....huh? It is about a Listbox, and keeping values across a close and reopen.

You can do this by putting the items in the Listbox into a DOCVARIABLE.

Option Explicit

Private Sub CommandButton1_Click()
ListBox1.AddItem TextBox1.Text
TextBox1.Text = ""
TextBox1.Select
End Sub

Private Sub Document_Close()
Dim j As Long
If ListBox1.ListCount = 0 Then
Exit Sub
End If
For j = 0 To ListBox1.ListCount - 1
ActiveDocument.Variables("myListbox").Value = _
ActiveDocument.Variables("myListbox").Value & _
"," & ListBox1.List(j)
Next
End Sub
Private Sub Document_Open()
Dim j As Long
Dim NewList
If ActiveDocument.Variables("myListbox").Value = " " Then
Exit Sub
End If
NewList = Split(ActiveDocument.Variables("myListbox").Value, ",")
For j = 1 To UBound(NewList)
ListBox1.AddItem NewList(j)
Next
ActiveDocument.Variables("myListbox").Value = " "
End Sub

Sub ClearListbox()
ListBox1.Clear
End Sub

On Document_Close, the document variable (myListbox) checks to see if there ARE items in the listbox; if there are, it parses through the Listbox adding each item - followed by a comma - into the doc variable.

On Document_Open, it checks to see if the variable is " " - thus on previous Close there WERE no items in the Listbox. If the variable is NOT " ", then it Splits the Value of the variable, adding each item in the Split array as individual AddItems to the Listbox.

Demo attached. The commandbutton takes the text from the Textbox and puts it in the Listbox. The demo has some sample text, but you can clear it to see...well, whatever...by click "Clear Listbox" on the top toolbar.

Paul_Hossler
04-27-2009, 03:58 PM
I'll have to read up on Doc variables -- I usually just use a Custom Document property (s) and have some encoding scheme to store the data, like Property ( ListBoxValues) = "YYYNNNYYYNNN" etc.

Your way sounds better

Paul

tuonela76
04-27-2009, 05:59 PM
I know that it works this way, and in addition you have to get the document back into focus and select the next object, and if you click too many times, you get the VBE... it's a pain. Otherwise you can use the drop down in the properties pane... also a pain. Again, renaming an object that already has a name seems useless for such a small project. Perhaps Microsoft should stop assigning names to objects automatically.


If you're adding ActiveX controls in 2007, if you click 'Properties' right below 'Design Mode' it will put a separate pane up where you can name the object, or change some of the other properties.

It stays up and if you select another object, it will then show that object's properties.

Paul

fumei
04-28-2009, 12:04 PM
"Perhaps Microsoft should stop assigning names to objects automatically."

Objects must have names to exist. This applies to all OOP (Object Oriented Programming) languages.

Regarding ActiveX controls, with items (Listbox and ComboBox), on document Save and reopened:

Word does NOT retain items that have been put into Comboboxes using .AddItem. This is why you need to repopulate them in Document_Open.

Word DOES retain selected items.

So....
Private Sub Document_Open()

ComboBox1.AddItem "first"
ComboBox1.AddItem "second"
ComboBox1.AddItem "third"
'ComboBox1.ListIndex = 0

End Sub
Note the commented out line for .ListIndex.

This adds the items ("first", "second", "third") to the Combobox, BUT...because ListIndex is not there, the Combobox will display as blank. Yes, the dropdown will show "first", "second", "third".

If you now comment out ALL of the population code (so Document_Open does NOT populate), if you save and reopen, the Combobox will NOT have anything there.

However, if you had selected an item (say "first"), then on reopen "first" WILL be there...but nothing else.

To repeat:

Word does NOT retain items that have been put into Comboboxes using .AddItem. This is why you need to repopulate them in Document_Open.

Word DOES retain selected items.

tuonela76
04-30-2009, 06:06 AM
"Perhaps Microsoft should stop assigning names to objects automatically."

Objects must have names to exist. This applies to all OOP (Object Oriented Programming) languages.


Not really what I meant, but it doesn't matter.

I have another question for you, though. I am unfamiliar with docvariable. I sent this document to my boss to show her what I had been working on. When she opened the document and the Document_Open code ran, an error message was displayed saying that an object had been deleted. The debugger showed that the deleted object was the first docvariable (and I'm certain I would get the same error on the other two docvariables in the Sub). Is there something else I need to do to avoid this error?