我相信你必須使用Apple Script才能在Mac上做到這一點。以下代碼允許用戶選擇從函數返回的作爲數組的文本文件。您只需修改Apple腳本即可返回其他文件類型並選擇目錄,我會將其留給您。
調用該函數,並顯示所有文件一個消息框代碼:
Sub GetTextFilesOnMac()
Dim vFileName As Variant
'Call the function to return the files
vFileName = Select_File_Or_Files_Mac
'If it's empty then the user cancelled
If IsEmpty(vFileName) Then Exit Sub
'Loop through all the files specified
For ii = LBound(vFileName) To UBound(vFileName)
MsgBox vFileName(ii)
Next ii
End Sub
而且,做蘋果腳本工作中的作用:
Function Select_File_Or_Files_Mac() As Variant
'Uses AppleScript to select files on a Mac
Dim MyPath As String, MyScript As String, MyFiles As String, MySplit As Variant
'Get the documents folder as a default
On Error Resume Next
MyPath = MacScript("return (path to documents folder) as String")
'Set up the Apple Script to look for text files
MyScript = "set applescript's text item delimiters to "","" " & vbNewLine & _
"set theFiles to (choose file of type " & " {""public.TEXT""} " & _
"with prompt ""Please select a file or files"" default location alias """ & _
MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
"set applescript's text item delimiters to """" " & vbNewLine & _
"return theFiles"
'Run the Apple Script
MyFiles = MacScript(MyScript)
On Error GoTo 0
'If there are multiple files, split it into an array and return the results
If MyFiles <> "" Then
MySplit = Split(MyFiles, ",")
Select_File_Or_Files_Mac = MySplit
End If
End Function
最後,它可以是一個如果您只想指定Word文檔,則用com.microsoft.word.doc
替換public.TEXT
,但這將不允許.docx
或.docm
文件。您需要分別使用org.openxmlformats.wordprocessingml.document
和org.openxmlformats.wordprocessingml.document.macroenabled
。有關這些更多信息,請參閱:https://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html
我不太瞭解VBA for Mac,但也許你可以看看下面的[post](http://stackoverflow.com/questions/15518682/to-add -header-and-footer-for-many-word-documents/15520412#15520412)爲Word 2010提供'msoFileDialogFilePicker'的確切結構 – 2013-03-20 22:07:34
謝謝,這就是我目前編寫加載項的方式。 FileDialog在Word 2011中引發了「用戶定義類型」錯誤。因此,它被稱爲別的東西,或者這些對話框無法通過內容模型訪問。 – Christina 2013-03-21 05:01:29