|
-
Dec 21st, 2006, 05:21 PM
#1
Thread Starter
Member
Save listview to txt file
I need to save a listview to a txtfile. I want to save with a CD too.
-
Dec 21st, 2006, 05:41 PM
#2
Re: Save listview to txt file
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
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Dec 21st, 2006, 05:47 PM
#3
Thread Starter
Member
Re: Save listview to txt file
No, like save the file, as if you were to open one with Command Dialog, just save the contents of the listview instead.
-
Dec 21st, 2006, 06:02 PM
#4
Re: Save listview to txt file
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").
-
Dec 21st, 2006, 06:06 PM
#5
Re: Save listview to txt file
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....
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Dec 21st, 2006, 06:17 PM
#6
Re: Save listview to txt file
 Originally Posted by koolsid
Note: I Might be wrong....
your absolutely right.
anyway the code is above to save it
-
Dec 21st, 2006, 06:40 PM
#7
Re: Save listview to txt file
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
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Dec 22nd, 2006, 08:15 PM
#8
Thread Starter
Member
Re: Save listview to txt file
 Originally Posted by Mark Gambo
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.
-
Jan 5th, 2009, 11:52 AM
#9
Hyperactive Member
Re: Save listview to txt file
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:
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
I have post my code as well.
I'm still on the path of learning.... 
-
Jan 5th, 2009, 12:46 PM
#10
Re: Save listview to txt file
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
Last edited by Chris001; Jan 5th, 2009 at 12:50 PM.
-
Jan 5th, 2009, 10:02 PM
#11
Hyperactive Member
Re: Save listview to txt file
Thanks a lot, Chris001. You solved my problems. I'm kinda new to listview feature in VB. Anyway, thanks again.
I'm still on the path of learning.... 
-
Jan 17th, 2009, 08:23 AM
#12
Hyperactive Member
Re: Save listview to txt file
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'm still on the path of learning.... 
-
Jan 17th, 2009, 11:00 AM
#13
Re: Save listview to txt file
 Originally Posted by cheowkwen
Hi, i'm using the Chris001's code which is saving the listview items in the text file and it works .... 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:
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.
-
Jan 17th, 2009, 09:49 PM
#14
Hyperactive Member
Re: Save listview to txt file
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?
I'm still on the path of learning.... 
-
Jan 17th, 2009, 10:15 PM
#15
Re: Save listview to txt file
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
Last edited by LaVolpe; Jan 17th, 2009 at 10:46 PM.
-
Jan 17th, 2009, 10:38 PM
#16
New Member
-
Jan 17th, 2009, 10:50 PM
#17
Hyperactive Member
Re: Save listview to txt file
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.
I'm still on the path of learning.... 
-
Feb 10th, 2009, 12:46 PM
#18
Hyperactive Member
Re: Save listview to txt file
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
-
Feb 10th, 2009, 01:36 PM
#19
Re: Save listview to txt file
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.
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
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.
-
Feb 10th, 2009, 02:57 PM
#20
Hyperactive Member
Re: Save listview to txt file
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|