Macro code to access bookmarks in another document

From my main document “CreateBookmarks” my macro code creates a new document based on a template “Bookmarks” that contains several bookmarks. I am having difficulties referring to these bookmarks.

Here is my test code contained in “CreateBookmarks”:

Sub CreateBookmarkDoc

    Dim sURL as String
    Const sPathName = "F:\Users\ifsian\Documents\OpenOffice\Templates\Bookmarks.ott"

	sURL = ConvertToURL(sPathName)
	StarDesktop.loadComponentFromURL(sURL,0,"_default",Array())
	
	msgbox "Bookmark Count: " & ThisComponent.getBookmarks().getcount()

End Sub

When executed in the original document, ThisComponent.getBookmarks().getcount() returns 0. If the above code is copied into a module under MyMacros and Dialogs and executed it works. A new document is created and the count of bookmarks in the document just created returned. I am guessing ThisComponent.getBookmarks().getcount() is counting bookmarks in the original document rather than the document just created even though this document has focus.

My question is:

What is the correct syntax to access bookmarks in another document?

Edited format of code block. mark_t

Instead of using ThisComponent use the object returned by loadComponentFromURL.

Example:- (Note I have not tested this but it should be OK)

Edit to correct format of the code

Sub CreateBookmarkDoc 

    Dim sURL as String 
    Const sPathName = "F:\Users\ifsian\Documents\OpenOffice\Templates\Bookmarks.ott"
    Dim oDoc As Object

    sURL = ConvertToURL(sPathName)
    oDoc = StarDesktop.loadComponentFromURL(sURL,0,"_default",Array())

    msgbox "Bookmark Count: " & oDoc.getBookmarks().getcount()

End Sub

Thank you mark_t that works.

I am still trying to understand the LO object model. I have been reading Andrew Pitonyak’s book and recording macros to attempt to gain a better understanding of LO macros but there are times when my old addled brain fails me!

PS: What is the protocol for marking a question as [closed]?

If you could check the grey tickmark next to the answer that best answers your question. You might also find a close option below your question, but I’m not sure if that requires minimum ammount of Karma to be enabled.