【问题标题】:FileDialog in Word 2011 VBAWord 2011 VBA中的文件对话框
【发布时间】:2020-07-07 05:35:10
【问题描述】:

我希望能进行一次健全性检查。我正在为 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)。

【问题讨论】:

  • 我对 Mac 的 VBA 了解不多,但也许你可以看看下面的 post,它为 Word 2010 提供了 msoFileDialogFilePicker 的确切结构
  • 谢谢,我的插件目前就是这样写的。 FileDialog 在 Word 2011 中引发“用户定义类型”错误。因此,要么它被称为其他名称,要么这些对话框无法通过内容模型访问。

标签: vba macos ms-word applescript


【解决方案1】:

我相信您必须使用 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,但是这不允许 .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 的额外乐趣! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
相关资源
最近更新 更多