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.

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

Enum2 = TextElement.createEnumeration<br />

' loop over all paragraph portions<br />

While Enum2.hasMoreElements<br />

TextPortion = Enum2.nextElement<br />

If TextPortion.CharWeight = _<br />

com.sun.star.awt.FontWeight.BOLD AND _<br />

TextPortion.getPropertyState("CharWeight") = _<br />

com.sun.star.beans.PropertyState.DIRECT_VALUE Then<br />

TextPortion.setPropertyToDefault("CharWeight")<br />

TextPortion.CharStyleName = "MyBold"<br />

End If<br />

Wend<br />

End If<br />

Wend<br />

Editing Text Documents<br />

The Structure of Text Documents<br />

The previous section has already discussed a whole range of options for editing text documents, focusing on the<br />

com.sun.star.text.TextPortion and com.sun.star.text.Paragraph services, which grant access to paragraph portions<br />

as well as paragraphs. These services are appropriate for applications in which the content of a text is to be edited<br />

in one pass through a loop. However, this is not sufficient for many problems. <strong>OpenOffice</strong>.<strong>org</strong> provides the<br />

com.sun.star.text.TextCursor service for more complicated tasks, including navigating backward within a<br />

document or navigating based on sentences and words rather than TextPortions.<br />

The TextCursor<br />

A TextCursor in the <strong>OpenOffice</strong>.<strong>org</strong> API is comparable with the visible cursor used in a <strong>OpenOffice</strong>.<strong>org</strong><br />

document. It marks a certain point within a text document and can be navigated in various directions through the<br />

use of commands. The TextCursor objects available in <strong>OpenOffice</strong>.<strong>org</strong> Basic should not, however, be confused<br />

with the visible cursor. These are two very different things.<br />

Note – VBA : Terminology differs from that used in VBA: In terms of scope of function, the Range object<br />

from VBA can be compared with the TextCursor object in <strong>OpenOffice</strong>.<strong>org</strong> and not — as the name possibly<br />

suggests — with the Range object in <strong>OpenOffice</strong>.<strong>org</strong>.<br />

The TextCursor object in <strong>OpenOffice</strong>.<strong>org</strong>, for example, provides methods for navigating and changing text which<br />

are included in the Range object in VBA (for example, MoveStart, MoveEnd, InsertBefore, InsertAfter). The<br />

corresponding counterparts of the TextCursor object in <strong>OpenOffice</strong>.<strong>org</strong> are described in the following sections.<br />

Navigating within a Text<br />

The TextCursor object in <strong>OpenOffice</strong>.<strong>org</strong> Basic acts independently from the visible cursor in a text document.<br />

A program-controlled position change of a TextCursor object has no impact whatsoever on the visible cursor.<br />

Several TextCursor objects can even be opened for the same document and used in various positions, which are<br />

independent of one another.<br />

A TextCursor object is created using the createTextCursor call:<br />

Dim Doc As Object<br />

Dim Cursor As Object<br />

Doc = ThisComponent<br />

Cursor = Doc.Text.createTextCursor()<br />

The Cursor object created in this way supports the com.sun.star.text.TextCursor service, which in turn<br />

provides a whole range of methods for navigating within text documents. The following example first moves the<br />

TextCursor ten characters to the left and then three characters to the right:<br />

Chapter 6 · Text Documents 75

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

Saved successfully!

Ooh no, something went wrong!