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.

The Structure of Text Documents<br />

Paragraphs and Paragraph Portions<br />

The core of a text document consists of a sequence of paragraphs. These are neither named nor indexed and there<br />

is therefore no possible way of directly accessing individual paragraphs. The paragraphs can however be<br />

sequentially traversed with the help of the Enumeration object described in Introduction to the API. This allows<br />

the paragraphs to be edited.<br />

When working with the Enumeration object, one special scenario should, however, be noted: it not only returns<br />

paragraphs, but also tables (strictly speaking, in <strong>OpenOffice</strong>.<strong>org</strong> Writer, a table is a special type of paragraph).<br />

Before accessing a returned object, you should therefore check whether the returned object supports the<br />

com.sun.star.text.Paragraph service for paragraphs or the com.sun.star.text.TextTable service for tables.<br />

The following example traverses the contents of a text document in a loop and uses a message in each instance to<br />

inform the user whether the object in question is a paragraph or table.<br />

Dim Doc As Object<br />

Dim Enum As Object<br />

Dim TextElement As Object<br />

' Create document object<br />

Doc = ThisComponent<br />

' Create enumeration object<br />

Enum = Doc.Text.createEnumeration<br />

' loop over all text elements<br />

While Enum.hasMoreElements<br />

TextElement = Enum.nextElement<br />

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

MsgBox "The current block contains a table."<br />

End If<br />

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

MsgBox "The current block contains a paragraph."<br />

End If<br />

Wend<br />

The example creates a Doc document object which references the current <strong>OpenOffice</strong>.<strong>org</strong> document. With the<br />

aid of Doc, the example then creates an Enumeration object that traverses through the individual parts of the<br />

text (paragraphs and tables) and assigns the current element to TextElement object. The example uses the<br />

supportsService method to check whether the TextElement is a paragraph or a table.<br />

Paragraphs<br />

The com.sun.star.text.Paragraph service grants access to the content of a paragraph. The text in the paragraph can<br />

be retrieved and modified using the String property:<br />

Dim Doc As Object<br />

Dim Enum As Object<br />

Dim TextElement As Object<br />

Doc = ThisComponent<br />

Enum = Doc.Text.createEnumeration<br />

While Enum.hasMoreElements<br />

TextElement = Enum.nextElement<br />

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

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

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

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

End If<br />

Wend<br />

The example opens the current text document and passes through it with the help of the Enumeration object. It<br />

uses the TextElement.String property in all paragraphs to access the relevant paragraphs and replaces the<br />

you, too and for strings with the U, 2 and 4 characters. The Replace function used for replacing does not<br />

fall within the standard linguistic scope of <strong>OpenOffice</strong>.<strong>org</strong> Basic. This is an instance of the example function<br />

70 <strong>OpenOffice</strong>.<strong>org</strong> 3.2 <strong>BASIC</strong> <strong>Guide</strong> · March 2010

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

Saved successfully!

Ooh no, something went wrong!