Macro to go to bookmark

In 2020 komma4 published a macro to be used in a form to go to a bookmark. I copied it but encountered a couple of problems.

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)
  1. The name of my bookmark is “End”. How do I “pass the name of the bookmark” in the correct syntax?

  2. When I run the macro it complains about a basic error pointing to the dot . after "Bookmark = " Is the dot required or not?

Could somebody point a beginner to the right way.

Do you really need a macro for that (apart from a pedagogical goal to study macro syntax)?

You just need to double-click in the bookmark name in the Navigator side pane.


jump2Bookmark "end"

and remove the starting dot in


I added this functionality to OOO Development Tools (OooDev).
This is for python but it may help.

See the docs, 7.6 Adding a Bookmark to the Document

No . before .ThisComponent, and Bookmark.Start or Bookmark.End

Bookmark = ThisComponent.Bookmarks.getByName(s).Anchor
ViewCursor.gotorange(Bookmark.Start, False)

Thanks for the replies. I edited according to your suggestions and now have
SUB jump2Bookmark End
ViewCursor = ThisComponent.CurrentController.getviewCursor()
Bookmark = ThisComponent.Bookmarks.getByName(s).Anchor
ViewCursor.gotorange(Bookmark.Start, False)
However it does not like “jump2Bookmark End” which results in the error “Property or method not found End”.
Writing “End” with quotes the error is "Unexpected symbol: End.
Not using a the makro but the form navigator to go to bookmark “End” works fine. However, I want others to use the form only without navigator and therefore need a macro.

Found a working solution. Note, in the macro the bookmark is called “Text”. Also when you set the bookmark in writer you have to set not only the cursor where the bookmark should be but you have the mark the whole word where to jump to.

Sub jump2bookmark
Dim Doc As Object
Dim Bookmark As Object
Dim Cursor As Object
Doc = ThisComponent
Bookmark = Doc.Bookmarks.getByName(“Text”)
Cursor = Doc.Text.createTextCursorByRange(Bookmark.Anchor)
Cursor.String = “Text”
End Sub

Edit: Sometimes works, sometimes not. Have not found the reason why! But the macro modifies the Text of the bookmark to TextTextTextText.

SUB jump2Bookmark
ViewCursor = ThisComponent.CurrentController.getviewCursor()
Bookmark = ThisComponent.Bookmarks.getByName("End").Anchor 'here is the name of your Bookmark
ViewCursor.gotorange(Bookmark.Start, False)

The questions you ask are the questions from the man that doesn’t know a basic programming. Study the manuals for macros Macro Guides | LibreOffice Documentation - LibreOffice User Guides

First - your Macro works like a charm every time I click the button. Thank you so much!
Second - you are quite right, I do not know about basic programming and I do not find a way to learn it. My field is artificial limbs and I try to put a business-logic into forms for somebody else to program a database application in the future - maybe. This is for colleagues in countries without many regulations and who have no business experience.
With your macro I can now design large forms, jump to the right place to demonstrate how a real business works.

I’m always skeptical when I read the macros via Ctrl+CV without bigger understanding of the macro code are put to bigger project with expectation that will be functional without chaos, but it seems it could be “functional” if people will not devise the stupid operations like “I deliberately do something wrong with it, and I will spy how it will behave” - that is probably typical behavior for many Czech people :-); and next phase is ridicule “Hahahááá, it failed :-)))!!”; and next is inner reinforcement “Haháá, the authors are cretins and I’m much better that I spoiled it (and absolutely never mind that I never made something functional, simply I’m much better!!)” :slight_smile:.

I think the Macro Book from A. Pitonyak (previous link) is really good source to start the learning of programming with macros in LibreOffice. But of course, it tooks a lot of time for beginners.
Here is small explanation you didn’t know, I hope for good start :-).

Sub or Function could have parameters and mostly have parameters → Sub macro1(paramater1, parameter2, parameter3 ...)

So if you create the Sub with parameters, you need call it with parameters. Exception is if you define the parameters as optionalSub macro2(optional sParam as string) → it that case you can run only macro2 withou parameter sParam.

If you assing the macro to the button, then it isn’t possible to put parameters to this macro. It is possible only to catch the button event as parameter, but it will be described later.
So for example you have Button1 and you assign the macro macro1 to this button:

Sub macro1 'macro that will run after click on button
	dim sName$
	sName="End" 'the name of bookmark
End Sub

And you will have also Button2. And the question is, if you will assign the same macro or if you will create macro2.
Because if you will assign the same macro, then variable sName will stay set on End.
So the 1st solution can be: make 2 macros, assign ones to 2 buttons, and make 3rd Sub that is called from button-macros but with different parameter.

Sub macroButton1 'macro for Button1
End Sub

Sub macroButton2 'macro for Button2
End Sub

Sub jumpToBookmark(sName$) 'it jumps to the bookmark that's name is in parameter sName
	dim oDoc as object, oBookmark as object
End Sub

Or it is possible to assign one macro for more buttons, and use the button event that is given automatically as single parameter to macro for clicked button → and then call execute function for example according to names of buttons (but it supposes the unique name for every button).
Both examples are in ODT, 1st for upper buttons, 2nd for bottom buttons.
macros-for-buttons.odt (44.6 kB)

1 Like

Thanks to your efforts I was able to create a macro to open another document. This is all I need for now.
I see myself as a member of a future team. Each member has a specialty but also knows something of the others tasks. In my case - I stay clear of programming but do my work in a way to make it easy for programmers. My demands should be realistic.
Am from Passau (not far from Czech Republic and familiar with your country men. But I live for now in Beijing and are fighting with the language (not only the basic-language) :wink:.