Word 2011 VBA 中的文件对话框

3
我希望进行一些检查以确保正确性。 我正在为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
我对Mac上的VBA不是很了解,但也许你可以看一下以下这篇帖子,其中提供了Word 2010中msoFileDialogFilePicker的确切构造方式。 - Kazimierz Jawor
谢谢,这就是我的插件目前的编写方式。FileDialog在Word 2011中引发了一个“用户定义类型”错误。所以,要么它被称为其他东西,要么这些对话框不能通过内容模型访问。 - Christina
1个回答

4
我相信你需要使用Apple Script来更好地在Mac上完成这个任务。以下代码允许用户选择文本文件,并将其作为数组从函数返回。你只需能够修改Apple Script以返回其他文件类型并选择目录,我将把这留给你。
调用该函数并显示带有所有文件的消息框的代码:
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.TEXTcom.microsoft.word.doc,但这不允许包含 .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


很高兴能够帮忙。在Mac上使用VBA的额外乐趣! :) - CuberChase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接