[Solved] [Writer] Editing text destroys formatting?

Keyboard macros or custom scripts

[Solved] [Writer] Editing text destroys formatting?

Postby nunof32 » Tue Jan 05, 2021 4:27 pm

Hello,

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 https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Structure_of_Text_Documents (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]

Thanks.
Attachments
testPreserveFormatting.odt
(12.99 KiB) Downloaded 72 times
Last edited by nunof32 on Tue Jan 05, 2021 6:12 pm, edited 1 time in total.
OpenOffice 4.1.10 with MacOS X 10.13.6
nunof32
 
Posts: 59
Joined: Sat Jul 28, 2012 9:52 am

Re: WRITER Editing text destroys formatting ?

Postby JeJe » Tue Jan 05, 2021 5:02 pm

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

?
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1567
Joined: Wed Mar 09, 2016 2:40 pm

Re: WRITER Editing text destroys formatting ?

Postby RoryOF » Tue Jan 05, 2021 5:08 pm

A quick and dirty way is to Paste a screenshot (image) of the original text.
Apache OpenOffice 4.1.10 on Xubuntu 20.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32694
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: WRITER Editing text destroys formatting ?

Postby nunof32 » Tue Jan 05, 2021 5:29 pm

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.
Attachments
After.png
Before.png
OpenOffice 4.1.10 with MacOS X 10.13.6
nunof32
 
Posts: 59
Joined: Sat Jul 28, 2012 9:52 am

Re: WRITER Editing text destroys formatting ?

Postby RoryOF » Tue Jan 05, 2021 6:00 pm

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 OpenOffice.org" helpful.
English Macro Document
Apache OpenOffice 4.1.10 on Xubuntu 20.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32694
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: WRITER Editing text destroys formatting ?

Postby nunof32 » Tue Jan 05, 2021 6:11 pm

Many thanks RoryOF.
OpenOffice 4.1.10 with MacOS X 10.13.6
nunof32
 
Posts: 59
Joined: Sat Jul 28, 2012 9:52 am


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 5 guests