File Control

From Apache OpenOffice Wiki
Jump to: navigation, search



The file control com.sun.star.awt.UnoControlFileControl has all the properties of a text field control, with the additional feature of a built-in command button. When the button is clicked, the file dialog shows up. The directory that the file dialog initially displays is set by the Text property.

The directory must be given as a system path, file URLs do not work at the moment. In Basic you can use the runtime function ConvertToURL() to convert system paths to URLs.

  oFileControl = oDialog.Model.FileControl1
  oFileControl.Text = "D:\Programme\Office60"

Filters for the file dialog can not be set or appended for the file control. An alternative way is to use a text field and a command button instead of a file control and assign a macro to the button which instantiates the file dialog com.sun.star.ui.dialogs.FilePicker at runtime. An example is provided below.

Once the dialog is open, the user may write a directory address in the text field. Then he/she clicks the button. The file dialog will display the file names in that directory, or by default in the work directory. The selected file name is stored in the text field.

  Sub OpenFileDialog()
      Dim oFilePicker As Object, oSimpleFileAccess As Object
      Dim oSettings As Object, oPathSettings As Object
      Dim oTextField As Object, oTextFieldModel As Object
      Dim sFileURL As String
      Dim sFiles As Variant
      REM file dialog
      oFilePicker = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
      REM set filter
      oFilePicker.AppendFilter( "All files (*.*)", "*.*" )
      oFilePicker.AppendFilter( "StarOffice 6.0 Text Text Document", "*.sxw" )
      oFilePicker.AppendFilter( "StarOffice 6.0 Spreadsheet", "*.sxc" )
      oFilePicker.SetCurrentFilter( "All files (*.*)" )
      REM if no file URL is set, get path settings from configuration
      oTextFieldModel = oDialog.Model.TextField1
      sFileURL = ConvertToURL( oTextFieldModel.Text )
      If sFileURL = "" Then
          oPathSettings = CreateUnoService( "com.sun.star.util.PathSettings" )
          sFileURL = oPathSettings.Work
      End If
      REM set display directory
      oSimpleFileAccess = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
      If oSimpleFileAccess.exists( sFileURL ) And oSimpleFileAccess.isFolder( sFileURL ) Then
          oFilePicker.setDisplayDirectory( sFileURL )
      End If
      REM execute file dialog
      If oFilePicker.execute() Then
          sFiles = oFilePicker.getFiles()
          sFileURL = sFiles(0)
          If oSimpleFileAccess.exists( sFileURL ) Then
              REM set file path in text field
              oTextField = oDialog.GetControl("TextField1")
              oTextField.SetText( ConvertFromURL( sFileURL ) )
          End If
      End If
  End Sub
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages