Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

-= How Can I Trap (Carriage Return & Line Feed) In A "RTF" File Using VB6 =-

521 views
Skip to first unread message

Jane Doe

unread,
Mar 6, 2003, 11:04:36 AM3/6/03
to
Here is the complete scenario:

I am making a label making program. The finished product gets saved
in/as RTF format.

For me to print an entire page of the same label, I want to grab the
first line, (from the .RTF file), and print it to the first top three
label lines at line position #1, then, likewise, grab line 2 from the
RTF, and print that to the top three labels at location line #2, and
so on.

That is not the problem, the problem is in retrieving each separate
line from the .RTF file.

I opened the .RTF file in Debug. Did a "D" dump just to see and make
sure that there were in fact Carriage Returns, and Line Feeds after
each line where I had in fact hit the Enter key, (0D-0A), and there
were, so I can use those to go by, but, heres the problem.

If I simply hit enter, when designing my label, to start my label on
the second line down, as opposed to flush up against the top, there is
no (0D-0A) in the RTF file signifying that the first line was simply
skipped.

First off, how can I read the RTF in VB6 to detect the 0D-0A's,
and second, how will I be able to detect that the first line, or any
line, has just been skipped via hitting the enter key?

Oci-One Kanubi

unread,
Mar 7, 2003, 11:49:07 AM3/7/03
to
Dear Jane,

I'm perplexed. Yer RTF file has to start with something
unintelligible to the normal human eye that looks something like this:

