Page 1 of 1

[Solved] How To Delete a Bookmark in VB.NET?

Posted: Tue Dec 20, 2011 6:06 pm
by cluelessDev114
Hello All,
I am trying to create a document from a template and delete a specific bookmark. The code i use to get the boookmark is

Code: Select all

        Dim BookmarksSupplier As XBookmarksSupplier = CType(Document, XBookmarksSupplier)
        Dim FoundBookmark As XTextContent = CType(BookmarksSupplier.getBookmarks.getByName(Bookmark).Value, XTextContent)
        Dim BookmarkField As XTextRange = FoundBookmark.getAnchor()
The bookmark i am deleting inserts the word 'Dear' before the bookmark text, using

Code: Select all

BookmarkField.setString("")

leaves the 'Dear' in the document i have also tried

Code: Select all

FoundBookmark.dispose()
and

Code: Select all

Dim Writer As XTextDocument = CType(Document, XTextDocument)
Writer.getText.removeTextContent(FoundBookmark)
both to no effect.

Thanks for your help

Re: How To Delete a Bookmark in VB.NET?

Posted: Wed Dec 21, 2011 12:10 am
by FJCC
This OOoBasic code deletes the text encompassed by the bookmark and the bookmark itself.

Code: Select all

oBookmarks = ThisComponent.Bookmarks
oMark = oBookmarks.getByName("Mark1")
oMark.Anchor.String = ""
oMark.dispose()
I notice the you say that the word "Dear" is before the bookmark. Does the word get highlighted if you jump to the bookmark? If not, then you cannot manipulate the bookmark anchor to delete it.

Re: How To Delete a Bookmark in VB.NET?

Posted: Wed Dec 21, 2011 2:01 pm
by cluelessDev114
Thanks for your help.

I think your right, its not actually part of the bookmark, just text before the bookmark which is why this is not working

Re: [SOLVED]How To Delete a Bookmark in VB.NET?

Posted: Wed Dec 21, 2011 3:43 pm
by FJCC
If the word is consistently placed relative to the bookmark, it could still be manipulated by using a text cursor. For example

Code: Select all

oBookmarks = ThisComponent.Bookmarks
oMark = oBookmarks.getByName("Mark1")
oText = ThisComponent.Text
oCurs = oText.createTextCursor
oCurs.gotoRange(oMark.Anchor, False)
oCurs.gotoPreviousWord(True)
oCurs.String = ""
I don't know that will work in your case, but I hope it gives you an idea of how a cursor may be used.