Modifying an attribute without losing its richtext formatting

Hi, I'd like to do the following. First I create a new object. After that I want to copy the content of an attribute of an already existing object into the new object. Then I want to replace some words in the new object. If the attribute contains for example bold text, I run into problems...

I already tried different ways of implementing this, but either the richtext information is completely lost, or the new object displays richtext tags, or the words can't be replaced.

Can someone help me with this? Does somebody know a way to modify an attribute without losing its richtext formatting?
SystemAdmin - Wed Apr 01 08:08:31 EDT 2009

Re: Modifying an attribute without losing its richtext formatting
dpechacek - Wed Apr 01 11:00:25 EDT 2009

You'd need to get the rich text as a string. Then loop through each chunk of rich text.

As you get each chunk, you'd have to get the text, find what markup it has, and append it in a new string. When you find the text that has the stuff you want to change, change it.
Something like:
 

RichText rt
 
for rt in richTextString do {
   chunktext = rt.text
   
   if(chunktext contains the text you want to change)
       // change it
   }
 
   // need an "if" statement for each type of rtf formatting
   if(the rich text is formatted this way) {
      newRTFString = reapply the rtf formatting to chunktext
   }
}
 
print newRTFString


AAI Services, Textron
dpechacek@sc-aaicorp.com
David.Pechacek@gmail.com

Re: Modifying an attribute without losing its richtext formatting
llandale - Wed Apr 01 13:38:51 EDT 2009

dpechacek - Wed Apr 01 11:00:25 EDT 2009

You'd need to get the rich text as a string. Then loop through each chunk of rich text.

As you get each chunk, you'd have to get the text, find what markup it has, and append it in a new string. When you find the text that has the stuff you want to change, change it.
Something like:
 

RichText rt
 
for rt in richTextString do {
   chunktext = rt.text
   
   if(chunktext contains the text you want to change)
       // change it
   }
 
   // need an "if" statement for each type of rtf formatting
   if(the rich text is formatted this way) {
      newRTFString = reapply the rtf formatting to chunktext
   }
}
 
print newRTFString


AAI Services, Textron
dpechacek@sc-aaicorp.com
David.Pechacek@gmail.com

Yup, as far as I know you cannot insert raw text into a rich-text chunk, preserving the markup. You need to plow through all the RT chunks, preserving the existing format, reapply that formatting to new strings, and rebuild the string. Notice this will NOT allow you to preserve non-DOORS supported markup, such as double underscores, that you can import (as far as I know).

I'd be tempted to bypass ..err.. to think about bypassing the 'for rt in string' loop, and just write a massive parser looking for actual rich text markup like "{" and "/b " and attempt to do the work that way, but it will be tough since the raw text portions may be split up by rich text mark up: substituting "will" with "shall" may result in finding "will" spread out across dozens of actual rich text bytes.

Looks hopeless to me.

>Louie

Re: Modifying an attribute without losing its richtext formatting
llandale - Wed Apr 01 13:41:04 EDT 2009

llandale - Wed Apr 01 13:38:51 EDT 2009
Yup, as far as I know you cannot insert raw text into a rich-text chunk, preserving the markup. You need to plow through all the RT chunks, preserving the existing format, reapply that formatting to new strings, and rebuild the string. Notice this will NOT allow you to preserve non-DOORS supported markup, such as double underscores, that you can import (as far as I know).

I'd be tempted to bypass ..err.. to think about bypassing the 'for rt in string' loop, and just write a massive parser looking for actual rich text markup like "{" and "/b " and attempt to do the work that way, but it will be tough since the raw text portions may be split up by rich text mark up: substituting "will" with "shall" may result in finding "will" spread out across dozens of actual rich text bytes.

Looks hopeless to me.

>Louie

Mmmmm .... maybe copy to Word, command word to 'replace', then copy it back to the object...

Re: Modifying an attribute without losing its richtext formatting
Tony_Goodman - Thu Apr 02 03:05:56 EDT 2009

llandale - Wed Apr 01 13:41:04 EDT 2009
Mmmmm .... maybe copy to Word, command word to 'replace', then copy it back to the object...

David and Louie are, as always correct. This is not trivial. Editing rich text will be a nightmare and will require a pretty large and clever script.

I am not a fan of rich text markup, so I would suggest you consider why you need to use it.
It looks nice in the display, but does it really add any value to your requirements?
I suspect not.

Re: Modifying an attribute without losing its richtext formatting
SystemAdmin - Thu Apr 02 03:27:55 EDT 2009

Tony_Goodman - Thu Apr 02 03:05:56 EDT 2009
David and Louie are, as always correct. This is not trivial. Editing rich text will be a nightmare and will require a pretty large and clever script.

I am not a fan of rich text markup, so I would suggest you consider why you need to use it.
It looks nice in the display, but does it really add any value to your requirements?
I suspect not.

I just found out, that there's a function richText(of string), which pretty much solved my problem. My implementation now looks like this:
 

attr_name = get(attrListDBE, i)         
attr_value = richTextNoOle(obj.attr_name)               
                
if (isRichText(attr_value))
{             
        while (findRichText(attr_value, PRODUCT_TAG, offset, len, true))
        {                                             
                attr_value = attr_value[0:offset-1] selected_product attr_value[offset+len:length(attr_value)-1]
        }
                        
        new_obj.attr_name = richText(attr_value)        
}

 


As I can be sure the word I have to replace doesn't contain markup, I think, I don't need to go through the string chunk by chunk, as you proposed. The only problem is, that OLE objects won't be copied, but it's not that big problem. If I use richTextWithOle() the program doesn't terminate. If someone knows an easy way to fix this, don't hesitate to tell me...

And thank you guys. Yesterday I was trying for hours and today while I was trying to implement your solution, I accidently stumbled across that function instantly.