Solved

How do I save VB6 RichTextBox multiline data to a file line-by-line?

Posted on 2013-10-23
11
1,074 Views
Last Modified: 2013-11-22
I need VB6 help in saving RichTextBox multiline data to a file.

I have a baseball game that records the play by play into a RichTextBox (RTB). The boxscore and the player statistic MSFlexGrids write to an HTML file. I want to combine the two; I want to put the play by play text at the bottom of the html report. When I try to get the RTB text to save to the html file, it always looks like this:


* Groundball-1B.......J Gilliam * K.......D Snider * Popup-SS.....G Hodges ******************************************* [End T1] * K.......L Doby * BB.......B Avila * Flyball-LC.....A Smith     Flyball---runner on first might go---he's going---S Amoros's throw is in there---OUT!---NO....the ball is loose, SAFE! * TWO BASES on that one for A Rosen...nice hitting---Double-xLF * K.......D Philley ******************************************* [End B1] * K.......P Reese * BB.......C Furillo
* Flyball-xLF.....R Campanella     Runner holds * Flyball-xRF.....S Amoros ******************************************* [End T2] * Flyball-LC.....J Hegan * Flyball-xLF.....V Wertz * K.......G Strickland ******************************************* [End B2]


I want it to look like this:


* Groundball-1B.......J Gilliam
* K.......D Snider
* Popup-SS.....G Hodges
******************************************* [End T1]
* K.......L Doby
* BB.......B Avila
* Flyball-LC.....A Smith     Flyball---runner on first might go---he's going---S Amoros's throw is in there---OUT!---NO....the ball is loose, SAFE!
* TWO BASES on that one for A Rosen...nice hitting---Double-xLF
* K.......D Philley
******************************************* [End B1]
* K.......P Reese
* BB.......C Furillo
* Flyball-xLF.....R Campanella     Runner holds
* Flyball-xRF.....S Amoros
******************************************* [End T2]
* Flyball-LC.....J Hegan
* Flyball-xLF.....V Wertz
* K.......G Strickland
******************************************* [End B2]


I have the RightMargin of the RTB set at 400 less than the width of the RTB. I'm not sure if this setting of the right margin automatically adds vbCRLF to the end of a line, but it seems that it should.

A jpg of the html report from a game is attached here so you can see what it looks like. I just want to 'append' the play-by-play text to the html report, but I can't figure out how extract the play-by-play text from the RTB one line at a time.

Here's VB6 code for the html report from one of the MSFlexGrids:

Open SaveFileDialog1.FileName For Output As #iFree

Print #iFree, lbl_VisTeam.Caption & "_at_" & lbl_HomeTeam.Caption & "_" & tb_TimeStamp.Text
Print #iFree, "<html><head></head><body><table border=1 cellspacing=0 bordercolor=#C0C0C0 width=800>"
With grd_VisStats
For i = 0 To .Rows - 1
    Print #iFree, "<tr>"
    For j = 1 To .Cols - 1
        Print #iFree, "<td><font color=#000000>" & .TextMatrix(i, j) & "</td>"
    Next
    Print #iFree, "</tr>"
Next
Print #iFree, "</table></body><html>"
Print #iFree, "<td>&nbsp;</td>"
End With


Any assistance is GREATLY appreciated!
BB-html-rpt.jpg
0
Comment
Question by:jazjef
11 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39595894
This assumes an RTB named RichTextbox1 and a command button named Command1.
Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any _
) As Long

Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_GETLINE = &HC4
Private Const EM_LINELENGTH = &HC1

Private Sub PrintRTB(RTB As RichTextBox)
    Dim strBuffer As String
    Dim lngLength As Long
    Dim intCurrentLine As Integer
    Dim lngLineNumber As Long
    Dim intCount As Integer
    With RichTextBox1
        intCount = SendMessage(.hwnd, EM_GETLINECOUNT, 0&, 0&)
        For lngLineNumber = 0 To intCount - 1
            'get line length
            lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineNumber, 0)
            'resize buffer
            strBuffer = Space(lngLength)
            'get line text
            Call SendMessage(.hwnd, EM_GETLINE, lngLineNumber, ByVal strBuffer)
            Debug.Print strBuffer
        Next
    End With
End Sub

Private Sub Command1_Click()
PrintRTB RichTextBox1
End Sub

Open in new window

0
 
LVL 4

Author Comment

by:jazjef
ID: 39610450
MartinLiss;
I've tried and tried .... but here's all I can get using your code:

