| View previous topic :: View next topic |
| Author |
Message |
shoe Newbie

Joined: 29 Oct 2003 Posts: 1
|
Posted: Wed Oct 29, 2003 11:26 am Post subject: open file dialog in SB |
|
|
I've looked all over the place, (this site, google, StarOffice 6 Basic guide) and I can't seem to find an easy way to create an open file dialog. Can anybody point me in the right direction? TIA
FWIW I'm writing a macro to populate a spreadsheet via a text file and would like the user to be able to browse the disk to find the text file to open. |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 4062 Location: Lawrence, Kansas, USA
|
Posted: Wed Oct 29, 2003 12:55 pm Post subject: |
|
|
| Code: | ' Create a FilePicker dialog.
oFileDialog = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
' Execute it.
oFileDialog.execute()
' Get an array of the files that the user picked.
' There will only be one file in this array, because we did
' not enable the multi-selection feature.
oFilesList = oFileDialog().getFiles()
' Display the zero'th element of the array.
MsgBox( "The file you picked was " + oFilesList(0) )
|
This creates a file dialog. Executes it. After you dismiss the dialog, it comes back with a list of files you picked. (Since we didn't enable the multi-selection feature of the dialog, you can only pick one file, and so the array will only have one file in it as the zero'th element.) Then display a MsgBox showing the file you picked.
Note that the file is in the form of a URL. If you would like a conventional pathname, then use the ConvertFromURL() function to get a pathname from the URL.
If you would like to enable the multi-selection mode of the dialog, then do this, prior to executing the dialog...
oFileDialog.setMultiSelectionMode( True )
If you would like a default filename displayed in the filename part of the picker dialog, then do this, prior to executing the dialog...
oFileDialog.setDefaultName( "Foobar.txt" )
If you would like the file dialog initially positioned in some directory, then do this....
oFileDialog.setDisplayDirectory( "file:///c:/some/directory" )
Note that the directory MUST be in URL format. You can use the ConvertToURL() function in Basic to obtain a URL from an ordinary pathname.
AFTER executing the dialog, you can obtain the URL of the directory where the file picker was...
cDirectoryURL = oFileDialog.getDisplayDirectory()
How do you learn how to use the FilePicker service? It's documented in the API here...
http://api.openoffice.org/docs/common/ref/com/sun/star/ui/dialogs/FilePicker.html
As you can see, it implements several interfaces. (Interface names start with an X.) All the stuff I just described was from the XFilePicker interface of the object.
http://api.openoffice.org/docs/common/ref/com/sun/star/ui/dialogs/XFilePicker.html
But there are other interfaces to this object which you should explore.
I suppose I should also mention this. Since we're using the OOo API to display this dialog, the file picker dialog appears where OOo is running, not where your program is running. Now in OOo Basic, these are one and the same place. But for instance, if my program (using the exact same services and interfaces as above) were written in Java running on Windows, and talking to an OOo running on a Linux computer, then the Java program would cause the file picker to appear on the computer where OOo is running, not where the Java program is running. Understand the difference?
I hope this helps. _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
WatchyPerry Guest
|
Posted: Wed May 26, 2004 3:56 pm Post subject: Open file Dialog in SB |
|
|
I have a Macro where I open a file with the filepicker dialog posted:
http://www.oooforum.org/forum/viewtopic.php?t=3582&start=0&postdays=0&postorder=asc&highlight=
Everything works great as long as I have oFileDialog.setMultiSelectionModeset() as false. ... when I set it to true I can get the values but...... I don't know how to loop through the array to find out how many files have been selected so that I can open them with another sub.
Each time I use the dialog I select a different number of files (csv for calc) that I need to open, copy and then paste to my main document.
any tips to loop through the array to find its size? |
|
| Back to top |
|
 |
dfrench Moderator

Joined: 03 Mar 2003 Posts: 1609 Location: Wellington, New Zealand
|
Posted: Wed May 26, 2004 4:45 pm Post subject: |
|
|
I have added a loop over the multipleselection.
Note If Multiselection is enabled: If only one file is selected, the first entry of the sequence contains the complete path/filename in URL format. If multiple files are selected, the first entry of the sequence contains the path in URL format, and the other entries contains the names of the selected files without path information.
| Code: | Sub Main
' Create a FilePicker dialog.
oFileDialog = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
oFileDialog.setMultiSelectionMode( true)
oFileDialog().getFiles()
' Execute it.
oFileDialog.execute()
oFilesList = oFileDialog().getFiles()
' deal with each element returned
for i = lbound( oFilesList) to ubound( oFilesList)
MsgBox( "The file you picked was " + oFilesList(i) )
next
End Sub |
|
|
| Back to top |
|
 |
r_vinoya Super User


Joined: 03 Dec 2003 Posts: 621 Location: Somewhere in the Philippines
|
Posted: Tue Nov 02, 2004 11:53 pm Post subject: |
|
|
Hello,
I tried using FilePicker dialog and it is working fine.
However when I select cancel button... the program returns error:
| Code: | Inadmissible value or data type
Index out of defined range |
How can I set the option when the user did not select any file (cancel button)? _________________ # : - ) |
|
| Back to top |
|
 |
dfrench Moderator

Joined: 03 Mar 2003 Posts: 1609 Location: Wellington, New Zealand
|
Posted: Wed Nov 03, 2004 2:16 am Post subject: |
|
|
works fine for me as it is,
on cancel, ubound( oFilesList) is set to -1 , so you could test for that |
|
| Back to top |
|
 |
r_vinoya Super User


Joined: 03 Dec 2003 Posts: 621 Location: Somewhere in the Philippines
|
Posted: Wed Nov 03, 2004 7:35 pm Post subject: |
|
|
| dfrench wrote: | works fine for me as it is,
on cancel, ubound( oFilesList) is set to -1 , so you could test for that |
Yes. It works with ubound function.
Note: I forgot to mention that the error where found when I use the first code posted by DannyB then hit the cancel button. Your code works fine _________________ # : - ) |
|
| Back to top |
|
 |
kenquad General User

Joined: 19 Dec 2006 Posts: 8
|
Posted: Mon Dec 22, 2008 10:34 am Post subject: |
|
|
[quote="DannyB"][code]
' Create a FilePicker dialog.
oFileDialog = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
' Execute it.
oFileDialog.execute()
' Get an array of the files that the user picked.
' There will only be one file in this array, because we did
' not enable the multi-selection feature.
oFilesList = oFileDialog().getFiles()
' Display the zero'th element of the array.
MsgBox( "The file you picked was " + oFilesList(0) )
[/code]
[/quote]
This is a very clear, concise example - I used it with success in my first OOo macro. Thanks! |
|
| Back to top |
|
 |
JaapAap Newbie

Joined: 20 Jan 2009 Posts: 1
|
Posted: Tue Jan 20, 2009 9:25 pm Post subject: Works for me, BUT |
|
|
Works for me, but only when I set OOO to use Use OpenOffice.org Dialogs:
Tools - Options - OpenOffice.org - General: Use OpenOffice.org Dialogs.
The OS native dialogs crash (Windows) |
|
| Back to top |
|
 |
MrtreashHouse Newbie

Joined: 29 Oct 2009 Posts: 1
|
Posted: Thu Oct 29, 2009 5:09 am Post subject: |
|
|
| Big thanks for useful info! |
|
| Back to top |
|
 |
|