loop in listview element and print in tabular mode

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

loop in listview element and print in tabular mode

Post by sal21 »

I need to loop in a listview all cells and all column and print the value in tabular mode in a txt file...
but without the last column (is hided i use that for a index)

now i have a liitle difficult :grin: :scratch:

Similar:

| qnsdataID | qnsID | ansID |
-------------------------------------
| qd0001 | qns0002 | ans0012 |
| qd0002 | qns0002 | ans0016 |
| qd0003 | qns0002 | ans0001 |
-------------------------------------

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: loop in listview element and print in tabular mode

Post by HansV »

Try this as starting point:

Code: Select all

    Const c = 25
    Dim i As Long
    Dim j As Long
    Dim m As Long
    Dim n As Long
    Dim f As Integer
    Dim s As String
    Dim t As String
    f = FreeFile
    Open "C:\Output\Test.txt" For Output As #f
    m = Me.ListView1.ListItems.Count
    n = Me.ListView1.ColumnHeaders.Count
    For j = 1 To n - 1
        t = Me.ListView1.ColumnHeaders(j).Text
        s = s & t & Space(c - Len(t))
    Next j
    Print #f, Trim(s)
    For i = 1 To n
        t = Me.ListView1.ListItems(i).Text
        s = t & Space(c - Len(t))
        For j = 1 To n - 2
            t = Me.ListView1.SubItems(j)
            s = s & t & Space(c - Len(t))
        Next j
        Print #f, Trim(s)
    Next i
    Close #f
I can't test it since I can't use listview.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: loop in listview element and print in tabular mode

Post by sal21 »

HansV wrote:Try this as starting point:

Code: Select all

    Const c = 25
    Dim i As Long
    Dim j As Long
    Dim m As Long
    Dim n As Long
    Dim f As Integer
    Dim s As String
    Dim t As String
    f = FreeFile
    Open "C:\Output\Test.txt" For Output As #f
    m = Me.ListView1.ListItems.Count
    n = Me.ListView1.ColumnHeaders.Count
    For j = 1 To n - 1
        t = Me.ListView1.ColumnHeaders(j).Text
        s = s & t & Space(c - Len(t))
    Next j
    Print #f, Trim(s)
    For i = 1 To n
        t = Me.ListView1.ListItems(i).Text
        s = t & Space(c - Len(t))
        For j = 1 To n - 2
            t = Me.ListView1.SubItems(j)
            s = s & t & Space(c - Len(t))
        Next j
        Print #f, Trim(s)
    Next i
    Close #f
I can't test it since I can't use listview.
hummmmmmm.... see image, please :sad: :scratch:
You do not have the required permissions to view the files attached to this post.

User avatar
Claude
cheese lizard
Posts: 6241
Joined: 16 Jan 2010, 00:14
Location: Sydney Australia

Re: loop in listview element and print in tabular mode

Post by Claude »

What does the error message mean in English ?
Cheers, Claude.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: loop in listview element and print in tabular mode

Post by HansV »

Try changing the offending line to

Code: Select all

            t = Me.ListView1.ListItems(i).SubItems(j)
Best wishes,
Hans