{\rtf1\pc\deff0{\fonttbl{\f0\fnil\fcharset0 Times New
Roman;{\f\fnil\fcharset0 Arial;}{\f2\fnil\fcharset2
Webdings;}}\viewkind4\pard

etc., going on for as many as 5,000 charaters (if it was wtitten by MS
Word) before the first line of actual text, which is STILL embedded in
codes, with no newline character-pairs.

Instead of opening it in Debug (why would you do such a thing? Is
there something about Debug I don't know? [Probably!]), try opening it
in Notepad. You will see that an RTF file doesn't necessarily have
any CRLF combinations at all. The newline indicator, equivalent to an
HTML "<br />", is "/par". This makes it kinda tuff to process with
straightforward VB I/O statements, since one Line Input #n is likely
to yield you thousands of characters.

MUST you write the data out in RTF in the first place? Can't you
write it out in plain text, so that you know exactly what you are
reading back in?

-Richard

======================================================================
Richard Hopley, Winston-Salem, NC, USA
rho...@earthlink.net 1-301-775-0471
Nothing really matters except Boats, Sex, and Rock'n'Roll
rho...@wfubmc.edu 1-336-713-5077
OK, OK; sound and serious programming for research also matters.
======================================================================


Jane Doe <ice...@yahoo.com> wrote in message news:<b5re6v0t5osj2qco3...@4ax.com>...


> Here is the complete scenario:
>
> I am making a label making program. The finished product gets saved
> in/as RTF format.
>
> For me to print an entire page of the same label, I want to grab the

> first line, (from the .RTF file), ...
>
[snip]

Jane Doe

unread,
Mar 7, 2003, 12:09:13 PM3/7/03
to
On 7 Mar 2003 08:49:07 -0800, rho...@earthlink.net (Oci-One Kanubi)
wrote:

>Dear Jane,
>
>I'm perplexed. Yer RTF file has to start with something
>unintelligible to the normal human eye that looks something like this:
>
>{\rtf1\pc\deff0{\fonttbl{\f0\fnil\fcharset0 Times New
>Roman;{\f\fnil\fcharset0 Arial;}{\f2\fnil\fcharset2
>Webdings;}}\viewkind4\pard

Yes, in fact it does look just like that.

>Instead of opening it in Debug (why would you do such a thing? Is
>there something about Debug I don't know? [Probably!])

Using Debug was an extreme move, but was the only way I could see any
embedded characters that notepad, or any other basic text displaying
program does not show.

>You will see that an RTF file doesn't necessarily have
>any CRLF combinations at all. The newline indicator, equivalent to an
>HTML "<br />", is "/par".

Yes, I did see the /par indicators, but like I had stated, I skip the
first line, and start on the second when making labels, and that first
line skip, does not show up as just a /par by itself, even though
common logic would dictate that it should. Thats part of the initial
question, how do I detect that first line skip, if there are no /par
statements for me to trap?


> This makes it kinda tuff to process with
>straightforward VB I/O statements, since one Line Input #n is likely
>to yield you thousands of characters.

Exactly, thats why I was hoping to discover a way that I was not
familiar with.


>MUST you write the data out in RTF in the first place? Can't you
>write it out in plain text, so that you know exactly what you are
>reading back in?

Oh I wish I could, but like I said, the finished products are labels,
with differnt Font's, different colors, you know.

Yes, straight text would have been a sinch, I would have been done by
now, ...LOL

D...@home.com

unread,
Mar 7, 2003, 12:32:35 PM3/7/03
to
If you are looking for vbCrLf's then use the Text property...
ie: lngPos = InStr(YourRTB.Text, vbCrLf)

Food for thought


On 7 Mar 2003 08:49:07 -0800, rho...@earthlink.net (Oci-One Kanubi) wrote:

Have a good day...

Don

Samir Talwar

unread,
Mar 9, 2003, 2:20:10 PM3/9/03
to
About different fonts etc. I would write this in my own version of
HTML, which I usually create as I'm writing the program to fit my
needs. Say, if I want a picture, I use <pic="blah.ext" pos="x,y">, and
code my program to recognise this.
Samir Talwar

Jane Doe <ice...@yahoo.com> wrote in message news:<j0kh6v80hp2csio8t...@4ax.com>...

Leo Swart

unread,
Mar 10, 2003, 8:33:24 PM3/10/03
to
Hello Samir

you seemed to failed the targed . . .

Please reviewed the question . . .

By. Leo

Samir Talwar

unread,
Mar 12, 2003, 1:46:30 PM3/12/03
to
I was suggesting an alternative to using RTF altogether, and therefore
avoiding her problem. I was telling Jane the way I would do it, and
letting her decide whether she wanted to.
Samir Talwar

"Leo Swart" <Leo....@t-online.de> wrote in message news:<b4jecv$j41$06$1...@news.t-online.com>...

Leo Swart

unread,
Mar 12, 2003, 6:26:17 PM3/12/03
to
Hello Samir

I believe You are the greatest :-)

> I was suggesting an alternative to using RTF altogether, and therefore
> avoiding her problem. I was telling Jane the way I would do it, and
> letting her decide whether she wanted to.

She has the problem while using any giving rtf.file. Please help in that
direction.

BTW reading Your first reply:

> About different fonts etc. YOU would write this in YOUR own version of
> HTML, which YOU usually create as YOU'r writing the program to fit YOUR
> needs. Say, if YOU want a picture, YOU use <pic="blah.ext" pos="x,y">, and
> code YOUR program to recognise this.
> Samir Talwar

By, Leo


J French

unread,
Mar 13, 2003, 5:29:37 AM3/13/03
to
On 12 Mar 2003 10:46:30 -0800, MadAndP...@msn.com (Samir Talwar)
wrote:

>I was suggesting an alternative to using RTF altogether, and therefore
>avoiding her problem. I was telling Jane the way I would do it, and
>letting her decide whether she wanted to.
>Samir Talwar

And it was a very good recommendation

Using a RichTextBox for a job like that is just plain daft

Printing needs to be layered
- something that 'wraps' the printing
- driven by a Template or Script

- this does the wrapping of the printing

Here is gPrint.cls followed by a Form

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "TGPrintObject"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

'====================================================================
'
' TGPrintObject.Cls
'
' A simple Encapsulation for a Print Target
' ie: Printer or PictureBox
'
' 14/03/01 je...@iss.u-net.com
'
'
'====================================================================
' 4/5/01 JF - Scalemode implemented properly for non Printer Objs
'
'
'
'====================================================================

Private Declare Function GetDeviceCaps Lib "gdi32" _
(ByVal hdc As Long, ByVal nindex As Long) As Long


Dim mObj As Variant
Dim mTopMargin#, mLeftMargin#
Dim mScaleMode%

' #################################################################
'
'
'

Public Property Set Device(Value As Object)
Set mObj = Value
LS_CalcMargins
End Property

Private Sub LS_CalcMargins()
Dim xMargin#, yMargin#, tpi#, H%, ScaleFactor#

mLeftMargin = 0
mTopMargin = 0

If Not TypeOf mObj Is Printer Then
mObj.ScaleMode = mScaleMode
Exit Sub
End If


Printer.ScaleMode = vbTwips
ScaleFactor# = Printer.ScaleWidth
Printer.ScaleMode = mScaleMode
ScaleFactor# = ScaleFactor# / Printer.ScaleWidth

With Printer
xMargin = GetDeviceCaps(.hdc, 112)
xMargin = (xMargin * .TwipsPerPixelX) / ScaleFactor#
yMargin = GetDeviceCaps(.hdc, 113)
yMargin = (yMargin * .TwipsPerPixelY) / ScaleFactor#
mLeftMargin = xMargin
mTopMargin = yMargin
End With

End Sub

Public Property Get Device() As Object
Set Device = mObj
End Property

Public Property Let CurrentX(Value As Double)
mObj.CurrentX = Value - mLeftMargin
End Property

Public Property Get CurrentX() As Double
CurrentX = mObj.CurrentX + mLeftMargin
End Property

Public Property Let CurrentY(Value As Double)
mObj.CurrentY = Value - mTopMargin
End Property

Public Property Get CurrentY() As Double
CurrentY = mObj.CurrentY + mTopMargin
End Property

Public Property Let ScaleMode(Value As Integer)
mScaleMode = Value
LS_CalcMargins
End Property

Public Property Get ScaleMode() As Integer
ScaleMode = mScaleMode
End Property

Public Property Set Font(Value As StdFont)
Set mObj.Font = Value
End Property

Public Property Get Font() As StdFont
Set Font = mObj.Font
End Property


Public Property Get TextWidth(S$) As Double
TextWidth = mObj.TextWidth(S$) ' this is in current scalemode
End Property

Public Property Get TextHeight(S$) As Double
TextHeight = mObj.TextHeight(S$) ' this is in current scalemode
End Property


Public Property Get Height() As Double
Height = mObj.ScaleHeight
End Property

Public Property Let ForeColor(Value As OLE_COLOR)
If TypeOf mObj Is Printer Then Exit Property
mObj.ForeColor = Value
End Property

Public Property Let BackColor(Value As OLE_COLOR)
If TypeOf mObj Is Printer Then Exit Property
mObj.BackColor = Value
End Property


Public Sub Cls()
If TypeOf mObj Is Printer Then
Exit Sub
End If
mObj.Cls
End Sub

' #################################################################
'
' Print a Line without moving the Print Head
Public Sub LineX(X#, W#)
Dim O As Object, V#, H#

Set O = mObj
O.Print "";
Call LS_Position("S")
H = X - mLeftMargin
V = Me.CurrentY - mTopMargin
O.FillStyle = vbFSSolid
O.Line (H, V)-(H + W, V + 0.25), O.ForeColor, BF
Call LS_Position("R")
End Sub

' #################################################################
'
'
Public Sub NewPage()
Dim O As Object, V#
If TypeOf mObj Is Printer Then
mObj.NewPage
Exit Sub
End If
' --- For a Picture Box
Set O = mObj
V = O.CurrentY + TextHeight("") / 2

O.Line (O.CurrentX, V)-(O.ScaleWidth, V)
Set O = Nothing
Call NewLine

End Sub

' #################################################################
'
'
Public Sub EndDoc()
If TypeOf mObj Is Printer Then
mObj.EndDoc
Exit Sub
End If
End Sub

' #################################################################
'
'
Public Sub ClearLine()
Dim H!, Q&
If TypeOf mObj Is Printer Then Exit Sub
H = Me.CurrentX
' --- Print Width * 1.5 spaces - fudge for Bold
Q = mObj.ScaleWidth / mObj.TextWidth(" ") * 1.5
Me.Print String$(Q, " ");
Me.CurrentX = H
End Sub

' #################################################################
'
' Note: We print *above* the CurrentY
' Necessary for aligning different Font Sizes
' Note: The Print Head moves right but NOT down - unless vbCr in
string
'
Public Sub Output(ByVal Text$, _
Optional ByVal V As Variant, _
Optional ByVal H As Variant)
Dim HoldV#, HoldH#, Q%

HoldV = Me.CurrentY
mObj.Print "";

If IsMissing(V) Then V = Me.CurrentY
If IsMissing(H) Then H = Me.CurrentX
Me.CurrentY = V
Me.CurrentX = H

' --- adjust vertical position for height of other font
' descender is 0.25 - below CurrentY - get Base of Char at
CurrentY
Me.CurrentY = Me.CurrentY - (Me.TextHeight("M") * 0.75)
Do
Q = InStr(Text$, vbCr)
If Q Then
HoldH = Me.CurrentX
mObj.Print Left$(Text$, Q - 1) ' Allow a line drop
HoldV = HoldV + Me.TextHeight("M") ' Remember the Line Drop
Me.CurrentX = HoldH ' Restore Horiz position
Text = Mid$(Text, Q + 1)
End If
Loop Until Q = 0

mObj.Print Text$;
Me.CurrentY = HoldV

End Sub


' #################################################################
'
' Center Some Text
'
Public Sub Center(Text$, HPos#, Width#)
Dim L#

L = (Width - Me.TextWidth(Text$)) / 2
Me.CurrentX = HPos + L
Me.Output Text$
End Sub

' #################################################################
'
'
'
Public Sub RightJust(Text$, _
Optional V As Variant, _
Optional H As Variant)
If IsMissing(V) Then V = Me.CurrentY
If IsMissing(H) Then H = Me.CurrentX

Me.Output Text, V, (H - Me.TextWidth(Text$))

End Sub

' #################################################################
'
' Return a wrapped string - vbCr indicates Wrap positions
'
Function Wrap$(ByVal Text$, W#)
Dim L$, P%, Result$, S$, i

S$ = "x"
i = 1
While Len(S$)
S$ = Mid$(Text$, i, 1)
If S$ = " " Then P = i ' remember last P
If TextWidth(L$ + S$) > W Then ' it would be too wide
If P = 0 Then P = i ' can't wrap
Result$ = Result$ + Trim$(Left$(L$, P)) + vbCr
Text$ = Trim$(Mid$(Text$, P + 1))
P = 0
L$ = ""
i = 1
Else
L$ = L$ + S$
i = i + 1
End If
Wend

Wrap = Result$ + L$

End Function

' #################################################################
'
'
Private Sub LS_FixDashField(Value$, L&)
If Left$(Value$, 1) = "-" Then
If Value$ = String$(Len(Value$), "-") Then
While mObj.TextWidth(Value$) < L
Value$ = Value$ + "-"
Wend
End If
End If
End Sub

' #################################################################
'
'
Public Sub NewLine()
Dim Q#

' --- Trap for running over page end
If TypeOf mObj Is Printer Then
Q = mObj.ScaleHeight
If (mObj.CurrentY + mObj.TextHeight("")) > Q Then
mObj.NewPage
End If
End If
mObj.Print
End Sub

' #################################################################
'
'
Private Sub LS_Position(Act$)
Static X#, Y#

Select Case Act$
Case "S": X = Me.CurrentX: Y = Me.CurrentY
Case "R": Me.CurrentX = X: Me.CurrentY = Y
Case Else: MsgBox "Bad Act$ - LS_Position"
End Select
End Sub


============= Contents of FORM1.FRM ==================

VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3930
ClientLeft = 60
ClientTop = 345
ClientWidth = 4740
LinkTopic = "Form1"
ScaleHeight = 3930
ScaleWidth = 4740
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Left = 1620
TabIndex = 1
Top = 3240
Width = 1155
End
Begin VB.PictureBox Picture1
Height = 2955
Left = 240
ScaleHeight = 2895
ScaleWidth = 4155
TabIndex = 0
Top = 180
Width = 4215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

' 1 Add a Picturebox
' 2 Add a Command Button

Dim PO As New TGPrintObject

Private Sub Command1_Click()
Dim S$

Set PO.Device = Picture1
'Set PO.Device = Printer

PO.ScaleMode = vbMillimeters
' ---
PO.Font.Bold = False
PO.Font.Name = "Courier"
PO.Output "This Is a Test", 10, 10
' ---
PO.Font.Bold = True
PO.Font.Size = 18
PO.Output "And More", 10, 45
' --- Print a horizontal Line
PO.CurrentY = 15
PO.LineX 10, 30
' --- Demonstrate Line Feed
S = "This is On" + vbCr + "two Lines"
PO.Output S, 20, 10
' --- Print Wrapped Text
PO.Font.Size = 8
S = "Here is a bit of wrapped Text"
S = PO.Wrap(S, 30)
PO.Output S, 30, 10
' --- Print Centred Text
PO.CurrentY = 42
PO.Center "Centered", 10, 30
' Set Vertical Position
PO.CurrentY = 47
' --- Print Right Justified
PO.RightJust "100.00", , 40
PO.Output " <-- Right Justified"
End Sub

Leo Swart

unread,
Mar 22, 2003, 6:49:08 PM3/22/03
to
J French wrote
"J French" <Bounce_...@iss.u-net.com_.bin> schrieb im Newsbeitrag
news:3e70541d...@news.u-net.com...

> On 12 Mar 2003 10:46:30 -0800, MadAndP...@msn.com (Samir Talwar)
> wrote:
>

very long snip of coding lines . . . .

Whow, i can not interpret these lines (because i am not an programmer :-(

what happend if i had no standard-printers available . . ..

By , Leo


0 new messages