Solved

Printing with List Boxes

Posted on 1998-12-15
9
145 Views
Last Modified: 2010-05-03
I have a large List Box on a Form full of items added using the "Additem" method. How do I print:
 
i)everything (all the items) into a report?
ii) only the selected (checked) items into a report?

NB I'm using VB5 Enterprise Edition.
0
Comment
Question by:its_ajt
9 Comments
 

Author Comment

by:its_ajt
ID: 1450218
Edited text of question
0
 

Author Comment

by:its_ajt
ID: 1450219
Edited text of question
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450220
call PrintListbox(list1, true) to print all
call PrintListbox(list1, true) to print only selected

Private Sub PrintListBox(ByVal list As listbox, Optional ByVal fAll As Boolean = True)
    Dim i As Integer
    For i = 0 To list.ListCount - 1
        If (list.Selected(i) Or fAll) Then
            Printer.Print list.list(i)
        End If
    Next
    Printer.EndDoc
End Sub

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450221
call PrintListbox(list1, false) to print only selected
0
 
LVL 12

Expert Comment

by:mark2150
ID: 1450222
I was wondering how two *identical* calls were producing different results! (grin) For a moment there I thought you'ld invented the "do what I meant" function!

M
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450223
It's magic..
0
 

Author Comment

by:its_ajt
ID: 1450224
Can't get the code to work properly - something about list.Selected(i) being an invalid array but the programs at home and I'm at work! - and also it doesn't cater for multiple pages...........else I need more help getting it to work with the Printer object/Common dialog Control.

Thanks - Merry Xmas
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450225
Good Luck
0
 
LVL 1

Accepted Solution

by:
twolff earned 200 total points
ID: 1450226
Mirkwoods code is not bad but I would handle it a different way. It will require 3 functions that can be reused in other applications.

I would write one function that would print the line on the page. This function would be passed the text to be printed. It would then compare the height of the page minus the currenty location to the amount of space that the text to be printed will require using the textheight method of printer object. If the text will fit on the page then print the line else start a new page and then print the line.

The next function will be used to loop through all of the items in the listbox and send the text to the first function to be printed.

The final function will be used to print only the selected items in the list. This is a little harder(or not depending on your api knowledge) than presented by mirkwood. You can use the SendMessage API to retreive an array of indexs of only the selected items in a listbox or filebox. You would then cycle through the array and only print the items that are chosen. This will eliminate the need to loop through the whole list when deciding what is selected. This technique will also work if the listbox's style is changed to checkbox in VB5 and VB6.

Here is a sample that I wrote:

> Add a filebox(File1) and a commandbutton(Command1) to a form and paste the following code.

Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LB_GETSELCOUNT = &H190
Const LB_GETSELITEMS = &H191

Private Sub Command1_Click()
   Dim lngNumSel As Long
   Dim lngSelItems() As Long
   Dim lngStatus As Long
   Dim strMsg As String
   Dim x As Long
   
   '--- get the number of selected items
   lngNumSel = SendMessage(File1.hwnd, LB_GETSELCOUNT, 0, 0)
   
   '--- redim the array to hold the selected items
   ReDim lngSelItems(lngNumSel)
   
   '--- get the selected items
   lngStatus = SendMessage(File1.hwnd, LB_GETSELITEMS, lngNumSel, lngSelItems(0))
   
   Select Case lngStatus
      Case -1
         strMsg = "Error Occurred"
      Case 0
         strMsg = "None Selected"
      Case Else
         For x = 0 To lngStatus - 1
            strMsg = strMsg & File1.List(lngSelItems(x)) & vbCrLf
         Next x
   End Select
   MsgBox strMsg
End Sub

I hope this helps. Enjoy.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now