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

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

问题描述

我希望进行一次健全性检查.我正在为Mac改编Word加载项(在VBA中为Word 2010编写),具体来说,是Word2011.我知道许多差异,但是我找不到.关于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脚本即可返回其他文件类型并选择目录,我将留给您.

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天全站免登陆