What is the syntax to create the Open File Dialog

Hello all, I’m new to programming in Basic. I’ve done a fair share in VBA, but I’m not familiar enough with the different functions to replicate what I want in LO.

I’m building a macro in Writer to resize images in a document. The current working macro in VBA uses the “Application.FileDialog(msoFileDialogFolderPicker)” object to allow the user to select the folder where the files to be worked on are stored.

Obviously, you don’t do this the same way in Basic. What is the equivalent?

In the example from the LO help:

Sub ExampleWorkWithAFile
    Dim iNumber As Integer
    Dim sLine As String
    Dim aFile As String
    Dim sMsg As String
    aFile = "C:\Users\ThisUser\data.txt"
    iNumber = Freefile
    Open aFile For Output As #iNumber
    Print #iNumber, "This is a line of text"
    Print #iNumber, "This is another line of text"
    Close #iNumber
    iNumber = Freefile
    Open aFile For Input As iNumber
    While Not eof(iNumber)
        Line Input #iNumber, sLine
        If sLine <>"" Then
            sMsg = sMsg & sLine & chr(13)
        End If
    Wend
    Close #iNumber
    MsgBox sMsg
End Sub

Where “aFile” is given by a string, I want to define this by having the user navigate to the folder.

Thanks in advance.

This works for me since many years:

Function pickFolder(sTitle$, sInit$) AS String
REM return folder URL or ""
REM dialog starts at office default directory if sInit = ""
	oPicker = CreateUnoService("com.sun.star.ui.dialogs.FolderPicker")
	oPicker.setTitle(sTitle)
	oPicker.setDisplayDirectory(sInit)
	if oPicker.execute() then
		pickFolder = oPicker.Directory
	endif
End Function

Hi,

this document might help, I guess : BASIC Dialogs

1 Like

It does. I have that, and forgot to look at it.

In VBA, I’m used to testing my code by stepping into it and seeing what happens, or if I programmed it correctly. In this case, I created:

Sub Main
	dim oFileDialog as Object
	dim filterNames(0) as string
	filternames(0) = "*.RTF"
	
	
	GlobalScope.BasicLibraries.LoadLibrary("Tools")
	oFileDialog = CreateUnoService("com.sun.star.ui.dialogs.SystemFolderPicker")
	
End Sub

But nothing happens. Shouldn’t I get a folder dialog box? I also tried “FolderPicker”, but still no response.

I don’t know what I don’t know, so I’m not sure why this isn’t working.

I tried this and got a run time error:

image

That function requires it to be called with 2 arguments, it cannot be executed without them.
You must call it from a “sub” in the form
PickFolder ("Select a folder"," "file://c:/Documents")
Select a folder will be the title of the dialog
c:/Documents will be the folder where the dialog starts

1 Like

Kudos for that. I should have seen that.

This achieved what I wanted.

I assume that I can set the default Directory to start in whatever folder I choose, correct? It appears that Directory is whatever value was last stored there, based on LO’s settings?

https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1ui_1_1dialogs_1_1XFilePicker.html#a646254bf23072a0e9253e769b0088769