We help IT Professionals succeed at work.

Printing the contents of a listview control

mrsreeji
mrsreeji asked
on
hi,        
          I want to take a printout of the contents displayed in  listview control preferably together with the grid lines displayed in it.Also i want to change the layout of the paper.Plz help.
Comment
Watch Question

Commented:
If your target PC has Excel installed, you can export the listview to Excel and then print from there. First create a flat file...

Public Sub ExportLvToFlatFile(ByRef voLv As Object, ByRef vsPath As String)
    On Error GoTo ReportTheError

    Dim lErr As Long
    Dim sErr As String

    Dim oLv As ListView
    Dim sLine As String
    Dim oLi As ListItem
    Dim lMousePointer As Long
    Dim oFs As Scripting.FileSystemObject
    Dim oTxt As Scripting.TextStream
   
    ' Remember the mousepointer and set to an hourglass
    lMousePointer = Screen.MousePointer
    Screen.MousePointer = vbHourglass
   
    ' Check that this is a listview
    If Not TypeOf voLv Is ListView Then
        Err.Raise 5, , "First parameter must be a listview"
    End If
    Set oLv = voLv
   
    Set oFs = New Scripting.FileSystemObject
    Set oTxt = oFs.CreateTextFile(vsPath, True)

    ' Heading line
    sLine = ""
    Dim i As Integer
    For i = 1 To oLv.ColumnHeaders.Count
        sLine = sLine & MailMergeString( _
            vsInString:=oLv.ColumnHeaders(i).Text, _
            bNoComma:=(i = oLv.ColumnHeaders.Count))
    Next
   
    oTxt.WriteLine sLine
   
    ' Now the listitems
    For Each oLi In oLv.ListItems
        sLine = ""
        sLine = sLine & MailMergeString(oLi.Text, True)
        For i = 1 To oLi.ListSubItems.Count
            sLine = sLine & "," & MailMergeString(oLi.ListSubItems(i).Text, True)
        Next
        oTxt.WriteLine sLine
    Next
   
    oTxt.Close
    Set oTxt = Nothing
    Set oFs = Nothing
    Set oLi = Nothing
    Set oLv = Nothing
   
    Screen.MousePointer = lMousePointer

    Exit Sub
ReportTheError:
    lErr = Err.Number
    sErr = Err.Description
    Screen.MousePointer = lMousePointer
    Err.Raise lErr, , sErr
End Sub
Private Function MailMergeString(vsInString, Optional bNoComma = False)
    On Error GoTo PassOnTheError

    Dim lErr As Long
    Dim sErr As String

    Const DOUBLEQUOTE = """"
   
    MailMergeString = Replace(vsInString, DOUBLEQUOTE, DOUBLEQUOTE & DOUBLEQUOTE)
    MailMergeString = DOUBLEQUOTE & Trim(MailMergeString) & DOUBLEQUOTE _
        & IIf(bNoComma, "", ",")
   
    MailMergeString = Replace(MailMergeString, vbCrLf, " ")
   
    Exit Function
PassOnTheError:
    lErr = Err.Number
    sErr = Err.DescriptionErr.Raise lErr, , sErr
End Function

Then export it to Excel:

    ' Get reference to Excel if it is running
    On Error Resume Next
    Set oExcel = GetObject(, "Excel.Application")
    If Err <> 0 Then
        ' Not running: create new instance
        Err.Clear
        On Error GoTo ReportTheError
        Set oExcel = CreateObject("Excel.Application")
    Else
        ' Running: make invisible to avoid lots of flicker.
        On Error GoTo ReportTheError
        oExcel.Visible = False
    End If

    ' Add a workbook and import the data
    oExcel.Workbooks.Add
    Set oWorkBook = oExcel.ActiveWorkbook
    Set oWorkSheet = oExcel.ActiveWorkbook.ActiveSheet
    oWorkSheet.Range("A1").Select
   
    With oWorkSheet.QueryTables.Add( _
        Connection:="TEXT;" & sPath, _
        Destination:=oExcel.Selection)
            .Name = "listviewexport"
            .TextFileTextQualifier = 1      ' xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileCommaDelimiter = True
            .Refresh False
            .EnableRefresh = False
            .EnableEditing = False
    End With
   
    ' Make titles bold
    oWorkSheet.Rows(1).Select
    oExcel.Selection.Font.Bold = True
   
    oExcel.Columns.AutoFit
    oWorkSheet.Range("A1").Select
   
    ' Make Excel visible
    oWorkBook.Activate
    oWorkSheet.Activate
    oExcel.Visible = True
   
    ' Cleanup
    Screen.MousePointer = lMousePointer
    Set oWorkSheet = Nothing
    Set oWorkBook = Nothing

Then write code to do the printing from Excel. The easiest may to generate this code is to, after running the above, record a macro in Excel and convert the generated VBA code to VB.

Commented:
This question appears to have been abandoned. A Moderator will be asked to
close this question after seven days, with the following recommended
disposition:

Points to stodge

If you have any comment or objection to the recommendation, please leave it here.

guidway
EE Cleanup Volunteer
Per recommendation, force-accepted.

Netminder
CS Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.