2015-09-05 27 views
1

在LibreOffice的4.2我試圖打開文件選擇器中選擇多個文件(我成功了),然後對這些文件的名稱(和路徑)轉讓給一個變量(或數組,無所謂)。的LibreOffice如何讓filepicker多個文件選擇數據

雖然我可以打開文件選擇器中選擇多個文件,我可以得到只有一個文件(第一個)的文件名和路徑。而我找不到其他方法。

我使用下面的代碼:

Sub TakeFile() 
    Dim FileNames(0 to 100) as String 
    FileNames() = fImportLocalFile() 
    Msgbox FileNames 
End Sub 

Function fImportLocalFile() 'as String 
' FJCC: Can't define the function as returning a String because now it returns an array 
    'this function opens a system file open dialog box and allows the 
    ' user to pick a file from thier computer to open into the 
    ' document for processing 

    'stores the filedialog object 
    Dim oFileDialog as Object 
    'stores the returned result of the activation of the dialog box 
    Dim iAccept as Integer 
    'stores the returned file name/path from the file dialog box 
    Dim sPath as String 
    'stores the set default path for the dialog box 
    Dim InitPath as String 

    'stores the types of files allowed in the filedialog 
    Dim sFilterNames as String 

    'setup the filters for the types of files to allow in the dialog 
    sFilterNames = "*.csv; *.txt; *.odt; *.ods; *.xls; *.xlt; *.xlsx" 

    'create the dialog box as a Windows File Dialog 
    oFileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker") 

    'set the filters for the dialog 
    oFileDialog.AppendFilter("Supported files", sFilterNames) 

    'set the path as blank 
    InitPath = "" 

    'add the default path to the dialog 
    oFileDialog.setDisplayDirectory(InitPath) 

    'setup the dialog to allow multiple files to be selected 
    oFileDialog.setMultiSelectionMode(True) 

    'set iAccept as the execution of the dialog 
    iAccept = oFileDialog.Execute() 

    'execute and test if dialog works 
    If iAccept = 1 Then 
     'set sPath as the chosen file from the dialog 
     'sPath = oFileDialog.Files(0) 
     FileArray = oFileDialog.getFiles() 'added by FJCC 
     'set the function as sPath for returning to the previous sub 
     fImportLocalFile = FileArray 'modified by FJCC 
    'end current if statement 
    End If 

End Function 

回答

0

你的錯誤,你正在分配所選文件的數組到funtion名稱本身!選擇一個不同的名字。

這適用於我的LO 5.0.0.5

SUB TakeFile() 
' Dim FileNames(0 to 100) as String 
' Dont limit yourself! 

    FileNames = fImportLocalFile() 

    path = FileNames(0) 
    FOR i = 1 TO Ubound(FileNames) 
     print path + FileNames(i) 
    Next 

End Sub 

,並在函數中:

path = FileArray(0) 
FOR i = 1 TO Ubound(FileArray) 
    print path + FileArray(i) 
Next 

fImportLocalFile = FileArray 
0

有一個接口XFilePicker2其中「擴展文件選擇器界面要解決一些設計上的問題。」該界面有一個方法getSelectedFiles

https://www.openoffice.org/api/docs/common/ref/com/sun/star/ui/dialogs/XFilePicker2.html

使用這種方法,而不是XFilePicker.getFiles

下面應該工作:

Sub TakeFile() 

    Dim FileNames() as String 
    FileNames = fImportLocalFile() 

    Msgbox Join(FileNames, Chr(10)) 

End Sub 

Function fImportLocalFile() as Variant 

    Dim oFileDialog as Object 
    Dim iAccept as Integer 
    Dim sPath as String 
    Dim InitPath as String 

    Dim sFilterNames as String 

    sFilterNames = "*.csv; *.txt; *.odt; *.ods; *.xls; *.xlt; *.xlsx" 

    oFileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker") 

    oFileDialog.AppendFilter("Supported files", sFilterNames) 

    InitPath = "" 

    oFileDialog.setDisplayDirectory(InitPath) 

    oFileDialog.setMultiSelectionMode(True) 

    iAccept = oFileDialog.Execute() 

    If iAccept = 1 Then 
     fImportLocalFile = oFileDialog.getSelectedFiles() 
    Else 
     fImportLocalFile = Array() 
    End If 

End Function 
相關問題