21.08.2013 Views

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

described in Search and Replace.<br />

The Structure of Text Documents<br />

Note – VBA : The content of the procedure described here for accessing the paragraphs of a text is<br />

comparable with the Paragraphs listing used in VBA, which is provided in the Range and Document objects<br />

available there. Whereas in VBA the paragraphs are accessed by their number (for example, by the<br />

Paragraph(1) call), in <strong>OpenOffice</strong>.<strong>org</strong> Basic, the Enumeration object described previously should be used.<br />

There is no direct counterpart in <strong>OpenOffice</strong>.<strong>org</strong> Basic for the Characters, Sentences and Words lists<br />

provided in VBA. You do, however, have the option of switching to a TextCursor which allows for navigation<br />

at the level of characters, sentences and words.<br />

Paragraph Portions<br />

The previous example may change the text as requested, but it may sometimes also destroy the formatting.<br />

This is because a paragraph in turn consists of individual sub-objects. Each of these sub-objects contains its own<br />

formatting information. If the center of a paragraph, for example, contains a word printed in bold, then it will be<br />

represented in <strong>OpenOffice</strong>.<strong>org</strong> by three paragraph portions: the portion before the bold type, then the word in<br />

bold, and finally the portion after the bold type, which is again depicted as normal.<br />

If the text of the paragraph is now changed using the paragraph's String property, then <strong>OpenOffice</strong>.<strong>org</strong> first<br />

deletes the old paragraph portions and inserts a new paragraph portion. The formatting of the previous sections is<br />

then lost.<br />

To prevent this effect, the user can access the associated paragraph portions rather than the entire paragraph.<br />

Paragraphs provide their own Enumeration object for this purpose. The following example shows a double loop<br />

which passes over all paragraphs of a text document and the paragraph portions they contain and applies the<br />

replacement processes from the previous example:<br />

Dim Doc As Object<br />

Dim Enum1 As Object<br />

Dim Enum2 As Object<br />

Dim TextElement As Object<br />

Dim TextPortion As Object<br />

Doc = ThisComponent<br />

Enum1 = Doc.Text.createEnumeration<br />

' loop over all paragraphs<br />

While Enum1.hasMoreElements<br />

TextElement = Enum1.nextElement<br />

If TextElement.supportsService("com.sun.star.text.Paragraph") Then<br />

Enum2 = TextElement.createEnumeration<br />

' loop over all sub-paragraphs<br />

While Enum2.hasMoreElements<br />

TextPortion = Enum2.nextElement<br />

MsgBox "'" & TextPortion.String & "'"<br />

TextPortion.String = Replace(TextPortion.String, "you", "U")<br />

TextPortion.String = Replace(TextPortion.String, "too", "2")<br />

TextPortion.String = Replace(TextPortion.String, "for", "4")<br />

Wend<br />

End If<br />

Wend<br />

The example runs through a text document in a double loop. The outer loop refers to the paragraphs of the text.<br />

The inner loop processes the paragraph portions in these paragraphs. The example code modifies the content in<br />

each of these paragraph portions using the String property of the string. as is the case in the previous example<br />

for paragraphs. Since however, the paragraph portions are edited directly, their formatting information is retained<br />

when replacing the string.<br />

Chapter 6 · Text Documents 71

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!