PDA

View Full Version : Solved: Printing problem with ListBox



dheffron
10-22-2012, 03:07 PM
I am using Word 2010 and a newbie at VBA. I created a ListBox in a Word document and populate it inside of VBA. There is a sub called FindApprovers that has the following code:

listbox.clear
listbox.additem "text1"
listbox.additem "text2"
...

Everytime there is a change somewhere in the document, at the completion of the change, the sub FindApprovers is called.

The form seems to work OK, until I print. When I click on the print button, the preview screen looks as I expect, with a full listbox, but the printed result is empty.

If, before I push the print button, I use the mouse to select an item in the listbox, then the print produces a full listbox. But I don't want my users to have to click the box, and I don't want a row to be selected, so I have "locked" the listbox.

Also, I am saving (and working) in "compatibility mode" since I will have users on various versions of Word.

Any ideas? Thanks in advance.

dheffron
10-23-2012, 08:23 AM
I found a work around for the the for the issue, but it is far less than optimal.

Instead of using a ListBox (with all the nice features that come with it) I am manually building and manipulating a TextBox. It is much more work to deal with it, but the printed result is what is really important.

gmaxey
10-23-2012, 08:44 AM
ActiveX listboxes also come with a lot of baggage. Maybe you could just add a userform to your project with a listbox and use the listbox to build your list but not show it:

Sub LoadList()
Dim oFrm As UserForm1
Dim lngRouter As Long
Dim i As Long
Set oFrm = New UserForm1
lngRouter = 3
With oFrm.ListBox1
Select Case lngRouter
Case 1
.AddItem "Bill Miller"
.AddItem "Joe Smith"
Case 2
.AddItem "Susan Jones"
.AddItem "Tom Daily"
Case 3
.AddItem "Doris Tillman"
.AddItem "Bobby Deats"
End Select
End With
'Your textbox.
ActiveDocument.Shapes(1).TextFrame.TextRange = ""
For i = 0 To oFrm.ListBox1.ListCount - 1
ActiveDocument.Shapes(1).TextFrame.TextRange.InsertAfter oFrm.ListBox1.List(i) & vbCr
Next i
ActiveDocument.Shapes(1).TextFrame.TextRange.Paragraphs.Last.Range.Delete
End Sub

dheffron
10-23-2012, 09:04 AM
This is an excellent idea. Now I don't have to redo the rest of my code.
Thanks!
:clap: