briceandre Newbie

Joined: 17 Mar 2012 Posts: 1
|
Posted: Sat Mar 17, 2012 2:27 am Post subject: Find text by bookmark and replace it |
|
|
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; |
|
probe1 Moderator


Joined: 18 Aug 2004 Posts: 2465 Location: Chonburi Thailand Asia
|
Posted: Fri Mar 23, 2012 3:31 am Post subject: |
|
|
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 |
|