escheider
asked on
Parsing Text
Hello Experts!
I have a RichTextBox that will have a variable length string in it a various times. What Im trying to do, is go through the textbox, similiar to parsing a text file, and extracting the data that I need. Here is an example of the text:
Summary
Property Location 9247 CHENAULT RD
Fire District 09 Pleasure Ridge Park
School District 0 Bailey County
Property Class 510 Res 1 family dwelling
Neighborhood 295
Satellite City No
Finished Square Feet 900
Acres
Year Built 1957
Full Bathrooms 1
Half Bathrooms 0
Old District 25
Legal Description
Property Information
LAFAYETTE PARK
Owner Information
Owner Information
DOE JOHN
Mail Information
9247 CHENAULT RD
ANYWHERE KY 40000
Assessment Info
Exemption: None
Land $17,000.00
Improvement $49,100.00
Total $66,100.00
Last Sale
Sale Amount $57,500.00
Date 03/04/98
Deed Book/Page 7012 334
For example, I need to capture the value next to Land, Improvement and Total. Question is, how do you sequentially go line by line through a text box?
Escheider
I have a RichTextBox that will have a variable length string in it a various times. What Im trying to do, is go through the textbox, similiar to parsing a text file, and extracting the data that I need. Here is an example of the text:
Summary
Property Location 9247 CHENAULT RD
Fire District 09 Pleasure Ridge Park
School District 0 Bailey County
Property Class 510 Res 1 family dwelling
Neighborhood 295
Satellite City No
Finished Square Feet 900
Acres
Year Built 1957
Full Bathrooms 1
Half Bathrooms 0
Old District 25
Legal Description
Property Information
LAFAYETTE PARK
Owner Information
Owner Information
DOE JOHN
Mail Information
9247 CHENAULT RD
ANYWHERE KY 40000
Assessment Info
Exemption: None
Land $17,000.00
Improvement $49,100.00
Total $66,100.00
Last Sale
Sale Amount $57,500.00
Date 03/04/98
Deed Book/Page 7012 334
For example, I need to capture the value next to Land, Improvement and Total. Question is, how do you sequentially go line by line through a text box?
Escheider
Give this a shot
With RTB
StartLocation = InStr(1, RTB.Text, "LAND")
StartLocation = StartLocation + 4 'adjust accordingly (just to the right of "Land"
RTB.SelStart = StartLocation
RTB.SelLength = 12 'Adjust Accordingly
MsgBox Format(Val(RTB.SelText), "$##,###,###.00")
End With
With RTB
StartLocation = InStr(1, RTB.Text, "LAND")
StartLocation = StartLocation + 4 'adjust accordingly (just to the right of "Land"
RTB.SelStart = StartLocation
RTB.SelLength = 12 'Adjust Accordingly
MsgBox Format(Val(RTB.SelText), "$##,###,###.00")
End With
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'll raise points if I have to, but here is another question on the same text.
How would I obtain the text below the label..as an example
Mail Information
9247 CHENAULT RD
ANYWHERE KY 40000
I want to get the address information, but there isn't a clear tag to the left of it.
You other example seems to work fine. Thanks.
How would I obtain the text below the label..as an example
Mail Information
9247 CHENAULT RD
ANYWHERE KY 40000
I want to get the address information, but there isn't a clear tag to the left of it.
You other example seems to work fine. Thanks.
Award the points as you see fit
Here ya go
Private Sub Command1_Click()
Dim StartLocation As Long
Dim StopLocation As Long
With RTB
StartLocation = InStr(1, RTB.Text, "Land")
StartLocation = StartLocation + 4 'adjust accordingly (just to the right of "Land"
RTB.SelStart = StartLocation
RTB.SelLength = 12 'Adjust Accordingly
'MsgBox Format(RTB.SelText, "$##,###,###.00")
StartLocation = InStr(1, RTB.Text, "Mail Information") + 16
StopLocation = InStr(1, RTB.Text, "Assessment Info") - 1
RTB.SelStart = StartLocation
RTB.SelLength = StopLocation - StartLocation
MsgBox Trim(RTB.SelText)
End With
End Sub
Here ya go
Private Sub Command1_Click()
Dim StartLocation As Long
Dim StopLocation As Long
With RTB
StartLocation = InStr(1, RTB.Text, "Land")
StartLocation = StartLocation + 4 'adjust accordingly (just to the right of "Land"
RTB.SelStart = StartLocation
RTB.SelLength = 12 'Adjust Accordingly
'MsgBox Format(RTB.SelText, "$##,###,###.00")
StartLocation = InStr(1, RTB.Text, "Mail Information") + 16
StopLocation = InStr(1, RTB.Text, "Assessment Info") - 1
RTB.SelStart = StartLocation
RTB.SelLength = StopLocation - StartLocation
MsgBox Trim(RTB.SelText)
End With
End Sub
escheider:
You can easily extract any line of a TextBox or a RichText box by using a couple of functions I've written: tbGetLineCount() and tbGetLine()
tbGetLineCount tells you how many lines are in your text box. A line of text could terminated by a carriage return and line feed, or it could end because of word-wrapping.
tbGetLine retrieves the line specified by the line index (first line is line #0).
Example:
Dim idxLine As Long
Dim strLine As String
idxLine = 0
For idxLine = 0 To tbGetLineCount(RichText1)
strLine = tbGetLine(RichText1, idxLine)
If StrComp(Left(strLine,4),"L and",vbTex tCompare)= 0 Then
' *** you've found land!
End If
'*** you can look for other words also
Next 'idxLine
To use this, add the following code to a Standard Code Module:
Option Explicit
Public Const EM_GETLINE = &HC4
Public Const EM_GETLINECOUNT = &HBA
Public Const EM_LINELENGTH = &HC1
Public Const EM_LINEINDEX = &HBB
Public Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
Public Declare Function SendMessageByStr Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As String _
) As Long
Public Function tbGetLine(tb As Object, ByVal idxLine As Long) As String
Dim idxCh As Long
Dim strLine As String
Dim LineLen As Long
idxCh = SendMessageByNum(tb.hWnd, EM_LINEINDEX, idxLine, 0&)
LineLen = SendMessageByNum(tb.hWnd, EM_LINELENGTH, idxCh, 0&)
strLine = Chr(LineLen And &HFF) & Chr((LineLen And &HFF00) / &H100) & Space(LineLen - 2)
SendMessageByStr tb.hWnd, EM_GETLINE, idxLine, strLine
tbGetLine = strLine
End Function
Public Function tbGetLineCount(tb As Object) As Long
tbGetLineCount = SendMessageByNum(tb.hWnd, EM_GETLINECOUNT, 0&, 0&)
End Function
-Dennis Borg
You can easily extract any line of a TextBox or a RichText box by using a couple of functions I've written: tbGetLineCount() and tbGetLine()
tbGetLineCount tells you how many lines are in your text box. A line of text could terminated by a carriage return and line feed, or it could end because of word-wrapping.
tbGetLine retrieves the line specified by the line index (first line is line #0).
Example:
Dim idxLine As Long
Dim strLine As String
idxLine = 0
For idxLine = 0 To tbGetLineCount(RichText1)
strLine = tbGetLine(RichText1, idxLine)
If StrComp(Left(strLine,4),"L
' *** you've found land!
End If
'*** you can look for other words also
Next 'idxLine
To use this, add the following code to a Standard Code Module:
Option Explicit
Public Const EM_GETLINE = &HC4
Public Const EM_GETLINECOUNT = &HBA
Public Const EM_LINELENGTH = &HC1
Public Const EM_LINEINDEX = &HBB
Public Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
Public Declare Function SendMessageByStr Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As String _
) As Long
Public Function tbGetLine(tb As Object, ByVal idxLine As Long) As String
Dim idxCh As Long
Dim strLine As String
Dim LineLen As Long
idxCh = SendMessageByNum(tb.hWnd, EM_LINEINDEX, idxLine, 0&)
LineLen = SendMessageByNum(tb.hWnd, EM_LINELENGTH, idxCh, 0&)
strLine = Chr(LineLen And &HFF) & Chr((LineLen And &HFF00) / &H100) & Space(LineLen - 2)
SendMessageByStr tb.hWnd, EM_GETLINE, idxLine, strLine
tbGetLine = strLine
End Function
Public Function tbGetLineCount(tb As Object) As Long
tbGetLineCount = SendMessageByNum(tb.hWnd, EM_GETLINECOUNT, 0&, 0&)
End Function
-Dennis Borg
>How would I obtain the text below the label..as an example
>
>Mail Information
>9247 CHENAULT RD
>ANYWHERE KY 40000
>
>I want to get the address information, but there isn't a
>clear tag to the left of it.
In the context of using the code I supplied, since you know the line number where you found "Mail Information", you can easily extract the data by retrieving the following 2 or 3 lines.
Here is some sample code to search for "Mail Information" and place the address into a string variable. For the sake of illustration, I'm assuming the name of your RichText box is "RichText1"
Dim idxMailInfo As Long 'Line Index of Mail Info Line
Dim strMailInfo As String 'Mailing Address
Dim LineCount As Long 'Number of lines in Rich Text Box
Dim Found As Boolean
LineCount = tbGetLineCount(RichText1)
idxMailInfo = 0
Do While Not Found
strMailInfo = Trim(tbGetLine(RichText1, idxMailInfo))
If StrComp(strMailInfo,"Mail Information", vbTextCompare) = 0 Then
Found = True
Else
idxMailInfo = idxMailInfo + 1
End If
Loop
If Found Then
strMailInfo = tbGetLine(RichText1, idxMailInfo + 1) & _
tbGetLine(RichText1, idxMailInfo + 2)
End If
This assumes only 2-line addresses, but can be modified however needed.
-Dennis Borg
>
>Mail Information
>9247 CHENAULT RD
>ANYWHERE KY 40000
>
>I want to get the address information, but there isn't a
>clear tag to the left of it.
In the context of using the code I supplied, since you know the line number where you found "Mail Information", you can easily extract the data by retrieving the following 2 or 3 lines.
Here is some sample code to search for "Mail Information" and place the address into a string variable. For the sake of illustration, I'm assuming the name of your RichText box is "RichText1"
Dim idxMailInfo As Long 'Line Index of Mail Info Line
Dim strMailInfo As String 'Mailing Address
Dim LineCount As Long 'Number of lines in Rich Text Box
Dim Found As Boolean
LineCount = tbGetLineCount(RichText1)
idxMailInfo = 0
Do While Not Found
strMailInfo = Trim(tbGetLine(RichText1, idxMailInfo))
If StrComp(strMailInfo,"Mail Information", vbTextCompare) = 0 Then
Found = True
Else
idxMailInfo = idxMailInfo + 1
End If
Loop
If Found Then
strMailInfo = tbGetLine(RichText1, idxMailInfo + 1) & _
tbGetLine(RichText1, idxMailInfo + 2)
End If
This assumes only 2-line addresses, but can be modified however needed.
-Dennis Borg
I forgot to add the CR/LF in the address. Here is the correction:
strMailInfo = tbGetLine(RichText1, idxMailInfo + 1) & vbCrLf & _
tbGetLine(RichText1, idxMailInfo + 2)
-Dennis Borg
strMailInfo = tbGetLine(RichText1, idxMailInfo + 1) & vbCrLf & _
tbGetLine(RichText1, idxMailInfo + 2)
-Dennis Borg
Some answers:
* To capture the clipboard, you can use the Clipboard object and use Clipboard.GetText
* To extract multiple "lines" in a textbox, use Split(Text1.text, vbCrLf)
Once the lines are split, you still need to figure out where the "fieldname" ends and the "data" begins.
For this I recommend inserting the field names into a database, then you can extract each word and search; if not found, then search for the first two words, etc. When you get a match, the rest of the line is the data.
* To capture the clipboard, you can use the Clipboard object and use Clipboard.GetText
* To extract multiple "lines" in a textbox, use Split(Text1.text, vbCrLf)
Once the lines are split, you still need to figure out where the "fieldname" ends and the "data" begins.
For this I recommend inserting the field names into a database, then you can extract each word and search; if not found, then search for the first two words, etc. When you get a match, the rest of the line is the data.
>When you get a match, the rest of the line is the data.
Except for the cases where the field names are on one line, and the value is contained in the following line(s).
For example: Mailing Information
-Dennis Borg
Except for the cases where the field names are on one line, and the value is contained in the following line(s).
For example: Mailing Information
-Dennis Borg
Ah yes, so the DB should also probably indicate whether the data is on the same line or ???
It's not a perfect answer, though, because the data could actually contain the "field" names.
Ideally, the incoming text should have some sort of delimiter, like an "=".
How is the incoming data arriving in that format? Do you have control over it?
It's not a perfect answer, though, because the data could actually contain the "field" names.
Ideally, the incoming text should have some sort of delimiter, like an "=".
How is the incoming data arriving in that format? Do you have control over it?
ASKER
Wow, thanks for all the input. Works great. DennisBorg, I will be awarding you points as well,(I used some of your examples too), so please check the VB section.
Escheider
Escheider
>Wow, thanks for all the input. Works great. DennisBorg,
>I will be awarding you points as well,(I used
>some of your examples too), so please check the VB section.
You're welcome, Escheider. And thank you for the points.
-Dennis Borg
>I will be awarding you points as well,(I used
>some of your examples too), so please check the VB section.
You're welcome, Escheider. And thank you for the points.
-Dennis Borg
ASKER
Im using VB6 SP5