Link to home
Start Free TrialLog in
Avatar of RickHolt
RickHolt

asked on

How do I prevent Growing Line Spacing in RichTextBox Control

I have two RichTextbox controls on a MS-Access form.When I moved data from one control to another, extra \par markers are inserted.  This action is not consistent among several machines, even when the versions of RichTx32.ock, Riched20.dll and Riched32.dll are in the same versions.
Specifically, in this simple statemnt is where the trouble occurs:
Public Sub SetBody(TextRTF As String, ImageString As String)
       
    R.TextRTF = TextRTF                                    '(RP) set the RTF..

TextRtf a string with the following contents

{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}{\f1\fnil\fcharset0 MS Sans Serif;}}
{\colortbl ;\red0\green0\blue255;}
{\*\generator Riched20 5.50.99.2050;}\viewkind4\uc1\pard\qc\cf1\f0\fs48\lang1033 VIP Medicine, LLC
\par
\par
\par
\par \fs24 12755 58th Street Mayer, MN 55360 800-652-3739\cf0\f1\fs17
\par
\par
\par
\par \pard
\par
\par
\par
\par
\par Yours Truly,entMD]
\par
\par
\par
\par }

Notice that there are \par tokens between the first two printable lines.  The target control, R is a Richtx32.ocx ActiveX control.  Upon execution of this single statement, the contents of R.TextRTF are now 5 \par tokens between the first two printable lines.  In addition, another extra \par token has been added at the end.

{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}{\f1\fnil\fcharset0 MS Sans Serif;}}
{\colortbl ;\red0\green0\blue255;}
{\*\generator Riched20 5.50.99.2050;}\viewkind4\uc1\pard\qc\cf1\f0\fs48\lang1033 VIP Medicine, LLC
\par
\par
\par
\par
\par \fs24 12755 58th Street Mayer, MN 55360 800-652-3739\cf0\f1\fs17
\par
\par
\par
\par
\par \pard
\par
\par
\par
\par
\par Yours Truly,entMD]
\par
\par
\par
\par
\par }

What is going on here and how can I prevent it.  Note also that upon research, the data streams are slightly different on the working machines, but only in the \Generator token.  On a machine where extra tokens are bein inserted, it is:

{\*\generator Riched20 5.50.99.2050;}

On a machine where there are no extra tokens being inserted, it is:

{\*\generator Riched20 5.50.30.2002;}\

If this is a version-related problem, how do I version level.  I have tried changing versions of Richtx32.ocx, Riched20.dll and riched32.dll, all to no avail.  The problem occurrs on Vista, Win/Xp and Win2k machines.  ON onther machine with the same OS, it does not occur.

one other thing I noticed that might be helpful.  In environments where the extra token is inserted, a CRLF(Asc(13) & Asc(10) moved into the text component results in two \par tokens being generated in the rtf.  In the non-failing environments, that sequence result in only 1 \par being generate.

Avatar of rheitzman
rheitzman
Flag of United States of America image

RichTextBoxes as a constant hassle when used with Access. If you can avoid them I'd suggest you do.

One trick to make things easier is to create an object that will work with the VBE Intellisense:

    Dim rtb As RichTextBox
    Set rtb = Me.R

Doing this will expose some to the hidden methods and properties of the control and make you life easier.

For example you can extract the text only (with line ends) from a Rich box:

   sText = rtb.Text

You can then assign the sText to another RichTextBox. You will loose formating of fonts but the text and line ends survives.

Sloshing the RTF between boxes using an external file sometimes works.

Don't expect RichTextBoxes to work on a Tab control. If you have to do that bind the control at the last moment (when the tab is made active.)

Expect weird problems.... Like a while back an automatic update changed something about the way line ends were encoded into RTF....

Hopefully there is something in the post that helps....


You might try stripping out an CrLf pairs that may be in the RTF string

R.TextRTF = Replace(TextRTF, vbCrLf)

You might need to filter for just a single vbCr or vbLf.
Avatar of RickHolt
RickHolt

ASKER

Unfortunately, the application would need to be rewritten to remove reliance on rich Text box controls.  I will try the method you mention just in case it exposes some kind of method or parameter that I have not seen in my extensive web searches and will respond if I find anything fruitful.
Avatar of Dana Seaman
This is what I use for API created RichEdit to prevent extra vbCrLF(/par).
Please try this an let me know if it fixes the issue.

Private Const WM_USER = &H400
Private Const EM_SETEDITSTYLE = WM_USER + 204
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
 
SendMessage RichTextBox1.hWnd, EM_SETEDITSTYLE, 1&, 1&

Open in new window

Update

Private Const WM_USER = &H400
Private Const EM_SETEDITSTYLE = WM_USER + 204
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
 
SendMessageLong RichTextBox1.hWnd, EM_SETEDITSTYLE, 1&, 1&

Open in new window

I tried to take this advice, but it did not seem to operate without the flaws cited.  That
is, the SendMessage appears to have no effect.  Below is the code sequence and the result.  
Keep in mind that R and R2 are RichTextBox ActiveX controls on an MS-Access form called
Correspondence_Editor.

First, the setup for the SendMessage API Call.  This is in the Declarations section of the form.

Private Const WM_USER = &H400
Private Const EM_SETEDITSTYLE = WM_USER + 204
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long


I am executing the exact same codein the Correspondence_editor form shown in the initial report with the exeception of the SendMessageLong, so we have the following:

    SendMessageLong Me!R.hWnd, EM_SETEDITSTYLE, 1&, 1&
    R.TextRTF = TextRTF                                    '(RP) set the RTF..

Before the executionof the SendMessage, the contents of TextRtf are as follows:

{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}{\f1\fnil\fcharset0 MS Sans Serif;}}
{\colortbl ;\red0\green0\blue255;}
{\*\generator Riched20 5.50.99.2050;}\viewkind4\uc1\pard\qc\cf1\f0\fs48\lang1033 VIP Medicine, LLC
\par
\par
\par
\par
\par \fs24 12755 58th Street Mayer, MN 55360 800-652-3739\cf0\f1\fs17
\par
\par
\par
\par \pard
\par
\par
\par
\par
\par
\par
\par
\par }

The return code of the SendMessageLong is 0

After execution of the R.TextRTF = TextRTF, the contents of R.TextRTF is:

{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}{\f1\fnil\fcharset0 MS Sans Serif;}}
{\colortbl ;\red0\green0\blue255;}
{\*\generator Riched20 5.50.99.2050;}\viewkind4\uc1\pard\qc\cf1\f0\fs48\lang1033 VIP Medicine, LLC
\par
\par
\par
\par
\par
\par \fs24 12755 58th Street Mayer, MN 55360 800-652-3739\cf0\f1\fs17
\par
\par
\par
\par
\par \pard
\par
\par
\par
\par
\par
\par
\par
\par
\par }

Despite the SendMessage, The same additional \par was added after the first text line, after the second text line and after the \pard.  Therefore, I conclude that the suggested solution does not work.  Perhaps I missed something?
[btw, I tried to respond individually to suggested soultions, but clicking onthe No box is causing Errors on the we page.]
ASKER CERTIFIED SOLUTION
Avatar of RickHolt
RickHolt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial