Welcome to Foxite.COM Community Weblog Sign in | Join | Help



Fully Justify an RTF



A question has been raised before whether we can fully justify in an RTF control but I am currently busy with enhancing my classes plus creating new ones I can think of to add beauty to a form’s appearance that I simply tuck this question away at the back of my mind for later retrieval and playing.

So after the 8 classes is posted, I opened up my RTF control here and much to my dismay, I believe it really is impossible to do that as even copy-pasting a fully justified document from word into the RTF control automatically converts fully-justified paragraphs into a left justified one.  Even importing from a doc or docx (please see my previous posts on how this can be done) have the same result.  So I gave up finding out how in an RTF control.

 

Nevertheless, if we cannot do that in an RTF control on a form, at least we can do that when we export the RTF control’s contents (TextRTF property) into a doc file or simply saves it as an outside RTF from a memo field.  In that way, our document is presented better.

 

The trick to make it proper is to only apply full justifications on left-justified paragraphs.  We should not touch Right and Center justified paragraphs, I will show you later how to do that.  What this trick will do is save content of an RTF control into a physical file, turns left justified into a right justified one, then utilizes ShellExecute() to open the file via its default program like MSWord. 

 

We will presume that you have an RTF control named OleRTF in a form (if you don’t have one yet, you can download any of the previous samples I gave last time regarding RTF), then add a command button and in its click event paste these:


#Define wdAlignParagraphLeft     0
#Define wdAlignParagraphJustify  3
#Define wdParagraph              4
#Define wdLine                   5

Local loWord As Word.Application, lnloop As Integer, lcTempRTF as Character
loWord = Createobject("word.application")

* Crate a filename reference in the local user’s temp folder, to be used for saving.  Saving can be done via STRTOFILE() but an RTF ActiveX control has a built-in method for that so we will use that instead

lcTempRTF = Addbs(Getenv("TMP"))+Sys(3)+".rtf"
Thisform.oleRTF.SaveFile(lcTempRTF)

* Open it up via automation
loWord.Documents.Open(lcTempRTF)

* Now we will check from top to bottom its justification settings and replace only the left justified ones
For lnloop = 1 To loWord.ActiveDocument.Paragraphs.Count
    If loWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
        loWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
    Endif
    loWord.Selection.MoveDown(wdParagraph,1)
Endfor

* Save the changes made, close it, and quit Word
loWord.ActiveDocument.SaveAs(lcTempRTF,6)
loWord.ActiveDocument.Close
loWord.Quit

* Open it in its default program to see outright the result
DECLARE INTEGER ShellExecute IN shell32.dll ;
    INTEGER hndWin, ;
    STRING cAction, ;
    STRING cFileName, ;
    STRING cParams, ;
    STRING cDir, ;
    INTEGER nShowWin

ShellExecute(0,"open",lcTempRTF,"","",1)


And there you have it, a very simple trick yet an effective one.  To test this properly, you can change justifications of some paragraphs into center or right in an RTF control before performing the above trick.  You will see that those settings will be retained in the final output.

The only problem I can see with the above is it will be applied to the entire document, i.e., all left justified paragraphs will be fully justified.

And here are the other two constants that we don’t need in the above trick but maybe will be useful on your playings as well:


#Define wdAlignParagraphCenter    1
#Define wdAlignParagraphRight     2


I hope this simple trick will be useful to you. 

In fully justifying of the contents of a memo field containing an RTF format in the Native VFP Report, I will take the liberty to refer you to Mike Gagnon’s wonderful article: http://www.atoutfox.org/articles.asp?ACTION=FCONSULTER&ID=0000000131


Enjoy!



Published Saturday, January 23, 2010 11:11 PM by sandstorm36
Filed Under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Thanks in accomplish destroy contributing good enough contentedness to the Blogosphere

Wednesday, February 10, 2010 5:55 PM by UGG Boots
I found this article useful in a paper I am writing at university.  Hopefully, I get an A+ now!

Thanks

Bernice Franklin

[url=http://www.uggworld.eu]UGG Boots[/url]

What do you think?

(required) 
required 
(required)