CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1

    Need Help Printing A Listview

    been working on my program for quite a while now and the only thing stopping me from completeing this work is printing out a search with the listview look the lines and so forth this is my code im useing now the search runs fine now i just need to get it to print any help will be great

    /
    Private Sub Command9_Click()
    Close
    Recordlen = Len(person)
    Filenum = 1
    Open "c:\program files\bondsman\info.dat" For Random As Filenum Len = Recordlen
    Currentrecord = 1
    Lastrecord = FileLen("c:\program files\bondsman\info.dat") / Recordlen
    For a = 1 To Lastrecord
    Get #Filenum, Currentrecord, person
    recnumber = Trim(person.recnumber)
    cfirst = Trim(person.cfirst)
    cmiddle = Trim(person.cmiddle)
    clast = Trim(person.clast)
    chphone = Trim(person.chphone)
    cssn = Trim(person.cssn)
    cdob = Trim(person.cdob)
    If msearch.Text = "" Then GoTo 1 Else

    22
    If msearch.Text = cdob Or msearch.Text = ray2 Then Text2 = Currentrecord
    If msearch.Text = cdob Or msearch.Text = ray2 Then ListView1.ListItems.Add , , recnumber
    If msearch.Text = cdob Or msearch.Text = ray2 Then ListView1.ListItems(ListView1.ListItems.Count).ListSubItems.Add , , cfirst
    If msearch.Text = cdob Or msearch.Text = ray2 Then ListView1.ListItems(ListView1.ListItems.Count).ListSubItems.Add , , cmiddle
    If msearch.Text = cdob Or msearch.Text = ray2 Then ListView1.ListItems(ListView1.ListItems.Count).ListSubItems.Add , , clast
    If msearch.Text = cdob Or msearch.Text = ray2 Then ListView1.ListItems(ListView1.ListItems.Count).ListSubItems.Add , , chphone
    If msearch.Text = cdob Or msearch.Text = ray2 Then ListView1.ListItems(ListView1.ListItems.Count).ListSubItems.Add , , cssn
    If msearch.Text = cdob Or msearch.Text = ray2 Then ListView1.ListItems(ListView1.ListItems.Count).ListSubItems.Add , , cdob
    Currentrecord = Currentrecord + 1
    Next a
    1
    Close
    End Sub code/

  2. #2
    Join Date
    Dec 2004
    Posts
    423

    Re: Need Help Printing A Listview

    You need to decide how you want to handle this, here are a few ways I suggest.

    Print to DataReport
    Print to Excel
    Print to NotePad

  3. #3

    Re: Need Help Printing A Listview

    printing to excel would be the perfect way to do this can ya give any suggestions and thanks for the help in advance

  4. #4
    Join Date
    Dec 2004
    Posts
    423

    Re: Need Help Printing A Listview

    OK
    First you need to add a reference to Excel in your project.

    then

    Code:
    Dim MyExcel as Excel.Application
    Dim MyBook as Excel.WorkBook
    Dim MySheet as Excel.WorkSheet
    	
    	Set MyExcel = New Excel.Application
    	Set MyBook = MyExcel.WorkBooks.Add
    	Set MySheet = MyBook.WorkSheets(1)
    The MySheet Object will all the features you need
    Code:
    MySheet.Range("A" & x).Value = 'WhatEver'
    
    OR 
    
    MySheet.Cells(1,1,).Value = 'WhatEver'

    And At the end of this procedure you need to

    Code:
    MySheet.Printout 'Prints Report
    MyBook.Close
    MYExcel.Quit
    SEt MyExcel = Nothing
    Set MyBook= Nothing
    Set MySheet = Nothing
    If you wanted this report to look professional I would suggest creating a template in Excel then instead of
    Code:
    SEt MyBook = MYExcel.WorkBooks.Add
    You can use
    Code:
    SET MyBook = MyExcel.WorkBooks.Open('Path')
    And all you need to do is speficy where the data goes.
    Hope that Helps!

  5. #5

    Re: Need Help Printing A Listview

    this is the code im useing for the search it works but i cant make the feilds larger in excel

    [
    Private Sub Command4_Click()

    Screen.MousePointer = vbHourglass

    Dim objXcell As Object
    Dim objWbook As Object
    Dim objSheet As Object
    Set objXcell = CreateObject("Excel.Application")
    '**********************************
    Set objWbook = objXcell.Workbooks.Add
    Set objSheet = objWbook.Worksheets.Add
    With objSheet
    If Dir("c:\search.xls") <> "" Then
    Kill "c:\search.xls"
    End If

    Dim lngCounter As Long
    For lngCounter = 1 To ListView1.ListItems.Count


    objSheet.Cells(lngCounter, 1).Value = ListView1.ListItems(lngCounter).Text
    objSheet.Cells(lngCounter, 2).Value = ListView1.ListItems(lngCounter).SubItems(1)
    objSheet.Cells(lngCounter, 3).Value = ListView1.ListItems(lngCounter).SubItems(2)
    objSheet.Cells(lngCounter, 4).Value = ListView1.ListItems(lngCounter).SubItems(3)
    objSheet.Cells(lngCounter, 5).Value = ListView1.ListItems(lngCounter).SubItems(4)
    objSheet.Cells(lngCounter, 6).Value = ListView1.ListItems(lngCounter).SubItems(5)
    objSheet.Cells(lngCounter, 7).Value = ListView1.ListItems(lngCounter).SubItems(6)
    objSheet.Cells(lngCounter, 8).Value = ListView1.ListItems(lngCounter).SubItems(7)

    Next
    .SaveAs "c:\search.xls"
    End With
    'free memory
    objWbook.Close False
    Set objSheet = Nothing
    Set objWbook = Nothing
    objXcell.Quit
    Set objXcell = Nothing
    Screen.MousePointer = vbDefault
    End Sub
    /code]


    need to widen the colums in excel

  6. #6
    Join Date
    Dec 2004
    Posts
    423

    Re: Need Help Printing A Listview

    If you don't want to create a template for it.
    I'm pretty sure the code is
    Code:
    MySheet.Columns("A:A").Width = 'NewWidth
    Neat tip for you would be to open up excel and create a macro.
    Then do the things you need to do. Stop the macro. Open up VBA.
    In Module1 all the code you need will be there. You might have to do a little adjustment but you can learn a lot that way.

  7. #7

    Thumbs up Re: Need Help Printing A Listview

    thank you that worked perfect did what ya said opened up the vba got the code thanks alot but i do have one last question if you could be so generious i want to open the excel document after the search not sure if i need a shell command or what thanks alot for all your help

  8. #8
    Join Date
    May 2006
    Posts
    30

    Re: Need Help Printing A Listview

    Hi! I'm interested too in printing listview contents but instead of printing it to Excel, I would like to have a button click and directly print what is in the listview (i.e the grid, columnheaders and the contents). Any Sample code please?

  9. #9
    Join Date
    Dec 2004
    Posts
    423

    Re: Need Help Printing A Listview

    Hello and Welcome to Codegure.

    The listview doesn't have a print feature.
    I have just thought of something that you could try though.
    This theory is with your print button in code you'll resize the listview to view everything and print. The only hold up is I don't think you make a form bigger than your screen.
    Even if everybody spoke the same language, nobody would be speaking the same language.

    --Daniel

  10. #10
    Join Date
    May 2006
    Posts
    30

    Re: Need Help Printing A Listview

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured