OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Find text by bookmark and replace it

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
briceandre
Newbie
Newbie


Joined: 17 Mar 2012
Posts: 1

PostPosted: Sat Mar 17, 2012 2:27 am    Post subject: Find text by bookmark and replace it Reply with quote

Hello,

I am writing w VB script to manipulate OOO documents. In the documents, the user can insert text bookmarks and my scritp replaces this bookmark with other text.

The macro I wrote properly works for nearly all documents, except when the bookmark is placed within a table.

here is a part of the code I use :
Code:

         ' Retrieve information on the position of the bookmark
         bookmarkAnchor = bookmark.getAnchor()
         
         ' Set the cursor just after the tag
         cursor.gotoRange(bookmarkAnchor.getStart(), false)
         cursor.goRight(1,0)
         
         ' Insert a new line just before the insertion point.
         ' This will allow keeping the style of the first line of the content file if the bookmark is at the beginning of the line
         template_file.Text.insertControlCharacter(cursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
         
         ' Insert content of the content file
         template_file.getCurrentController().insertTransferable(content_data)
         
         ' Remove the paragraph that we have generated at the end of the content file
         cursor.goLeft(1,1)
         cursor.setString("a")
         
         ' Set the cursor at the beginning of the insertion point
         cursor.gotoRange(bookmarkAnchor.getStart(), false)
         cursor.goRight(1,0)
         
         ' Remove the whole tag
         cursor.goLeft(markSize,1)
         cursor.setString("a")
         
         ' Remove the line that was inserted before the content insertion
         cursor.goRight(1,1)
         cursor.setString("a")



The problem is that, when the bookmark is placed in an array, the text insertion part (template_file.getCurrentController().insertTransferable(content_data)) works perfectly, all the text selection stuff too (cursor.gotoRange, cursor.goRight, etc.) and the proper code is selected, but when I execute all cursor.setString("a") instructions, the selected text is not replaced by "a". In fact, the "a" character is not inserted at all.

In the original code, the cursor.setString("a") was replaced by cursor.setString("") and the effect on a document where the bookmark is not within a table is that the selected text is suppressed. But when the bookmark is in a table, those instructions have no effect.

Does someone has an idea ?

Thanks in advance,
Brice



Moderation probe1: moved to MACROS AND API section, where all macro related questions belong to;
Back to top
View user's profile Send private message
probe1
Moderator
Moderator


Joined: 18 Aug 2004
Posts: 2560
Location: Chonburi Thailand Asia

PostPosted: Fri Mar 23, 2012 3:31 am    Post subject: Reply with quote

This works for me - without the stupid cursor movement

Code:
SUB replaceBookmarkTextInTable

oBM = ThisComponent.getBookmarks().getByName( "wr" )

' Inhalt setzen
oBM.getAnchor().String = "neu"

END SUB

_________________
Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group