2013-03-20 36 views
3

我希望有一點理智的檢查。我正在爲Mac改寫一個Word插件(用VBA編寫的Word 2010),特別是在Word 2011中。我意識到很多差異,但我一直無法找到很多文檔都明顯缺乏FileDialog。最接近我的答案是:http://www.rondebruin.nl/mac.htm作者使用Application.GetOpenFilename。這種方法似乎並不適用於Word,但(該網站的重點是Excel)。Word 2011中的FileDialog VBA

有誰知道如何使用FileDialog提供的文件和文件夾選取器對話框?我對Applescript並不熟悉,但爲了解決Word 2011中時髦的文件管理問題(Dir,FileCopy等),我不得不學習一點。所以,如果這就是答案,那麼對Applescript中的代碼看起來會有什麼樣的感覺將不勝感激。 (我或多或少知道如何將其翻譯成VBA)。

+1

我不太瞭解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

+0

謝謝,這就是我目前編寫加載項的方式。 FileDialog在Word 2011中引發了「用戶定義類型」錯誤。因此,它被稱爲別的東西,或者這些對話框無法通過內容模型訪問。 – Christina 2013-03-21 05:01:29

回答

4

我相信你必須使用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.documentorg.openxmlformats.wordprocessingml.document.macroenabled。有關這些更多信息,請參閱:https://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html

+0

這很好,謝謝!正是我需要的。 – Christina 2013-03-21 16:49:49

+0

很高興幫助。 Mac上VBA的額外樂趣! :) – CuberChase 2013-03-21 21:04:35