Basic macro: how to get a bookmark page number?

I’ve got a letter template where the actual letter text is following an information page. I want to export the actual letter to PDF, without enclosing the information page-s. As this information thing can expand on several pages, according to the user’s typing, I need to know the actual page number where the letter itself starts.

For that I’ve set a bookmark in the letter header. I then need to know at what page this bookmark is. I can do this using the visible cursor, but, as I don’t want to break the work-flow, I’d prefer to use a text cursor instead so that the focused page is not changed.

I cannot find any information about the way to achieve this. Any hint or suggestion appreciated. There’s perhaps another way to achieve that goal.


Use getPage() with the view cursor. From

[The] supports the following cursor capabilities that depend on having the necessary information about the current layout state, therefore it is not supported by the model cursor.

For a more complex solution, it may work to temporarily insert a cross-reference field that displays the page number.


Here is example code using the view cursor.

Sub PageOfBookmark
    oDoc = ThisComponent
    oVC = oDoc.getCurrentController().getViewCursor()
	oBookmark = oDoc.getBookmarks().getByName("Bookmark 1")
	oVC.gotoRange(oBookmark.getAnchor(), False)
	MsgBox "Bookmark is on page " & oVC.getPage()
End Sub

Thanks Jim.
yes, the TextViewCursor might be the thing but, dooh, I feel stupid and can’t figure how to create the needed object :frowning:
I’ll also test the cross-ref way and see what it gives.

Thanks again John,
(and sorry for the late answer)
my current code is very similar to what you’ve provided above. This code actually moves the visible cursor to the bookmark location, which I don’t want (the user won’t understand what’s happening). I’m looking for a solution that would use some other cursor that won’t show that behavior and leave the visible cursor at its current location.
Thanks again, anyway

John??? Anyway, this should be a comment on the question, not an answer. See guidelines for asking. As stated in my answer, model cursors do not support XPageCursor.

According to the OOo Developer’s Guide, p. 828, “Certain information about the current layout, such as the number of lines and page number must be retrieved at the view cursor.” So, it would seem that there’s no way around using the visible cursor.

You might try moving the view cursor to get the page number and then moving it back to it’s previous location. Here’s a possible approach, based on Jim’s example code. Note that the code hasn’t been tested, and I don’t know if such an approach would cause some unsightly flicker of the display.

Sub PageOfBookmark
    oDoc = ThisComponent
    oVC = oDoc.getCurrentController().getViewCursor()

    REM Save view cursor location
    oText = ThisComponent.Text
    oTextCursor = oText.createTextCursorByRange(oVC)

    oBookmark = oDoc.getBookmarks().getByName("Bookmark 1")
    oVC.gotoRange(oBookmark.getAnchor(), False)

    REM Grab page number and then return view cursor to previous location
    pg = oVC.getPage()
    oVC.gotoRange(oTextCursor, False)

    MsgBox "Bookmark is on page " & pg
End Sub