Getting selected text in Writer document?

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
raff_nix
Posts: 17
Joined: Wed Jan 05, 2011 4:05 pm

Getting selected text in Writer document?

Post 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
OpenOffice 3.3 on Windows 7
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: getting selected text in writer document?

Post 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
raff_nix
Posts: 17
Joined: Wed Jan 05, 2011 4:05 pm

Re: getting selected text in writer document?

Post 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. :(
OpenOffice 3.3 on Windows 7
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: getting selected text in writer document?

Post by JohnV »

.getViewData()
Don't do Java anymore but should this be:
.getViewCursor()?
FJCC
Moderator
Posts: 9623
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: getting selected text in writer document?

Post 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.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
raff_nix
Posts: 17
Joined: Wed Jan 05, 2011 4:05 pm

Re: Getting selected text in writer document?

Post 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?
OpenOffice 3.3 on Windows 7
FJCC
Moderator
Posts: 9623
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Getting selected text in writer document?

Post 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.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
raff_nix
Posts: 17
Joined: Wed Jan 05, 2011 4:05 pm

Re: Getting selected text in Writer document?

Post 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?
OpenOffice 3.3 on Windows 7
Post Reply