[Solved] Clicking Close or Cancel on dialogs.FilePicker

Creating a macro - Writing a Script - Using the API

[Solved] Clicking Close or Cancel on dialogs.FilePicker

Postby oonk » Sat Nov 02, 2019 5:47 am

Clicking 'Open' button without selecting a file, there is no problem for the dialog remains unchanged.
But clicking 'Close' button or 'Cancel' button, there is an error.
What is the way to handle these 2 button?
Attachments
Screenshot_20191102_102203.jpg
Screenshot_20191102_102423.jpg
Last edited by oonk on Sun Nov 03, 2019 2:14 am, edited 1 time in total.
| Fedora 31 Workstation KDE of Fedora from Spins | LibreOffice 6.2.8.2-2 | Base with embedded HSQLDB |
oonk
 
Posts: 11
Joined: Fri Oct 04, 2019 8:03 am
Location: Pathum Thani, THAILAND

Re: Event of clicking Close or Cancel on dialogs.FilePicker

Postby Zizi64 » Sat Nov 02, 2019 9:03 am

Please upload a real ODF type document file (together the embedded macro code) here, instead of the pictures.
Tibor Kovacs, Hungary; LO6.1.6 on Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.3.3; AOO4.1.6
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 8534
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Event of clicking Close or Cancel on dialogs.FilePicker

Postby Villeroy » Sat Nov 02, 2019 10:39 am

Your variable s is declared as string. Declare it as variant and test which type of value is assigned to that variable.

 Edit: getFiles is deprecated. For now it returns an empty array or an array with one string element. 

https://api.libreoffice.org/docs/idl/re ... d93556a60d

P.P.S.
With LO I would get a single selected file like this
Code: Select all   Expand viewCollapse view
x = oDialog.execute()
if x = 0 then
REM Cancel
else
s = oDialog.getSelectedFiles()(0)
endif

or like this:
Code: Select all   Expand viewCollapse view
oDialog.execute()
a() = oDialog.getSelectedFiles()
if uBound(a())=-1 then
REM Cancel
elseif uBound(a())=0 then
s = a(0)
else
REM multi-selection
endif

If the code needs to be compatible with AOO, you can use the deprecated getFiles() in the exact same ways as long as LO supports this method and as long as you don't need the result of a multiple file selection. In this case AOO returns one element more than the selected files. First element is the path URL, subsequent elements are the file names without path.
Last edited by Villeroy on Sat Nov 02, 2019 12:06 pm, edited 1 time in total.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27375
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Event of clicking Close or Cancel on dialogs.FilePicker

Postby Lupp » Sat Nov 02, 2019 12:03 pm

This works for me in LibO and in AOO 4.1.5 as well:
Code: Select all   Expand viewCollapse view
Function pickedCsvUrl()
pickedCsvUrl = ""
filePicker = createUnoService("com.sun.star.ui.dialogs.FilePicker")
With filePicker
  .MultiSelectionMode = False
  .AppendFilter("CSV files (.csv)", "*.csv")
  returned = .Execute
  If returned=1 Then
    pickedUrl = .Files(0)
    splitUrl = Split(pickedUrl, ".")
    u = Ubound(splitUrl)
    extension = splitUrl(u)
    If extension="csv" Then pickedCsvUrl = url
   End If
End With
End Function

See also https://xkcd.com/2116/
On Windows 10: LibreOffice 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2564
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Event of clicking Close or Cancel on dialogs.FilePicker

Postby Villeroy » Sat Nov 02, 2019 1:50 pm

c.s.s.ui.dialogs.FilePicker supports all the options that are availlable in the complex file picker that appears when you hit Ctrl+O, i.e. extra check boxes and grouped file type filters.

This is a simplified file picker for one file with a simple pattern filter:
Code: Select all   Expand viewCollapse view
Function pickFile(sTitle$, sInit$, sFilterLabel$, sPattern$)
REM dialog starts at office default directory if sInit = ""
Dim oPicker, x()
   oPicker = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
   oPicker.setTitle(sTitle)
   oPicker.setDisplayDirectory(sInit)
   oPicker.setMultiSelectionMode(False)
   oPicker.appendFilter(sFilterLabel, sPattern)
   if oPicker.execute()<>0 then
      x = oPicker.getFiles()
      pickFile = x(0)
   endif
End Function

Takes the window title, the start path, the displayed filter name and one or more file name patterns separated by semicolon.
It is compatible with both office suites as long as LibreOffice supports getFiles(). With LO only you should use the new method getSelectedFiles().

Example call:
Code: Select all   Expand viewCollapse view
f = pickFile("TEST", "", "Blah Text", "*.txt;*.csv"

Window title: TEST
If the init path is "", the picker starts in the default document directory.
Name of file type: "Blah Text"
Displayed files: *.txt and *.csv
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 27375
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 11 guests