# LibreOffice Calc File selection Path

Hi, I need to be able to insert the path of the file selected by the button inside a cell. how do I link a file selection button to a cell?

edit retag close merge delete

I edited the title, changed "Libre-Cacl" into "LibreOffice Calc".

( 2018-02-17 16:02:21 +0200 )edit

Sort by » oldest newest most voted

Hello @peppinosh,

To physically link a File Selection button to a Calc cell, you could just right-click on the File Selection button ( in Design Mode ), and then choose the menuitem Anchor : To Cell.

Alternatively, you could set this same option via the Properties Dialog of your File Selection button.

If you then position the File Selection button into the anchor cell, it will physically move along with the cell when a new row or column is inserted, and it will grow or shrink along with the cell, when the cell is resized.

However it not a data link, and the chosen File Path from the File Selection button will not be automatically transferred into the cell.

In order to automatically transfer the chosen File Path into the anchor cell, you could connect the following basic macro to the “Text modified” event of your File Selection button:

code:

Sub on_FileSelection_TextModified( oEvent )
REM Connect this macro to the "Text modified" event of a FileSelection control that is anchored to a Spreadsheet Cell.
REM The Selected File Path should then automatically be inserted into the Anchor Cell.
Dim oControlModel As Object : oControlModel = oEvent.Source.getModel()
Dim oDrawPages As Object    : oDrawPages    = ThisComponent.getDrawPages()      REM Assumes Calc document.
Dim oDrawPage As Object
Dim oShape As Object
Dim i As Long, j As Long
For i = 0 To oDrawPages.getCount() - 1      REM Find ControlShape to get the Anchor:
oDrawPage = oDrawPages.getByIndex( i )
For j = 0 To oDrawPage.getCount() - 1
oShape = oDrawPage.getByIndex( j )
If oShape.getShapeType() = "com.sun.star.drawing.ControlShape" Then
Dim oControl As Object : oControl = oShape.getControl()
If oControlModel.getName() = oControl.getName() Then        REM Found our FileSelection control.
Dim oAnchor As Object : oAnchor = oShape.getAnchor()
oAnchor.setString( oControl.Text )
Stop
End If
End If
Next j
Next i
End Sub


With Regards, lib

To mark this answer as correct, please click on the round checkmark icon on the left, and karma-permitting upvote it.

more