Alle 00:31, martedì 17 aprile 2007, Johnny Andersson ha scritto:
> I have a small problem that I just can't figure out at these late hours
> (00:13 right now):
>
> I have written a macro that reads highlighted text and replaces it with
> something else (which is calculated by the macro).
>
> To read the highlighted text I use ThisComponent.getCurrentSelection
> ().getByIndex(0).getString().
>
> This caused me no problems, however I couldn't figure out the parameter in
> getByIndex(0). Why 0? What does it mean? 0 what? I tried 1 and 2 but they
> only caused errors.

Because the getCurrentSelection() method in writer normally gets a
com.sun.star.text.TextRanges (that is a container)
In general it contains at least one TextRange, but in case of multiple 
selection it contains more elements.
Use the getCount() method to discover how many TextRanges are actually 
selected


> Ok, I guess I can figure that one out myself with some help from Google, so
> here is my real question:
>
> When I replace the highlighted text, I do that with
> ThisComponent.getCurrentSelection().getByIndex(0).setString(MyString). The
> problem is that I want the new inserted string also to be highlighted, so I
> can perform the macro on it without having to highlight it manually.
>
> Any ideas? Is there some better way to do this?
>
> Just an example to illustrate what I mean:
[....]

Hoping that I have correctly understood your example, the following code 
should do (more or less) what you need.
Check the Andrew Pitonyak's macro document for better techniques and examples

-----------8<-----------
REM  *****  BASIC  *****

Sub Main

oRange = ThisComponent.currentselection(0)
oText = oRange.getText()

oCursor = oText.createTextCursorByRange(oRange)
bAbsorb = True
oText.insertString( oRange, "new string", bAbsorb)

'restore selection
ThisComponent.CurrentController.select(oRange)

End Sub
-----------8<-----------


ciao
Paolo M

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to