I need to save a listview to a txtfile. I want to save with a CD too.
Printable View
I need to save a listview to a txtfile. I want to save with a CD too.
This will create a text file that will include the SubItems:
VB Code:
Option Explicit Private Sub Command1_Click() Dim lvwItem As MSComctlLib.ListItem Dim a As Long, b As Long Dim strText As String With ListView1 .View = lvwReport With .ColumnHeaders .Clear For a = 1 To 4 .Add , "_" & a, "Col " & a, (ListView1.Width * 0.99) / 4 Next a End With With .ListItems .Clear For a = 1 To 15 Set lvwItem = .Add(, "_" & a, "Item " & a) For b = 1 To 3 lvwItem.SubItems(b) = a & "SubText " & b Next b Next a End With End With Open "c:\TextBox.txt" For Output As #1 With ListView1 With .ListItems For a = 1 To .Count Set lvwItem = .Item(a) strText = strText & lvwItem.Text & ", " For b = 1 To 3 strText = strText & lvwItem.SubItems(b) & ", " Next b strText = Left(Trim(strText), Len(Trim(strText)) - 1) Print #1, strText strText = "" Next a End With End With Close #1 End Sub
No, like save the file, as if you were to open one with Command Dialog, just save the contents of the listview instead.
That's what the examples do.. the Common Dialog only gets the file name for you (use .ShowSave to show the Save As dialog), you need to write the code (which ZenDisaster and MarkGambo have done) to actually save the file.
Simply change the 'Open' line in the examples above to use the filename that you get from the dialog (instead of App.Path & "\save.txt" or "c:\TextBox.txt").
Hi
I don't think there is an automatic way to save listview data. You will need to extract each piece of information and write it out to a file. The common dialog does nothing but provide a convenient way for the user to select a filename ... the actual saving is up to you.
Note: I Might be wrong....
your absolutely right.Quote:
Originally Posted by koolsid
anyway the code is above to save it
Here is an example with the CDL Control:
VB Code:
Option Explicit Private Sub Command1_Click() On Error GoTo Command1_Click_Error Dim lvwItem As MSComctlLib.ListItem Dim a As Long, b As Long Dim strText As String Dim intSubColCount As Integer intSubColCount = 3 With ListView1 .View = lvwReport With .ColumnHeaders .Clear For a = 1 To intSubColCount + 1 .Add , "_" & a, "Col " & a, (ListView1.Width * 0.99) / 4 Next a End With With .ListItems .Clear For a = 1 To 15 Set lvwItem = .Add(, "_" & a, "Item " & a) For b = 1 To intSubColCount lvwItem.SubItems(b) = a & "SubText " & b Next b Next a End With End With With CommonDialog1 .CancelError = True .InitDir = "C:\" .DialogTitle = "Select Text File Name" .FileName = "SaveText.Txt" .Filter = "Text (*.txt) | *.txt" .ShowSave If Len(Trim(.FileName)) = 0 Then Exit Sub Open .FileName For Output As #1 End With With ListView1 With .ListItems For a = 1 To .Count strText = "" Set lvwItem = .Item(a) strText = strText & lvwItem.Text & "," For b = 1 To 3 strText = strText & lvwItem.SubItems(b) & "," Next b strText = Left(Trim(strText), Len(Trim(strText)) - 1) Print #1, strText Next a End With End With Close #1 On Error GoTo 0 Exit Sub Command1_Click_Error: If Err.Number = 32755 Then Exit Sub 'User pressed Cancel on CDL Else MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Command1_Click of Form Form1" End If End Sub
Thanks, it works. But when I save it, there are three commas like this ,,, at the end of each line.Quote:
Originally Posted by Mark Gambo
Hi, I have tried the code given by Mark Gambo, that one with the CDL control. It's not work for me. It gives weird result. May i know what is the problem? Need help, please...
My listview is suppose as shown in below but the texfile gives weird result as shown below:
I have post my code as well.Code:Option Explicit
Private Sub Form_Load()
' Set the View
ListView1.View = lvwReport
' Add the columns
With ListView1.ColumnHeaders
.Add , , "Latitude"
.Add , , "Longitude"
.Add , , "Time"
End With
End Sub
Private Sub Command1_Click()
With ListView1.ListItems
.Add , , Text1.Text 'add the latitude
End With
With ListView1
.ListItems(ListView1.ListItems.Count).SubItems(1) = Text2.Text
.ListItems(ListView1.ListItems.Count).SubItems(2) = Time
End With
End Sub
Private Sub Command2_Click()
On Error GoTo Command1_Click_Error
Dim lvwItem As MSComctlLib.ListItem
Dim a As Long, b As Long
Dim strText As String
Dim intSubColCount As Integer
intSubColCount = 3
With ListView1
.View = lvwReport
With .ColumnHeaders
.Clear
For a = 1 To intSubColCount + 1
.Add , "_" & a, "Col " & a, (ListView1.Width * 0.99) / 4
Next a
End With
With .ListItems
.Clear
For a = 1 To 15
Set lvwItem = .Add(, "_" & a, "Item " & a)
For b = 1 To intSubColCount
lvwItem.SubItems(b) = a & "SubText " & b
Next b
Next a
End With
End With
With CommonDialog1
.CancelError = True
.InitDir = "C:\"
.DialogTitle = "Select Text File Name"
.FileName = "SaveText.Txt"
.Filter = "Text (*.txt) | *.txt"
.ShowSave
If Len(Trim(.FileName)) = 0 Then Exit Sub
Open .FileName For Output As #1
End With
With ListView1
With .ListItems
For a = 1 To .Count
strText = ""
Set lvwItem = .Item(a)
strText = strText & lvwItem.Text & ","
For b = 1 To 3
strText = strText & lvwItem.SubItems(b) & ","
Next b
strText = Left(Trim(strText), Len(Trim(strText)) - 1)
Print #1, strText
Next a
End With
End With
Close #1
On Error GoTo 0
Exit Sub
Command1_Click_Error:
If Err.Number = 32755 Then
Exit Sub 'User pressed Cancel on CDL
Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Command1_Click of Form Form1"
End If
End Sub
That's not a weird result. Mark Gambo's example first loads data into the Listview (overwriting the existing data) and then saves it to a text file. Remove the part of the code that loads the data into the Listview.
Try this.
Code:Private Sub Command2_Click()
On Error GoTo Command1_Click_Error
Dim lvwItem As MSComctlLib.ListItem
Dim a As Long, b As Long
Dim strText As String
Dim FF As Integer
With CommonDialog1
.CancelError = True
.InitDir = "C:\"
.DialogTitle = "Select Text File Name"
.FileName = "SaveText.Txt"
.Filter = "Text (*.txt) | *.txt"
.ShowSave
If Len(Trim(.FileName)) = 0 Then Exit Sub
FF = FreeFile
Open .FileName For Output As #FF
End With
With ListView1
With .ListItems
For a = 1 To .Count
strText = ""
Set lvwItem = .Item(a)
strText = strText & lvwItem.Text & ","
For b = 1 To 2
strText = strText & lvwItem.SubItems(b) & ","
Next b
strText = Left(Trim(strText), Len(Trim(strText)) - 1)
Print #FF, strText
Next a
End With
End With
Close #FF
On Error GoTo 0
Exit Sub
Command1_Click_Error:
If Err.Number = 32755 Then
Exit Sub 'User pressed Cancel on CDL
Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Command1_Click of Form Form1"
End If
End Sub
Thanks a lot, Chris001. You solved my problems. I'm kinda new to listview feature in VB. Anyway, thanks again.
Hi, i'm using the Chris001's code which is saving the listview items in the text file and it works. But there is one minor problem, when i click the button for saving, there is common dialog box pop up, if i don't want to save it and i click cancel, it does exit the common dialog. But there will always a null text file created in my app folder. What is the problem? Although this is does not create failure/error to my program, but i just wondering why this happened.
I doubt the file is being created by the code posted. If the user hits cancel, then the routine exits the function without creating a file. I think one of these things might apply:Quote:
Originally Posted by cheowkwen
1) You are using On Error Resume Next in the function
2) the file was already there
3) the file is being created by some other part of your project.
Hi, LaVolpe. I know what is the problem. I have set the .cancelerror to false. Therefore, it will always a null text file created at my app folder. But if i set it to true, when i click cancel, a error message pop up with error number 32755. However, based on my understanding of Chris001's code, if this error happens, it should exit sub, that mean there were no error message pop up and exit the common dialog even though i have set the .cancelerror to true.Am i right?
Did you remove his original On Error GoTo statement?
You can tweak his code a little if you like.
Code:Private Sub Command2_Click()
Dim lvwItem As MSComctlLib.ListItem
Dim a As Long, b As Long
Dim strText As String
Dim FF As Integer
On Error Resume Next ' slight change here
With CommonDialog1
.CancelError = True
.Flags = cdlOFNPathMustExist Or cdlOFNExplorer Or cdlOFNOverwritePrompt
' ^^ else you need to check yourself and create a path if necessary
.InitDir = "C:\"
' ^^ optional; can take line out & dialog will go to folder last visited
.DefaultExt = "txt"
' ^^ this will append txt to the filename if user doesn't provide it
.DialogTitle = "Select Text File Name"
.FileName = "SaveText.Txt"
' ^^ optional; can take line out or even use .FileName = ""
.Filter = "Text (*.txt) | *.txt"
End With
CommonDialog1.ShowSave
If Err Then ' user pressed cancel button
Err.Clear
Exit Sub
End If
On Error GoTo Command1_Click_Error
FF = FreeFile
Open CommonDialog1.FileName For Output As #FF
' if an error occurs in line above; file is locked and cannot be overwritten
' or drive/device is not ready, other potential errors possible
' add your listview save routine here
With ListView1
...
End With
Close #FF
FF = 0
Command1_Click_Error:
If Err Then
' error here can mean out of disk space, error in your save loop, other errors
' the err.description will help explain it
If FF Then Close #FF
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Command1_Click of Form Form1"
Err.Clear
End If
End Sub
Hi, LaVolpe. I having the same problem with cheowkwen. It always create a null txt file in the app folder. I have played around with your code. It solved the null text file and finally I get to know what is problem that caused the null text file. Thanks a lot. :thumb: :thumb: :thumb: Thanks for cheowkwen as well to bring up this matter.
LaVolpe,
The problem is at the On Error GoTo statement?! I got it!! I have tried to tweak the code as what you taught and it solved the problem. Thanks for your help, LaVolpe.
Thanks for cheowkwen as well to bring up this matter.
You are welcome, log85. We are just learning from each others.
Just to avoid opening a new thread,I'll ask my question here.I hope that's OK.
I have a similar problem.I can save data from listview to .txt file,but I would also like to save the ColumnHeaders,so my saved txt file would look something like this:
NAME SURNAME AGE
xxxx yyyyyy zzzz
This is my code:
Code:Private Sub cmdExport_Click()
Dim lvwItem As MSComctlLib.ListItem
Dim x As Long, y As Long
Dim StringTekst As String
Dim TT As Integer
On Error Resume Next
With CommonDialog1
.CancelError = True
.InitDir = "C:\"
.DialogTitle = "Izaberi Ime Za Fajl"
.FileName = "Spremi me.txt"
.Filter = "Text (*.txt) | *.txt"
End With
CommonDialog1.ShowSave
If Err Then
Err.Clear
Exit Sub
End If
On Error GoTo Command1_ClickError
TT = FreeFile
Open CommonDialog1.FileName For Output As #TT
With ListView1
With .ListItems
For x = 1 To .Count
StringTekst = ""
Set lvwItem = .Item(x)
StringTekst = StringTekst & lvwItem.Text & " "
For y = 1 To 8
StringTekst = StringTekst & lvwItem.SubItems(y) & " "
Next y
StringTekst = Left(Trim(StringTekst), Len(Trim(StringTekst)) + 1)
Print #TT, StringTekst
Next x
End With
End With
Close #TT
TT = 0
On Error GoTo 0
Exit Sub
Code:Command1_ClickError:
If Err Then
If TT Then Close #TT
MsgBox "Error " & Err.Number & " (" & Err.Description & ") u proceduri na formi"""
Err.Clear
End If
End Sub
There is nothing wrong with starting a new thread. If the original author subscribed to this thread, then they are getting email notifications each time this post is replied to; not really fair to them.
The listview column headers can be looped thru just like the listview contents.
If storing this to same file as the listview items, you may want to add markers in your text file to indicate where columnheaders start/end and where the listivew items start. This way when you read them back, you know what your text file's input line refers to.Code:For X = 1 To ListView1.ColumnHeaders.Count
Print #TT, ListView1.ColumnHeaders(x).Text; ","; ListView1.ColumnHeaders(x).Width
' you can make the header attributes comma delimited or separate entries as desired
Next
Thank you for your reply.The code you gave me works fine,as long as I want to keep the ColumnHeaders under the listview items.
If I try to save the headers before(above-just like in ListView) the items,it all looks so messed up.
I wouldn't like you to write the code for me,but if you could point me in the right direction,I would be grateful.