Word 2011 VBA中的FileDialog

时间:2013-03-20 21:47:59

标签: applescript word-vba

我希望能进行一次健全检查。我正在为Mac修改一个Word插件(用VBA编写的Word 2010),特别是在这一点上,Word 2011.我知道很多不同之处,但我找不到一个很多文档都是明显缺乏FileDialog。我最接近答案的地方是:http://www.rondebruin.nl/mac.htm作者使用Application.GetOpenFilename。但是,Word似乎并不存在这种方法(该网站的重点是Excel)。

有谁知道如何使用FileDialog提供的文件和文件夹选择器对话框?我真的不熟悉Applescript,但我必须学习一点才能解决Word 2011的时髦文件管理问题(Dir,FileCopy等)。所以,如果这就是答案,那么对Applescript中的代码可能会有什么样的感觉将会非常感激。 (我或多或少知道如何将其转换为VBA)。

1 个答案:

答案 0 :(得分: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

Apple Script的功能是:

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文档,那么将public.TEXT替换为com.microsoft.word.doc,但这不允许{{1 }或.docx个文件。您需要分别使用.docmorg.openxmlformats.wordprocessingml.document。有关这些内容的详细信息,请参阅:https://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html