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

search and replace text without deleting bookmarks

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


Joined: 15 May 2006
Posts: 6

PostPosted: Tue May 16, 2006 3:59 am    Post subject: search and replace text without deleting bookmarks Reply with quote

Using openoffice.org 2.0.2 Basic, how can I do search and replace whiteout deleting any bookmarks that may occur in the text that I'm searching for?

Following code will delete bookmarks:

Code:
oReplace = ThisComponent.createReplaceDescriptor()
oReplace.setSearchString(sChars$)
oReplace.setReplaceString("")
oReplace.SearchRegularExpression = True
oReplace.searchAll = True
ThisComponent.replaceAll(oReplace)
Back to top
View user's profile Send private message
knobo
General User
General User


Joined: 15 May 2006
Posts: 6

PostPosted: Thu May 18, 2006 1:52 am    Post subject: Search and destroy - elaboration Reply with quote

I can improve my question a bit..
I have in my document the text &&& several places. As in the example on the line under where the | character represents a bookmark

|&&& This line should be removed.

It Actually looks like the bookmark is on top of the character, because when I delete the first & manually, the bookmark is also removed. I want to search my document for all places where &&& are and remove the &&& without removing the bookmark, if there is a bookmark there.

In MSOff. its possible to delete text without deleting the bookmark which is in the front of the text, and the code which I'm translating from msoff. to Openoffice.org uses this quite alot.
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Thu May 18, 2006 4:56 am    Post subject: Reply with quote

I do:
1) Set bookmark at beginning of line and then type &&&.
2) Type &&& at beginning of line, select it and set bookmark.

I find it interesting, and the opposite of what I expected, that if I do your find and replace, the bookmark in case 1 above is removed but in case 2 it is not.
Back to top
View user's profile Send private message
knobo
General User
General User


Joined: 15 May 2006
Posts: 6

PostPosted: Thu Oct 05, 2006 2:04 am    Post subject: Is there a better way: search and (not) destroy (bookmarks) Reply with quote

I have found a solution, but right now the code is so ugly that I'll not post it. If someone wants it, tell me, and I'll clean it up.

I'll just post a snip of it:

Code:

   On Error GoTo RemErr
      
   oSearch= ThisComponent.createSearchDescriptor()
   oSearch.setSearchString( "^" + sChars$ + ".*$" )
   oSearch.SearchRegularExpression = True

   oMatch = ThisComponent.findFirst( oSearch )

   while not isNull(oMatch)
      dim oBookmark
      dim oBookmarkElements
      dim i
      'oMatch.goRight(1,true)                   'Added 1209 to see if it helps with newline issues. It helpt, but fsck'ed up some cases
      oBookmarkElements = ThisComponent.getBookmarks().ElementNames
      BStackcount = 0

      for i = 0 to UBound( oBookmarkElements )
         oBookmark = ThisComponent.getBookmarks().getByName( oBookmarkElements(i) ).getAnchor()
         If EqualUNOObjects(oMatch.getText(), oBookmark.getText()) then
            Select Case oMatch.getText().compareRegionStarts(oBookmark.getEnd(),oMatch.getStart())
               Case 0
                  BStack( BStackcount ) = oBookmarkElements(i)
                  BStackcount = Bstackcount + 1
               Case 1
                  pos = oMatch.getText().compareRegionStarts(oBookmark.getStart(),oMatch.getStart())
                  if pos <= 0 then
                     BStack( BStackcount ) = oBookmarkElements(i)
                     BStackcount = Bstackcount + 1
                  end if
               Case -1
                  pos = oMatch.getText().compareRegionStarts(oBookmark.getStart(),oMatch.getEnd())
                  if pos >= 0 then
                     BStack( BStackcount ) = oBookmarkElements(i)
                     BStackcount = Bstackcount + 1
                  end if
               Case Else
               End Select

         dim newBookmark
         dim j As Integer

         oMatch.setString("")

         for j = 0 to ( BStackcount - 1 )
            dim NewMarkName
            NewMarkName = BStack( j )
            
            if not ThisComponent.Bookmarks.hasByName(NewMarkName) Then
               newBookmark = ThisComponent.createInstance("com.sun.star.text.Bookmark")
               newBookmark.Name = NewMarkName
               ThisComponent.Text.insertTextContent(oMatch, newBookmark, false)
            End If
         next j   

      oMatch = ThisComponent.findNext( oMatch, oSearch)
   wend



Hmm.. I actualy ended up cleaning up some small issues, but this is not a good editor to write in (browserwindow). So this code might not work "as is".

If someone have a better way to do this, I would be glad.
Back to top
View user's profile Send private message
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