Passing argument to a macro in Base

I have a standard button on a Base form which I want to use to open a file. The file path comes from a textbox on the form. When the button is clicked, I want to call

openFile(myTextBox)

which is a generic sub I created that takes a textbox as its parameter and opens the file given in that text box. However, when I assign the openFile sub to the “Mouse button pressed” event for the button, I can’t give it any parameters. I looked at this topic, which has a similar discussion for Calc, and the solution there suggests passing the parameter to a function and calling that function from within the sub. However, Base’s “assign macro” feature doesn’t seem to allow passing arguments to functions, either.

Is there a way to do such a thing in Base?

How to pass a parameter to a Sub when calling it by an onClick event from a button should not much depend on the type of the document containing the button. (The event should be described as Execute action.)
The appropriate property any FormControl has is .Tag. Gravely misleading this property is described as Additional information in the (English) UI.
To identify the TextBox you should use the name of the control. There also is an (often unused) name of the shape hosting it. Trying to use thos name to get the control would complicate things.
The code below should show the relevant facts.

Sub onButtonClick(pEvent)
buttonControl = pEvent.Source.Model
buttonTag = buttonControl.Tag
form = pEvent.Source.Model.Parent
textBox = form.getByName(buttonTag)
REM Button and TextBox are assumed to be elemenmts of the same Form!
myUrl = textBox.CurrentValue
myDoc = StarDesktop.loadComponentFromURL(myUrl, "_blank", 0, Array())
End Sub
1 Like

Without macro
Action: Open Document or URL
URL: file:///path/document.xyz

This also works with user interface dispatches. URL .uno:CloseDoc closes the form document.

Hello @Chell.

Please see:

Can be used for opening files as well as URL’s.