Writer macro to go to a bookmark

There is a way to write a (Libre Writer) macro to go to a certain bookmark?
(registering it’s not possible)
Thank you

Yes, depending on what you mean to “go to” a bookmark.

SUB jump2Bookmark(s)
' pass the name of the bookmark
' to scroll to bookmark
ViewCursor = ThisComponent.CurrentController.getviewCursor()
Bookmark = .ThisComponent.Bookmarks.getByName(s).Anchor
ViewCursor.gotorange(Bookmark, False)
END SUB

Does this answer your question, already?
Anyway, if you want to manipulate a boolmark’s content… this should show you how to access it :wink:

Thank you. It seems work. I will do more tests.

Does not completely works if (Bookmark, False) : no focus on cursor; for some aspects better (fucus on cursor) if (Bookmark, True). To work I have to move the cursor, so focus on cursor.
The problems are the non writer apps: Calc, Dbase: I get always an error message :frowning:
This is the macro: "
sub vai_qui
ViewCursor = ThisComponent.CurrentController.getviewCursor()
Bookmark = ThisComponent.Bookmarks.getByName(“qui”).Anchor
ViewCursor.gotorange(Bookmark, False)
end

Thank you, komma4. Now I need some code to : 1) focus on cursor; 2) avoid error message in Calc and in Base. This second thing: is not possible an “if” command (if there is “qui” bookmark, then … your macro)

To avoid error messages on other modules of LO than Writer:

  • don’t call the macro from any other document than a Writer in the first place :slight_smile:

  • check for supportsService;

    If NOT ThisComponent.supportsService(_
    “com.sun.star.text.TextDocument” ) Then
    Exit Sub
    End If

  • check for exsitance of a bookmark

    oBookmarks = ThisComponent.getBookmarks()
    If NOT oBookmarks.hasByName("qui") Then

Get MRI extension and generate / write your code

Thank you, komma4. Remains only the problem of focus :frowning:

Solution of the focus problem: if I re-do the macro, it’s all OK. So I duplicate, I repeat the instructions 2 times, and so it works!

The solution of komma4 does not work properly with the new release of LibreOffice (Version: 5.4.1.2
Build ID: 1:5.4.1-0ubuntu1 - CPU threads: 4; OS: Linux 4.13; UI render: default; VCL: kde4; Locale: it-IT (en_US.UTF-8); Calc: group).
I get an error message: “BASIC runtime error. Property or method not found: supportsService”
Could you help me?

As stated above: this is a macro for WRITER.
CALC doesn’t have bookmarks.
In case I you tried in WRITER (“Calc: group” in your version information): which statement is highlighted, as the error occurs?

Thank you. I know this is a writer macro, and in effect in writer it works well; but not in base, after upgrade to kubuntu 17.10. In Librebase (not in LibreCalc, strange) I get that error message :(. in the current version of LibreOffice it seems that supportService is not recognized in LibreBase.

I tried a temporary workaround: created an empty macro with the same name (“vai_qui”) in the odb file I open often, and set macro security level to “low” (I know: not recommended). But don’t works.

not sure, but ThisComponentinstruction refers to the document where the macro is saved/attacht to… If you store the macro in the ODB file and want to use is within a Form (which is another document with ODT format) - it doesn’t work. Your workaround points to this.

Hello. I cannot get komma4’s solution to work at all in LO 6.3.6.2. I am coming from the Word VBA world and creating a macro to go to a bookmark was easy there. I created it years ago in WordBasic and it still works. It looks like this:

WordBasic.EditBookmark Name:=“myplace”, SortBy:=0, GoTo:=1

…and that’s all. Might there be a way to translate this to Writer? Thanks for any help!
Les