How to format text from field to RTF document

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
planix
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 47
Joined: Tue Mar 10, 2009 12:47 pm

How to format text from field to RTF document

Post by planix » Tue Mar 10, 2009 1:02 pm

Hi,

I am trying to set up some simple reports by using the merge function described in runrev.

I have no problem getting the information from fields to the right places on the template report. But, if the field is multi-line the merged result is a single line of text with no spaces and no hard returns.

For example, one field contains 3 lines of text;
Mr H. Example
21 Somewhere
888888888

I have then used

Code: Select all

 put field "fldAddress" into mrgAddress
and get the result

MrH.Example21Somewhere888888888

I have tried

Code: Select all

get the formattedtext of field "fldAddress"
put it into mrgAddress
and I get exactly the same result.

Does anyone have some thoughts on what I am doing wrong here? Is it a property setting? Am I using the wrong type of field? Is there some trick to making text from a field wrap?

Thanks.
****************************************
PLANIX
"Knowing anything is better than knowing nothing. But knowing that nothing ain't anything, why that's just plain nirvana."

Duncan
Posts: 13
Joined: Wed Feb 25, 2009 4:25 pm
Contact:

Post by Duncan » Tue Mar 10, 2009 2:12 pm

I am new here so do not believe anything I say :lol: but....

I think lineDelimiter is what you are looking for.

set the lineDelimiter to numToChar(13)
put field "fldAddress" into mrgAddress

will pick up a Return character at the end of a line and separate your output.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Tue Mar 10, 2009 2:53 pm

Dear planix,

In the title of the subject, you mention RTF and document, but in your question, there is no mention of that. Therefore, it is not entirely clear to me what you are trying to do. I think that large relevant parts of your scripts are missing in your question.

That said, I believe that the rtfText property might help you. Just as a start, try saving the rtfText of a field to a binary file and see if you can open that in a text editor.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

planix
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 47
Joined: Tue Mar 10, 2009 12:47 pm

Post by planix » Tue Mar 10, 2009 9:42 pm

Hi,

Thanks for these responses. I am not sure how to make the problem clearer really as there is no real issue with the code that I am using to get text from fields into a word document using the merge function.

The problem is that when I merge multi-line text from a field the return character does not seem to carry over.

This is not a code problem, I don't think, but some problem with the setting for the field. I just can't figure out what.

Sorry to say that setting the linedelimiter property didn't work and neither does putting the rtftext.

Cheers
****************************************
PLANIX
"Knowing anything is better than knowing nothing. But knowing that nothing ain't anything, why that's just plain nirvana."

planix
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 47
Joined: Tue Mar 10, 2009 12:47 pm

Post by planix » Wed Mar 11, 2009 1:21 am

Hi,

I have tried a few experiments with this.

Here is some simplified code for what I am trying.

I have a template RTF file which has placeholder text like [[mrgAddress]].

I have code which reads something like this.

Code: Select all

on mouseUp
  local theTemplateFile, theMergedFile, theRTFtext
  -- set up the files for template and merged file
  answer file "Where is 'EpOfCare.rtf' template file?"
  if it is empty then exit mouseUp
  put it into theTemplateFile
  ask file "Save your message as:"
  if it is empty then exit mouseUp
  put it into theMergedFile
  -- put the card fields into the variables for merging
local mrgAddress

put field "fldAddress" into mrgAddress

 -- merge and save the new file
  put URL("binfile:" & theTemplateFile) into theRTFtext
  set the fileType to "MSWDRTF"
  put merge(theRTFtext) into URL("binfile:" & theMergedFile)
end mouseup
What this code does is to identify the template file and a name for the output file. It then takes the text from a multi-line address field and puts it into a variable (mrgAddress) which has the same name as the placeholder in the template file. The merge function then replaces the placeholder text with the text in the variable of the same name.

The problem is that there are no carriage returns in the text that is merged. So the text is merged as one line not multiple lines.

I have tried putting text with carriage returns into the mrgAddress variable like this;

Code: Select all

put "test" & numToChar(13) & "test" into mrgAddress
and the merged text is "testtest"- so no carriage return. I am wondering if the merge function strips out or ignores the carriage return function so it has no effect. I tried this with space;

Code: Select all

put "test" & numToChar(32) & "test" into mrgAddress
and got a space. But when I tried line feed/new line

Code: Select all

put "test" & numToChar(10) & "test" into mrgAddress
I got "testtest".

Any ideas?

thanks
****************************************
PLANIX
"Knowing anything is better than knowing nothing. But knowing that nothing ain't anything, why that's just plain nirvana."

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Wed Mar 11, 2009 2:53 am

What platform are you on?

I just tried this on my mac:

Code: Select all

on mouseUp
   put "mark" & cr & "smith" into tName
   put "my name is [[tName]], hello" into tText
   put merge(tText)
end mouseUp
and got

my name is mark
smith, hello

If you're on windows, perhaps you should try crlf.

best,

Mark

planix
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 47
Joined: Tue Mar 10, 2009 12:47 pm

Post by planix » Wed Mar 11, 2009 3:51 am

Hi Mark,

I am on Windows.

I just put your code into my button and got the same result as you in the message box. I then tried that with the merge into my template document and got the output without any carriage returns.

I then tried putting my field contents into the message box using the merge function and output had the carriage returns.

So, it looks like the merge works until it comes to trying to merge the variables with the placeholders in the template file. At that point the carriage returns disappear.

I suspect there is something that I am not understanding about trying to merge with an external RTF file.

Thanks for your help but this seems like a much more complicated issue than I had thought.

Cheers

[/code]
****************************************
PLANIX
"Knowing anything is better than knowing nothing. But knowing that nothing ain't anything, why that's just plain nirvana."

planix
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 47
Joined: Tue Mar 10, 2009 12:47 pm

Post by planix » Wed Mar 11, 2009 4:12 am

Hi,

It seems that this does have something to do with the template file being an RTF file.

I set up the template file as a plain text file and the field is merged with carriage returns in place.

A text file is no good for my final product but at least this narrows it down to being some issue with multi line fields merging into an RTF document.

Mind you, I have no idea where to go with this from here.
****************************************
PLANIX
"Knowing anything is better than knowing nothing. But knowing that nothing ain't anything, why that's just plain nirvana."

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Wed Mar 11, 2009 7:01 am

Try this:

Code: Select all

local mrgAddress
put field "fldAddress" into mrgAddress 
replace return with (return & "\par ") in mrgAddress
The "\par " bit forces the start of a new paragraph.

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

planix
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 47
Joined: Tue Mar 10, 2009 12:47 pm

Post by planix » Wed Mar 11, 2009 8:51 am

Hi Jan,

Yay! It works. Thanks very much for that.

I wish I knew how to search stuff like that out.

Thanks to you and this forum the response and goodwill has been excellent.

All the best.
****************************************
PLANIX
"Knowing anything is better than knowing nothing. But knowing that nothing ain't anything, why that's just plain nirvana."

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Mar 12, 2009 7:01 am

Actually, for styled text formatting, the best trick to learning the format is to start small and put the htmlText or rtfText property of a field into another field, and study the contents.
Just drop a second field onto your card, open the message box, type the following

Code: Select all

set the text of the last field to the rtfText of field "Foobar"
hit return, and you can now see what rtf codes are necessary to accomplish the formatting you had setup in field "Foobar".
But you can also use this in the other direction, tweaking the rtc codes before you change the original field; again use the message box to type

Code: Select all

set the rtfText of field "Foobar" to the text of the last field
hit return and study the effects.
Don't expect support for the full array of Word functions though :-)

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply