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

How To: Replace Bookmark Text in VB.NET and VS 2010

Post new topic   Reply to topic Forum Index -> Code Snippets
View previous topic :: View next topic  
Author Message

Joined: 17 Aug 2010
Posts: 3

PostPosted: Fri Aug 20, 2010 11:10 pm    Post subject: How To: Replace Bookmark Text in VB.NET and VS 2010 Reply with quote


Spent a lot of time (because I'm new to OpenOffice) getting this to work. Thanks very much to Andrew Pitonyak who gave me some very usefull pointers.

I struggled to get the bookmarks to work, but to be fair, its pretty easy really.

The code for this follows, but to use it you ned to create a Windows Forms Application, add Form1, add two buttons called OpenExisitingDocButton and ReplaceBookmarksButton.

I think the rest is pretty self explanatory. Here is the code:


Option Explicit On
Option Strict On

Imports System.Windows.Forms
Imports System.IO


Public Class Form1

    Private myLocalContext As XComponentContext
    Private myMultiServiceFactory As XMultiServiceFactory
    Private myComponentLoader As XComponentLoader
    Private myComponent As XComponent
    Private myTemplateComponent As XComponent
    Private myTextDocument As XTextDocument

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub CallBootstrap()
        ' Call the bootstrap method to get a new ComponentContext object.
        ' If OpenOffice isn't already started this will start it and then return the ComponentContext.
        myLocalContext = uno.util.Bootstrap.bootstrap()

    End Sub

    Private Sub GetServiceManager()
        ' Get a new service manager of the MultiServiceFactory type
        ' we need this to get a desktop object and create new CLI objects.
        myMultiServiceFactory = CType(myLocalContext.getServiceManager(),

    End Sub

    Private Sub CreateNewDesktopInstance()
        ' Create a new Desktop instance using our service manager
        ' Notice: We cast our desktop object to XComponent loader
        ' so that we can load or create new documents.
        myComponentLoader = CType(myMultiServiceFactory.createInstance(""), XComponentLoader)

    End Sub

    Private Sub OpenExistingWriterDocument(ByVal FileNameAndPath As String)

        ' get the filename, check it exists, load it
        myTemplateComponent = myComponentLoader.loadComponentFromURL("file:///" & FileNameAndPath, "_blank", 0, New {})

        ' myTextDocument = DirectCast(myTemplateComponent,

    End Sub

    Private Sub OpenExistingDocButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenExisitingDocButton.Click
        ' open existing doc

    End Sub

    Private Sub ReplaceBookmarksButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceBookmarksButton.Click
        ' replace the bookmarks
        Dim myBookmarksSupplier As XBookmarksSupplier = DirectCast(myTemplateComponent, XBookmarksSupplier)
        Dim myNamedBookmarks As XNameAccess = myBookmarksSupplier.getBookmarks()
        Dim myBookmark As uno.Any
        Dim bmCount As Integer = myNamedBookmarks.getElementNames.Count

        For x As Integer = 0 To bmCount - 1
            ' MsgBox(myNamedBookmarks.getElementNames.ElementAt(x).ToString)
            myBookmark = myNamedBookmarks.getByName(myNamedBookmarks.getElementNames.ElementAt(x).ToString)
            Dim myBookmarkContent As XTextContent = CType(myBookmark.Value, XTextContent)
            Dim myAnchor As XTextRange = CType(myBookmarkContent.getAnchor(), XTextRange)
            ReplaceBookmark(myNamedBookmarks.getElementNames.ElementAt(x).ToString, myAnchor)
    End Sub

    Private Sub ReplaceBookmark(ByVal bookmarkName As String, ByVal anchor As XTextRange)

        Select Case bookmarkName
            Case "Petname"

            Case "Clientname"

            Case "Address"
                anchor.setString("I live here...")

            Case Else
                ' nop

        End Select
    End Sub

End Class

Hope this helps someone get over one or two pitfalls I fell into.


Back to top
View user's profile Send private message

Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Sun Aug 22, 2010 11:02 am    Post subject: Reply with quote

Excellent.... glad it works (figured I would post rather than email directly) Laughing
Andrew Pitonyak
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic Forum Index -> Code Snippets 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