Word 2011 VBA 中的 FileDialog [英] FileDialog in Word 2011 VBA

查看:40
本文介绍了Word 2011 VBA 中的 FileDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm hoping for a bit of a sanity check. I'm adapting a Word add-in (written in VBA for Word 2010) for Mac, specifically, at this point, Word 2011. I'm aware of many of the differences, but one that I haven't been able to find much documentation on is the apparent lack of FileDialog. The closest I've come to an answer is here: http://www.rondebruin.nl/mac.htm where the author uses Application.GetOpenFilename. That method doesn't seem to exist for Word, though (the focus of that site is Excel).

有谁知道如何使用 FileDialog 提供的文件和文件夹选择器对话框?我真的不熟悉 Applescript,但我必须学习一点才能解决 Word 2011 的时髦文件管理问题(Dir、FileCopy 等).因此,如果这就是答案,那么我们将不胜感激任何有关 Applescript 中代码外观的信息.(我或多或少知道如何将其翻译成 VBA).

Does anyone know how to use the file and folder picker dialogs that FileDialog makes available? I'm not familiar with Applescript, really, but I've had to learn a little in order to get around Word 2011's funky file management issues (Dir, FileCopy, etc.). So, if that's the answer, any sense of what the code might look like in Applescript would be greatly appreciated. (I more or less know how to translate that into VBA).

推荐答案

我相信您必须使用 Apple Script 才能在 Mac 上做得更好.以下代码允许用户选择从函数中作为数组返回的文本文件.您只需修改 Apple Script 即可返回其他文件类型并选择目录,我将把它留给您.

I believe you have to use Apple Script in order to do this a bit better on the Mac. The following code allows the user to select text files which is returned as an array from the function. You would simply be able to modify the Apple Script to return other file types and select directories, I'll leave that to you.

调用函数并显示包含所有文件的消息框的代码:

The code that calls the function and displays a message box with all the files:

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 的函数:

And the function that does the Apple Script work:

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

Finally, it can be a bit of a pain specifying different file types, if you want to specify only Word documents, then replace public.TEXT with com.microsoft.word.doc, however this won't allow .docx or .docm files. You need to use org.openxmlformats.wordprocessingml.document and org.openxmlformats.wordprocessingml.document.macroenabled respectively for these. For more info on these see: https://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html

这篇关于Word 2011 VBA 中的 FileDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