* It's a solid base hit for D * It's gonna roll forever.... Hodges---Triple-CFL * It's gonna roll forever.... Reese---Triple-RF * Single for C Furillo---Singl * Popup-SS.....R Campanella * Flyball-LF.....S Amoros ****************************** * Groundball-1B.......P Rose * Groundball-SS.......J Bench * Flyball-CFL.....T Perez ****************************** * That one might be out of her Hoak---Triple-LC * Popup-3B.....B Cox * K.......J Gilliam * BB.......D Snider * Flyball-LF.....G Hodges ****************************** * Flyball-xRF.....D Concepcion * Groundball-3B.......C Geroni * Flyball-LC.....J Morgan ****************************** * Popup-2B.....P Reese * Flyball-xRF.....C Furillo * Groundball-1B.......R Campan ****************************** * Flyball-RF.....G Foster * K Griffey stuck with it...ba Single-CF * Base hit for D Driessen...te CF * Flyball-LC.....P Rose Runners HOLD * K.......J Bench ******************************************* [End * Nice piece of hitting by S Amoros...that's a s Single-RF * K.......D Hoak * DOUBLE BAGGER for B Cox---Double-LC * BB.......J Gilliam * Base hit for D Snider---Single-RF * K.......G Hodges * Groundball-2B.......P Reese force OUT at 2 ******************************************* [End * DOUBLE T Perez---Double-LC * Groundball-1B.......D Concepcion OUT at fi runner on 2nd moves up * Popup-3B.....C Geronimo * BB.......J Morgan * Groundball-P.......G Foster ******************************************* [End * Popup-SS.....C Furillo


Formatting is still lost...... I can get this same result with something as simple as:

Print #iFree, RichTextBox1.Text
0
 
LVL 1

Expert Comment

by:TheGiops
ID: 39618255
Use the RichTextBox.Rtf  - not RichTextBox.Text !!!!
0
 
LVL 15

Expert Comment

by:eemit
ID: 39618335
Try this:
CommonDialog1.ShowSave
RichTextBox1.SaveFile CommonDialog1.FileName, rtfText

Open in new window

0
 
LVL 4

Author Comment

by:jazjef
ID: 39618745
I need to be able to incorporate the MSFlexGrid data and the RichTextBox data into the same file.

"RichTextBox1.SaveFile CommonDialog1.FileName, rtfText" will indeed save the RichTextBox data as a SEPARATE file that is formatted-----but I don't want 2 files-----I want 1 file with my MSFLexGrid data and my RichTextBox data (without losing the RTB formatting).

*** Notice how the posted code will 'loop' through the MSFlexGrid and write the HTML for the MSFlexGrid. I need a statement to add to the MSFlexGrid code that will 'loop' through the RichTextBox one line at a time, and render it as formatted HTML in the same HTML document that has the MSFlexGrid data.

With RichtTextBox1
   For each unique line of data in RichTextBox1 (beginning with the first line)
      Select a line
      Print the line (in HTML format)
   Next line
End With
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 15

Expert Comment

by:eemit
ID: 39618954
Tra to change your code for the html report as:

'/
'Open SaveFileDialog1.FileName For Output As #iFree
Open CommonDialog1.FileName For Append As #iFree
'/

Print #iFree, lbl_VisTeam.Caption & "_at_" & lbl_HomeTeam.Caption & "_" & tb_TimeStamp.Text
Print #iFree, "<html><head></head><body><table border=1 cellspacing=0 bordercolor=#C0C0C0 width=800>"
With grd_VisStats
For i = 0 To .Rows - 1
    Print #iFree, "<tr>"
    For j = 1 To .Cols - 1
        Print #iFree, "<td><font color=#000000>" & .TextMatrix(i, j) & "</td>"
    Next
    Print #iFree, "</tr>"
Next
'/
'Print #iFree, "</table></body><html>"
'Print #iFree, "<td>&nbsp;</td>"
'End With

Print #iFree, "</table>
End With

Print #iFree, "<pre>"
RichTextBox1.SaveFile CommonDialog1.FileName, rtfText
Print #iFree, "</pre>"

Print #iFree, "</body><html>"

Close #iFree
'/

Open in new window

0
 
LVL 4

Author Comment

by:jazjef
ID: 39619354
Here's what I get eemit:

   Run-time error '75:

   Path/File access error

I see what you code is trying to do----it tries to 'append' to the file after writing the msflexgrid----I think this is a great idea. Some sort of 'append' might be the answer. At the moment, the error abovei s what I get....
0
 
LVL 15

Expert Comment

by:eemit
ID: 39619440
Sorry for typos, the line:
Open CommonDialog1.FileName For Append As #iFree

Open in new window

should be:
Open SaveFileDialog1.FileName For Append As #iFree

Open in new window

and:
RichTextBox1.SaveFile CommonDialog1.FileName, rtfText

Open in new window

should be:
RichTextBox1.SaveFile SaveFileDialog1.FileName, rtfText

Open in new window

0
 
LVL 4

Accepted Solution

by:
jazjef earned 0 total points
ID: 39655439
I solved my own problem---I decided to use a TextBox and finally found some code that works; here's the solution:


Open SaveFileDialog1.FileName For Output As #iFree
With TextBox1
Dim HTDOC As Variant
Dim CurLin
Dim FinalHTML As String
    HTDOC = TextBox1.Text
    HTDOC = Split(HTDOC, vbNewLine)
    DoEvents
    For i = 0 To UBound(HTDOC)
        CurLin = HTDOC(i)
        FinalHTML = FinalHTML & CurLin & "<br>"
    Next i
    DoEvents
    Print #iFree, FinalHTML
Close #iFree
End With
0
 
LVL 15

Expert Comment

by:eemit
ID: 39666008
jazjef,
It solves the problem I had. Posts from others did not.
What is wrong with solution I posted?
0
 
LVL 4

Author Closing Comment

by:jazjef
ID: 39668434
It solves the problem I had. Posts from others did not.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now