Ask Your Question
0

Basic macro: how to get a bookmark page number?

asked 2018-01-29 08:57:04 +0200

jfn gravatar image

Hi, 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.

Thanks,

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
2

answered 2018-01-29 15:16:51 +0200

Jim K gravatar image

updated 2018-01-30 14:19:04 +0200

Use getPage() with the view cursor. From https://wiki.openoffice.org/wiki/Docu...:

[The com.sun.star.text.TextViewCursor] 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.

com.sun.star.text.XPageCursor

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

EDIT:

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
edit flag offensive delete link more

Comments

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

jfn gravatar imagejfn ( 2018-01-29 16:04:57 +0200 )edit
0

answered 2018-02-04 20:52:01 +0200

jfn gravatar image

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

edit flag offensive delete link more

Comments

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.

Jim K gravatar imageJim K ( 2018-02-05 20:14:45 +0200 )edit
0

answered 2018-02-05 13:43:10 +0200

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
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-01-29 08:57:04 +0200

Seen: 141 times

Last updated: Feb 05 '18