Difference between revisions of "Documentation/DevGuide/Basic/File Control"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
m
Line 12: Line 12:
 
<!--[BUG641+]-->
 
<!--[BUG641+]-->
 
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 <code>ConvertToURL()</code> to convert system paths to URLs.
 
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 <code>ConvertToURL()</code> to convert system paths to URLs.
 
+
<source lang="oobas">
 
   oFileControl = oDialog.Model.FileControl1
 
   oFileControl = oDialog.Model.FileControl1
 
   oFileControl.Text = "D:\Programme\Office60"
 
   oFileControl.Text = "D:\Programme\Office60"
 
+
</source>
 
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 <idl>com.sun.star.ui.dialogs.FilePicker</idl> at runtime. An example is provided below.  
 
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 <idl>com.sun.star.ui.dialogs.FilePicker</idl> at runtime. An example is provided below.  
 
<!--[SOURCE:BasicAndDialogs/ToolkitControls/FileDialog.xba]-->
 
<!--[SOURCE:BasicAndDialogs/ToolkitControls/FileDialog.xba]-->
 
+
<source lang="oobas">
 
   Sub OpenFileDialog()
 
   Sub OpenFileDialog()
 
       Dim oFilePicker As Object, oSimpleFileAccess As Object
 
       Dim oFilePicker As Object, oSimpleFileAccess As Object
Line 56: Line 56:
 
       End If
 
       End If
 
   End Sub
 
   End Sub
 
+
</source>
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Basic and Dialogs]]
 
[[Category:Documentation/Developer's Guide/Basic and Dialogs]]

Revision as of 10:10, 22 October 2009



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.

  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
          oSettings = CreateUnoService( "com.sun.star.frame.Settings" )
          oPathSettings = oSettings.getByName( "PathSettings" )
          sFileURL = oPathSettings.getPropertyValue( "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