2007/4/17, Paolo Mantovani <[EMAIL PROTECTED]>:
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
Oh, I see. I didn't realize that it was possible to have more than one selection in Writer (I knew it's possible in Calc though), but a quick test with the Ctrl key certainly proves to me that it's possible. Yes, maybe I should include that possibility in my macro, so not only the first (or rather 0th...) selection is handled.
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 I haven't tried your suggestion yet, but I will as soon as possible.
Just one follow-up-question, if you don't mind: The objects we are talking about here, ThisComponent and its sub objects (or whatever I should call them), contain other objects, variables and methods etc, right? Many years ago when I studied some C++ (which I never used since then, sorry...) I was told that when created classes, it was a good idea to keep variables private and do most things with them through public local methods. To me it felt like the proper thing to do is to always use local methods for everything, when it comes to changing values and contents of things. In your example you go for an object directly: ThisComponent.CurrentSelection(0). In my example I do like this to obtain the same (?) thing: ThisComponent.getCurrentSelection().getByIndex(0) My thought was something like "since there is a method, it is probably supposed to be used". On the other hand, if ThisComponent.CurrentSelection(0) is possible to do, that object is obviously not private, so I should feel free to use it. Which is the "proper" way to do things? Is the rule that "if a variable or an object is private, use their methods, otherwise manipulate them directly"? Or is it "always use the method designed for what you want to do with the variable/object"? Or is it something else? Johnny Andersson
