Page 1 of 1

[Solved] [Writer] Editing text destroys formatting?

PostPosted: Tue Jan 05, 2021 4:27 pm
by nunof32

My problem : I want to replace a portion of text with a different text while keeping the formatting of the original text. I have explored several alternatives and found only one way of doing it which is to use a replaceDescriptor. However, I cannot use that technique because it replaces ALL occurrences of the sought text – I want to replace only one or a few occurrences – and it parses the whole document – I want to restrict the search and the changes to a certain region of the document.

(The other techniques I have explored allow me to restrict the scope of the changes (number and / or range), albeit sometimes at the cost of some acrobatics …)

Thanks for any suggestion that you may have.

The rest of this post is a summary of the investigations that I have done and their results. Thanks for the patience you may have to read through it.

The use of the Replace function to edit text usually destroys the formatting that the user may have originally applied to that text. This is well documented in the BASIC Programming Guide (see section on Paragraph Portions).

To prevent destroying the formatting the BASIC Programming Guide recommends that for a paragraph that needs editing an enumeration of its ‘portions’ is built and that each ‘portion’ is edited individually (with the help of the Replace function).

The Guide provides an example macro for doing that. The comment of the macro states that “Since […] the paragraph portions are edited directly, their formatting information is retained […].

Not so, unfortunately.

I have created a simple 1-paragraph document (uploaded). The paragraph is default-formatted except for a number of passages to which I have manually applied special formatting : red, italics, bold. Then I copied and pasted on my installation the macro provided by the Guide. When I run that macro the formatting of the entire paragraph is destroyed : everything is in red, everything following the chunk in italics is in italics and everything following the chunk in bold is in bold.

[The enclosed document contains a macro that, at the time I have created this post, is an exact replica of the example macro provided by the Guide (you should double-check that). If you run the macro you can see by yourself the described effects : formatting destroyed]

So what other solutions are available if I want to edit text and retain the original formatting, be it direct or indirect ?

In the macro associated with my example document,
- Section A. is, as already stated, an exact replica of the macro provided by the Guide. It does not fit the bill for me
- Section B. illustrates an alternative method : B1 uses a Search Descriptor and edits the content of the Found range – formatting is not preserved; B2 uses a Replacement Descriptor and the formatting is preserved
- Section C. uses yet another technique : a cursor to select text and edit that text – formatting is not preserved.

[To check the effect of the different techniques comment-out everything between Sub and End Sub and un-comment only the section that you want to check before running the macro]


Re: WRITER Editing text destroys formatting ?

PostPosted: Tue Jan 05, 2021 5:02 pm
by JeJe
All the above is giving me a headache. Have you got
a before
the text you want inserted
an after of how you want it to look


Re: WRITER Editing text destroys formatting ?

PostPosted: Tue Jan 05, 2021 5:08 pm
by RoryOF
A quick and dirty way is to Paste a screenshot (image) of the original text.

Re: WRITER Editing text destroys formatting ?

PostPosted: Tue Jan 05, 2021 5:29 pm
by nunof32
Uploaded images of the text before and the text after.
"chunk" has been replaced with "word", formatting preserved.
In the real problem I have, "chunk" would have to be replaced with "word" sometimes, some other times with something else, yet some other times not replaced at all, depending on context. That is why I need to write a macro, otherwise I would do it with the standard Find & Replace of the GUI.

Re: WRITER Editing text destroys formatting ?

PostPosted: Tue Jan 05, 2021 6:00 pm
by RoryOF
In all three cases you have shown, your problem is retrieving the attributes of the found text, and applying these to the replacement text. You may find section 7.14 et seq of Andrew Pitonyak's "Useful Macro Information for" helpful.
English Macro Document

Re: WRITER Editing text destroys formatting ?

PostPosted: Tue Jan 05, 2021 6:11 pm
by nunof32
Many thanks RoryOF.