[Solved] Can't change a bookmarked word in a table in Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

[Solved] Can't change a bookmarked word in a table in Writer

Post by zenira »

Hi
I can change word outside in table but can't change inside in table in Writer Document.
This code works
Image

Code: Select all

Sub ChangeWord
dim oDoc as object
dim oVal()
dim Bookmarks,oAnchor,oCurs as object
url="D:\work.odt" 
oDoc=StarDesktop.LoadComponentFromURL(ConvertToURL(url),"_blank",0,oVal())
Bookmarks=oDoc.getBookmarks()
oAnchor = Bookmarks.getByName("Dog").getAnchor()
oCurs = oDoc.getCurrentController().getViewCursor()
oCurs.gotoRange(oAnchor, False)
oCurs.String ="Cad"
End Sub
But when I move the "Dog" word into a table, code does not change it to "Cad"
Image

Code: Select all

Sub ChangeWord
dim oDoc as object
dim oVal()
dim Bookmarks,oAnchor,oCurs as object
dim oTable as object
url="D:\work.odt" 
oDoc=StarDesktop.LoadComponentFromURL(ConvertToURL(url),"_blank",0,oVal())
Bookmarks=oDoc.getBookmarks()
oAnchor = Bookmarks.getByName("Dog").getAnchor()
oCurs = oDoc.getCurrentController().getViewCursor()
oCurs.gotoRange(oAnchor, False)
oCurs.String ="Cad"
End Sub
Thanks for your help
Last edited by Hagar Delest on Sun Mar 12, 2017 12:57 pm, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 4.1.0.4 on Windows 7
FJCC
Moderator
Posts: 9231
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Why I can't change a bookmarked word in a table in Write

Post by FJCC »

I see the same problem in OpenOffice. As an alternative, this seems to work

Code: Select all

 Bookmarks=oDoc.getBookmarks()
oAnchor = Bookmarks.getByName("Dog").getAnchor()
oAnchor.String = "Cad"
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.
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: Why I can't change a bookmarked word in a table in Write

Post by zenira »

Oh thank you FJCC. But what is different with oCurs and without oCurs?
LibreOffice 4.1.0.4 on Windows 7
User avatar
Hagar Delest
Moderator
Posts: 32594
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Can't change a bookmarked word in a table in Wr

Post by Hagar Delest »

The tables are special objects in Writer and I think that if you want to go that way (with a cursor), you need to handle the text first and then the tables. Whereas changing the content by the .String command is more straightforward.
Since the Bookmarks array allows you to edit its entries, no need of a cursor.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
zenira
Posts: 88
Joined: Sun May 11, 2014 11:24 am

Re: [Solved] Can't change a bookmarked word in a table in Wr

Post by zenira »

Thank you Hagar Delest
LibreOffice 4.1.0.4 on Windows 7
Post Reply