Page 1 of 1

[Solved] Creating SaveAs and Open Dialog box prompt

PostPosted: Tue Dec 07, 2010 8:55 am
by stevetronik
Greetings, thanks to this forum some of the solutions to my issues were found but except for a very simple feature that I could not done properly
I'm totally new with OOo BASIC, I had being trying to find a template that calls upon a Save As Dialog box that other BASICS I used to before have

Code: Select all   Expand viewCollapse view
Filename =  "C:\..\file location\..\Export.xml"            ' This method fixed the location where my export goes to
   FileNo = FreeFile
   Open Filename For Output As #FileNo

   Print #FileNo "<List>"
   Call printlist
   Print #FileNo "</List>"
   Close #FileNo

Above creates an Export.xml at a fixed location specified in code. It temporary allows me to continue my work but it is not the solution I need. I want to achieved a save as dialog box will prompt the user to choose a file location and file-name like normal save as dialog box with extension filter would be nice. Additionally I need to be able to do an Open file as well like normal open dialog box. Not an automated process like most solutions I found in the forum, but a prompt when it came to this part of the code.

Thanks in advance.

Re: creating SaveAs and Open Dialog box prompt

PostPosted: Tue Dec 07, 2010 4:22 pm
by rudolfo
Andrew Pitonyak has a macro based on the FilePicker dialog for this purpose.
As the name suggests it only picks the filename. It is your decision if you use the name for opening a file or for saving it.

Code: Select all   Expand viewCollapse view
'Opens a Open File Dialog to allow the end user to select a file to import into the program.
'This code is from Andrew Pitonyak's free Useful Macros book
Function fOpenFile() as String

   Dim oFileDialog as Object
   Dim iAccept as Integer
   Dim sPath as String
   Dim InitPath as String
   Dim oUcb as object
   Dim filterNames(3) as String

   filterNames(0) = "*.*"
   filterNames(1) = "*.png"
   filterNames(2) = "*.jpg"

   'Note: The following services must be called in the following order,
   ' otherwise the FileDialog Service is not removed.
   oFileDialog = CreateUnoService("")
   oUcb = createUnoService("")

   AddFiltersToDialog(FilterNames(), oFileDialog)
   'Set your initial path here!
   InitPath = ConvertToUrl("C:\Pictures")

   If oUcb.Exists(InitPath) Then
   End If

   iAccept = oFileDialog.Execute()
   If iAccept = 1 Then
      sPath = oFileDialog.Files(0)
      fOpenFile = sPath
   End If

End Function

Sub TestFilePicker()
   Dim sFilename As String
   sFilename = fOpenFile()
   MsgBox sFilename & chr(10) & ConvertFromUrl(sFilename)
End Sub

You want to use the filename with Open, hence you need the name in OS notation while the file picker macro always returns it in URL notation. But ConvertFromUrl is your friend here.

Re: Creating SaveAs and Open Dialog box prompt

PostPosted: Wed Dec 08, 2010 3:22 am
by stevetronik
Thanks rudolfo, It works I'll mark it as solved.

in case someone is looking for SaveAs dialog as many solutions points towards filepicker which is obvious in making Open dialogs below is the code I used for Save As

Code: Select all   Expand viewCollapse view
Function fSaveFile() as String

   'Set the Dialog Arguments to a Template for FILESAVE

   sFilePickerArgs = Array(_ )

   'register the Service for Filepicker
   oFilePicker = CreateUnoService( "" )

   'Pass some arguments to it

   With oFilePicker
      .Initialize( sFilePickerArgs() )
      .setDisplayDirectory( "C:/" )
      .appendFilter("XML Files (.xml)", "*.xml" )
      .setTitle( "Save As ..." )
   End With

   'If the savepath is selected return the complete path and display it in an messagebox   

   If oFilePicker.execute() Then
      sFiles = oFilePicker.getFiles()
      fSaveFile = sFiles(0)
'      MsgBox( sFileURL )
   End If

   ' Close the Dialog
End Function

Re: [Solved] Creating SaveAs and Open Dialog box prompt

PostPosted: Sat Jul 23, 2011 3:42 pm
by Bergkaffee
Thank you very much. I was looking for quite some time for something like that.