Page 1 of 1

Getting selected text in Writer document?

Posted: Sat Mar 12, 2011 8:01 pm
by raff_nix
Hello everybody,

I want to get selected Strings in a Writer Document. So I tried the following Java-Code, but it causes a runtime exception in openoffice.org. I don't know, whats wrong.

Code: Select all

XIndexAccess acc = UnoRuntime.queryInterface(XIndexAccess.class, xModel.getCurrentSelection());
XTextRange range = UnoRuntime.queryInterface(XTextRange.class, acc.getByIndex(0));

String selectedText = range.getString();
If I call the range.getString() function, the error is caused. :(
What I have to do, to get selected Text?

Appreciate for any kind of help

Re: getting selected text in writer document?

Posted: Sat Mar 12, 2011 9:01 pm
by JohnV
Perhaps is Basic code will help.

Code: Select all

Sub Main
oVC = ThisComponent.CurrentController.getViewCursor
If oVC.isCollapsed then
  Print "Nothing selected."
 Else Print oVC.String 
EndIf
End Sub

Re: getting selected text in writer document?

Posted: Sat Mar 12, 2011 9:35 pm
by raff_nix
Hello,

thanks, but i tried it in java, but it doesn't work.

Code: Select all

String txt = xModel.getCurrentController().getViewData().toString();
I only get the positions of selected text, but not the text itself. :(

Re: getting selected text in writer document?

Posted: Sat Mar 12, 2011 11:33 pm
by JohnV
.getViewData()
Don't do Java anymore but should this be:
.getViewCursor()?

Re: getting selected text in writer document?

Posted: Sun Mar 13, 2011 8:13 am
by FJCC
I messed around with JavaScript, about which I know nothing, and got this to work for changing the string of the current selection

Code: Select all

importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.text.XTextRange);
importClass(Packages.com.sun.star.container.XIndexAccess);
oDoc = XSCRIPTCONTEXT.getDocument();
oCurSel = oDoc.getCurrentSelection();
xIndex = UnoRuntime.queryInterface(XIndexAccess,oCurSel);
Sel0 = xIndex.getByIndex(0);
xTextRange = UnoRuntime.queryInterface(XTextRange,Sel0);
xTextRange.setString("BLAH");
So I suggest querying the XTextRange interface of the selection and using the setString() method of that.

Re: Getting selected text in writer document?

Posted: Tue Mar 15, 2011 5:49 pm
by raff_nix
Hello,

Thanks it works, but now i have the problem, that, if i have a table an images in my writer document the runtime error occurs. But if i only have text without other embedded elements it works. I don't know how to solve this problem. :? Any ideas?

Re: Getting selected text in writer document?

Posted: Tue Mar 15, 2011 8:47 pm
by FJCC
I just tried my code on a document with a table and an embedded picture and it works fine. I'm sorry I don't have anything to suggest.

Re: Getting selected text in Writer document?

Posted: Wed Mar 16, 2011 7:43 pm
by raff_nix
I think the problem is that if i use Documents which are written with MS Word and are transformed into odt file. Then the error occurs. If i write the documents in openoffice, it works... but if somebody send me an document in doc or docx format, i get this problem... of openoffice runtime error... :(

Any ideas? is there a way to convert doc or docx files into odt without problems and then getting selected text?