In this discussion the problem arises in the context of calc, but there is another option that can certainly be used also in writer, namely to execute a user macro by customizing the toolbar and / or the contextual menu.
The context menu as well as being able to insert new entries in the root has the additional advantage of being able to create a hierarchy of submenus, each of which can be associated with the call of a user macro.
Furthermore, being the contextual menus different according to the context in which they are open (text, table, print preview, toolbar, etc.), the possibilities to customize each menu with different user functions is remarkable.
Customizing the context menu in writer recently solved a problem on which I had opened a discussion that remained unanswered, namely the management of mouse events in writer.
The initial purpose was to run different user macros according to the position of the mouse in the document, the context, the function to be performed
Apparently neither from the toolbar nor from the context menu it is possible to execute macros by passing parameters, or, at least, the event object.
However, the most important parameters, for example the mouse coordinates and the area of the current document, it is not possible to execute on call neither parameters nor the event object without causing this scripting error.
vnd.sun.star.script: Standard.ggWriter.wrtDocEv? language = Basic & location = application.
Message: The following Basic script could not be found:
library: ‘Standard’
module: ‘ggWriter’
method: ‘wrtDocEv’
location: ‘application’
But if you access the viewCursor in the macro called, you will reconstruct both the coordinates of the point where the contextual was called with a right click and, consequently, the portion of the surrounding text (sections, tables, text, etc.)
This simple user macro, for example, selects the paragraph in which the contextual menu has been activated
sub wrtDocEv ()
dim a, oD, oVC
oD=ThisComponent
oVC = oD.currentController.viewCursor
oD.currentController.select (oVC.textParagraph)
end sub
an interesting use can be to use user macros in a submenu of the contextual menu to
-
initialize the contents of an array or a collection declared as global
-
add an item (e.g. section, table, paragraph) associated with the current view cursor in array / collection
-
process the content of the elements saved
A second use is to search additional info on the current selection (e.g. the meaning, a translation, etc.) possibly using an input box in the macro to further specify the function to be performed
But the applications can be many, facilitated by the immediacy of the use of the contextual menu and by the possibility to customize it
Thanks to @lupp for having reopened this discussion with related contributions, very interesting and thanks to those who opened it and participated in it
Edit: mini-tutorial more accurate
In the following we will call PX the point in the document where the context menu was opened
- Use a wrtEv user function that initially contains the statement
oVC = thisComponent.currentController.viewCursor
oVC is the reference to the viewCursor
- Enter wrtEv both in the contextual menu (hereinafter mcText) associated with the “text” (the contextual that opens with a right click on a text segment at one point) and in the contextual menu (hereinafter mcTable) associated with the “table” , the specific menu contextual with right click inside a table
This done, you can now access everything a viewCursor has access to:
-
with oVC. textParagraph directly accesses the paragraph containing the PX point
-
with oVC.location the X and Y coordinates of PX are obtained
-
the section (if any) containing PX is located in oVC.textSection
-
the table and the cell containing PX (if existing) are in oVC.textTable and oVC.cell
There is therefore no need for a keystroke to call up the macro, it is activated directly from the context menu which, if desired, can contain submenus with hierarchies of user macros, each of which can perform different functions in relation to the position of PX and the relative text Content