Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1320
  • Last Modified:

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

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
jazjef
Asked:
jazjef
1 Solution
 
Martin LissOlder than dirtCommented:
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
 
jazjefAuthor Commented:
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
 
TheGiopsCommented:
Use the RichTextBox.Rtf  - not RichTextBox.Text !!!!
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
eemitCommented:
Try this:
CommonDialog1.ShowSave
RichTextBox1.SaveFile CommonDialog1.FileName, rtfText

Open in new window

0
 
jazjefAuthor Commented:
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
 
eemitCommented:
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
 
jazjefAuthor Commented:
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
 
eemitCommented:
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
 
jazjefAuthor Commented:
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
 
eemitCommented:
jazjef,
It solves the problem I had. Posts from others did not.
What is wrong with solution I posted?
0
 
jazjefAuthor Commented:
It solves the problem I had. Posts from others did not.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now