jcrick Newbie

Joined: 17 Aug 2010 Posts: 3
|
Posted: Fri Aug 20, 2010 11:10 pm Post subject: How To: Replace Bookmark Text in VB.NET and VS 2010 |
|
|
Hi
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:
| Code: |
Option Explicit On
Option Strict On
Imports System.Windows.Forms
Imports System.IO
Imports unoidl.com.sun.star.lang
Imports unoidl.com.sun.star.uno
Imports unoidl.com.sun.star.bridge
Imports unoidl.com.sun.star.frame
Imports unoidl.com.sun.star.text
Imports unoidl.com.sun.star.container
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(), unoidl.com.sun.star.lang.XMultiServiceFactory)
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("com.sun.star.frame.Desktop"), 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 unoidl.com.sun.star.beans.PropertyValue(-1) {})
' myTextDocument = DirectCast(myTemplateComponent, unoidl.com.sun.star.text.XTextDocument)
End Sub
Private Sub OpenExistingDocButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenExisitingDocButton.Click
' open existing doc
CallBootstrap()
GetServiceManager()
CreateNewDesktopInstance()
OpenExistingWriterDocument("C:\OpenOfficeWriterIntegrationTestDoc2.odt")
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)
Next
End Sub
Private Sub ReplaceBookmark(ByVal bookmarkName As String, ByVal anchor As XTextRange)
Select Case bookmarkName
Case "Petname"
anchor.setString("Daisy")
Case "Clientname"
anchor.setString("Gauntlett")
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.
Regards
Jerry |
|