|
-
Feb 10th, 2004, 03:29 PM
#1
Thread Starter
New Member
Printing the contents of a listbox in VBA
I have a UserFrom with a combobox (called cboEmpList), a commandbuttom (called cmdUpdate), and a listbox (called lstReport). When I select an item from the combobox and hit the command button the listbox is populated with some formatted text. I can do this multiple times to create a report of hours spent by employees on a project. I am having trouble creating a button that will take the contents of the listbox and write it to a text file called "hours.txt" which I can print at a later time. Can anyone offer some assistance.
-
Feb 11th, 2004, 04:29 AM
#2
Junior Member
what sort of trouble are you having with this ? Writing to the text file should be fairly easy, in the example below I've ommitted the sheet name from the front of the listbox object (ie sheet1 or form1 or whatever you have it on) but the principle works.
Sub Print_Me()
Open "hours.txt" For Input As #1
For i = 0 To (lstReport.ListCount - 1)
Write #1, Cells(i, 2).Value
Next i
Close #1
End Sub
"much to learn you still have"
-
Feb 11th, 2004, 12:58 PM
#3
Thread Starter
New Member
Thanks Shattered, that for next loop is the right idea, except it hangs up on the "write #1" statement. It gives me an "application-defined or object-defined error. Here is the code for this button I have so far:
Sub cmdReport_Click()
'On Error Resume Next
Dim counter
Dim MyDate As Variant
counter = 0
Kill "C:\TimeRpt.txt"
Open "C:\TimeRpt.txt" For Output As #1
MyDate = FileDateTime("c:\TimeRpt.txt") ' Assign date
Print #1, MyDate
For i = 0 To (lstReport.ListCount - 1)
Write #1, Cells(i, 2).Value ' Here is where the problem is
Next i
Me.Hide
Sheets("Open Page").Activate
Range("A1").Select
MsgBox "Please open and print C:\TimeRpt.txt.", vbOKOnly, "Document created"
Close #1
Unload Me
End Sub
I the past I have used the "Print #1" statement although it didn't work either when I used it in place of the "write #1" statement.
Any other help would be deeply appreciated.
-
Feb 11th, 2004, 04:26 PM
#4
Thread Starter
New Member
After some more thought and searching, I have found the answer.
The following is the code that works, now I just need to figure out how to format my text doc so that the headings line up on screen in the listbox and in file.
Sub cmdReport_Click()
On Error Resume Next
For iList = 0 To (lstReport.ListCount - 1)
sList = sList & lstReport.List(iList) & vbCrLf
Next iList
Kill "C:\TimeRpt.txt"
Open "C:\TimeRpt.txt" For Output As #1
MyDate = FileDateTime("c:\TimeRpt.txt") ' Assign date
Print #1, MyDate
Print #1, " "
Print #1, sList
Close #1
Me.Hide
Sheets("Open Page").Activate
Range("A1").Select
MsgBox "Please open and print C:\TimeRpt.txt.", vbOKOnly, "Document created"
Unload Me
End Sub
VBA dosen't have a .list option so sList = sList & lstReport.List(iList) & vbCrLf creates it (variables where dim'ed in the top of the form - so they can be used by all forms)
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
|