Page 1 of 1

[Solved] How to open a SaveAs dialog box in calc macro

Posted: Mon Jul 21, 2008 10:32 am
by icarey
I am converting an Excel Macro to Calc and I would like to know how to open a SaveAs dialog box so the user can enter a file name and file location.
As I have just started using Calc. I am not sure of the exact requirements for writing Macros. I have installed the latest SDK, I am also looking for more information on what the methods and properties are. The help in Calc is very limited compared with Excel
All the Excel code is below.
Rem Attribute VBA_ModuleType=VBAModule
Option VBASupport 1
Option Explicit
Sub saveAsText()

Dim strFilename As String
Dim fName
Dim rngRow As Range
Dim fh
Dim strLine As String
Dim rngCell As Range

fName = Application.GetSaveAsFilename

If fName = False Then Exit Sub

fh = FreeFile
Open fName For Output As fh

For Each rngRow In ActiveSheet.UsedRange.Rows
strLine = ""
For Each rngCell In Intersect(rngRow, ActiveSheet.UsedRange)
If strLine <> "" Then
strLine = strLine & ","
End If
strLine = strLine & """" & CStr(rngCell.Value) & """"
Next rngCell
Print #fh, strLine
Next rngRow
Close #fh
End Sub

Thanks,
Ivan

Re: How to open a SaveAs dialog box in calc macro

Posted: Mon Jul 21, 2008 4:04 pm
by Villeroy
Intanciate a FilePicker service

Code: Select all

oFP=createUnoService("com.sun.star.ui.dialogs.FilePicker")
OOo makes use of 11 predefined file picker templates. You set the dialog's template by calling a intitialization routine with an array of one of the template constants 0 to 10.

Code: Select all

oFP.initialize(Array(com.sun.star.ui.dialogs.TemplateDescription.FILEOPEN_SIMPLE))
Templates 1, 2, 3, 4, 5, 6 and 10 are for saving files (constants named c.s.s.ui.dialogs.TemplateDescription.FILESAVE_...), the others are templates for file-open dialogs (constants named c.s.s.ui.dialogs.TemplateDescription.FILEOPEN_...).
Testing all the templates:

Code: Select all

Sub test_FilePickerTemplates()
for i = 0 to 10
  oFP=createUnoService("com.sun.star.ui.dialogs.FilePicker")
    oFP.initialize(Array(i))
    iExec=oFP.execute()
    REM if not canceled ...
    if iExec = 1 then
      aURLs=oFP.getFiles()
      msgbox aURLs(0)
    endif   
next
End Sub
Method execute() returns one of dialog results for OK or Cancel (1 or 0).
Method getFiles() returns an array of file-URLs even if multiple selection is disabled.
http://api.openoffice.org/docs/common/r ... le-ix.html