Excel VB 打开文件 OSX 和 Windows

2024-04-04

我有一个电子表格,它使用一些基本代码来让用户选择一个文件(txt 文件)。它在 Windows 上完美运行,但在 OSX 上显然由于 FileDialog 调用的差异而失败。我已经做了一些研究,但似乎找不到太多有关在 OSX 和 Windows 上打开 Excel/VB 文件对话框的信息。

当前的代码是,

FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to import", _
FileFilter:="Excel Files *.xls (*.xls),")
''
If FileToOpen = False Then
MsgBox "No file specified.", vbExclamation, "Duh!!!"
Exit Sub
Else
Workbooks.Open Filename:=FileToOpen
End If

答案可以在这里找到——http://msdn.microsoft.com/en-us/library/office/hh710200%28v=office.14%29.aspx http://msdn.microsoft.com/en-us/library/office/hh710200%28v=office.14%29.aspx

代码如下,

OSX

Sub Select_File_Or_Files_Mac()
    Dim MyPath As String
    Dim MyScript As String
    Dim MyFiles As String
    Dim MySplit As Variant
    Dim N As Long
    Dim Fname As String
    Dim mybook As Workbook

    On Error Resume Next
    MyPath = MacScript("return (path to documents folder) as String")
    'Or use MyPath = "Macintosh HD:Users:Ron:Desktop:TestFolder:"

    ' In the following statement, change true to false in the line "multiple 
    ' selections allowed true" if you do not want to be able to select more 
    ' than one file. Additionally, if you want to filter for multiple files, change 
    ' {""com.microsoft.Excel.xls""} to 
    ' {""com.microsoft.excel.xls"",""public.comma-separated-values-text""}
    ' if you want to filter on xls and csv files, for example.
    MyScript = _
    "set applescript's text item delimiters to "","" " & vbNewLine & _
               "set theFiles to (choose file of type " & _
             " {""com.microsoft.Excel.xls""} " & _
               "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"

    MyFiles = MacScript(MyScript)
    On Error GoTo 0

    If MyFiles <> "" Then
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With

        MySplit = Split(MyFiles, ",")
        For N = LBound(MySplit) To UBound(MySplit)

            ' Get the file name only and test to see if it is open.
            Fname = Right(MySplit(N), Len(MySplit(N)) - InStrRev(MySplit(N), Application.PathSeparator, , 1))
            If bIsBookOpen(Fname) = False Then

                Set mybook = Nothing
                On Error Resume Next
                Set mybook = Workbooks.Open(MySplit(N))
                On Error GoTo 0

                If Not mybook Is Nothing Then
                    MsgBox "You open this file : " & MySplit(N) & vbNewLine & _
                           "And after you press OK it will be closed" & vbNewLine & _
                           "without saving, replace this line with your own code."
                    mybook.Close SaveChanges:=False
                End If
            Else
                MsgBox "We skipped this file : " & MySplit(N) & " because it Is already open."
            End If
        Next N
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End If
End Sub

Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Contributed by Rob Bovey
    On Error Resume Next
    bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function

Windows

Sub Select_File_Or_Files_Windows()
    Dim SaveDriveDir As String
    Dim MyPath As String
    Dim Fname As Variant
    Dim N As Long
    Dim FnameInLoop As String
    Dim mybook As Workbook

    ' Save the current directory.
    SaveDriveDir = CurDir

    ' Set the path to the folder that you want to open.
    MyPath = Application.DefaultFilePath

    ' You can also use a fixed path.
    'MyPath = "C:\Users\Ron de Bruin\Test"

    ' Change drive/directory to MyPath.
    ChDrive MyPath
    ChDir MyPath

    ' Open GetOpenFilename with the file filters.
    Fname = Application.GetOpenFilename( _
            FileFilter:="Excel 97-2003 Files (*.xls), *.xls", _
            Title:="Select a file or files", _
            MultiSelect:=True)

    ' Perform some action with the files you selected.
    If IsArray(Fname) Then
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With

        For N = LBound(Fname) To UBound(Fname)

            ' Get only the file name and test to see if it is open.
            FnameInLoop = Right(Fname(N), Len(Fname(N)) - InStrRev(Fname(N), Application.PathSeparator, , 1))
            If bIsBookOpen(FnameInLoop) = False Then

                Set mybook = Nothing
                On Error Resume Next
                Set mybook = Workbooks.Open(Fname(N))
                On Error GoTo 0

                If Not mybook Is Nothing Then
                    MsgBox "You opened this file : " & Fname(N) & vbNewLine & _
                           "And after you press OK, it will be closed" & vbNewLine & _
                           "without saving. You can replace this line with your own code."
                    mybook.Close SaveChanges:=False
                End If
            Else
                MsgBox "We skipped this file : " & Fname(N) & " because it is already open."
            End If
        Next N
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End If

    ' Change drive/directory back to SaveDriveDir.
    ChDrive SaveDriveDir
    ChDir SaveDriveDir
End Sub


Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Contributed by Rob Bovey
    On Error Resume Next
    bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function

选取器功能

Sub WINorMAC()
' Test for the operating system.
    If Not Application.OperatingSystem Like "*Mac*" Then
        ' Is Windows.
        Call Select_File_Or_Files_Windows
    Else
        ' Is a Mac and will test if running Excel 2011 or higher.
        If Val(Application.Version) > 14 Then
            Call Select_File_Or_Files_Mac
        End If
    End If
End Sub
Sub WINorMAC_2()
' Test the conditional compiler constants.
    #If Win32 Or Win64 Then
        ' Is Windows.
        Call Select_File_Or_Files_Windows
    #Else
        ' Is a Mac and will test if running Excel 2011 or higher.
        If Val(Application.Version) > 14 Then
            Call Select_File_Or_Files_Mac
        End If
    #End If
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Excel VB 打开文件 OSX 和 Windows 的相关文章

  • 如何从 Mac OS X 导出证书以获得一对 .cert 和 .key 文件?

    我在 Mac 10 10 1 上创建了证书 并且在钥匙串访问证书中拥有私钥 在 Windows 上我必须使用这个 并且我需要 cert 和 key 文件 如何导出该证书以获得这两个文件 我可以导出为 pb12 或 cert 但不能导出为 c
  • Excel - 确定排列的奇偶性

    我正在处理一个 Excel 工作表 需要确定大小数字的垂直数组的奇偶校验N 该数组包含来自的每个数字1 to N每一次正好一次 在这种情况下 奇偶校验被定义为将加扰数组转换为从小到大排序的数组所需的交换次数 例如 数组 3 1 2 4 具有
  • Mac OS X 上的 64 位程序集运行时错误:“dyld:无可写段”和“Trace/BPT trap”

    当尝试运行以下汇编程序时 globl start start pushq 0x0 movq 0x1 rax subq 0x8 rsp int 0x80 我收到以下错误 dyld no writable segment Trace BPT t
  • 如何在 Unix 控制台或 Mac 终端中编译和运行 C/C++ 代码?

    如何在 Unix 控制台或 Mac 终端中编译 运行 C 或 C 代码 如果是一个简单的单源程序 make foo 源文件在哪里foo c foo cpp等等 你甚至不需要 makefile Make 有足够的内置规则将源文件构建为同名的可
  • 如何在网页中显示嵌入的 Excel 文件?

    我想允许在网页中查看嵌入的 Excel 报告 有什么办法吗 我不想使用 ActiveX 或 OWC Office Web 组件 我只想从 Internet Explorer 应用程序打开现有文件 我不希望用户下载然后打开它 使用 ifram
  • Java 在 OS X 报告上拖放移动而不是复制

    更新状态 Bug 已报告给 Oracle 目前仍处于开放 未解决状态 https bugs openjdk java net browse JDK 8054325 https bugs openjdk java net browse JDK
  • VBA 激活 Internet Explorer 窗口

    我正在制作一个宏 用于打开 Internet Explorer 导航并登录网站 一切正常 但我需要将 IE 窗口放在前面并激活它 这样我就可以使用SendKeys在上面 我发现网站和视频在名为的命令上有不同的方法AppActivate我已经
  • VBA - HTML 抓取问题

    我正在尝试从网站上抓取拍卖数据https www rbauction com heavy equipment auctions https www rbauction com heavy equipment auctions 我当前的尝试是
  • 从网站上的表格中抓取数据,而无需搜索标签

    这是这个问题的延续使用 InStr 搜索引号 空格 冒号等 https stackoverflow com questions 52673819 using instr to search for quotes spaces colons
  • 在 MS Outlook 中,报告所有未收到回复的已发送邮件

    我每天都会发送大量电子邮件 但常常无法跟踪哪些邮件得到了实际回复 有没有办法使用 VBA 脚本查看上周发送的所有消息 并检查他们是否收到回复 具体来说 是一份已发送电子邮件的报告 这些电子邮件尚未从至少一个发送到的地址收到回复 我了解一点
  • 将最后几个功能添加到我的调度模板生成器中

    感谢 StackOverflow 上的一些出色的人 我几乎完成了调度模板生成器的代码 我还想补充三件事 但我遇到了一些麻烦 当前细分 我有两张表 2 员工列表 和 X 模板 员工列表中的值是从单元格 D9 开始的一列员工姓名 然后将员工值转
  • 如何模拟“焦点”和“打字”事件

    尝试模拟 onfocus 和打字事件 但它不起作用 Sub Login MyLogin MyPass Dim IEapp As InternetExplorer Dim IeDoc As Object Dim ieTable As Obje
  • 如何在Mac上启动Syslogd服务器以接受远程日志记录消息?

    有人知道如何在 Mac 上启动 Syslogd 服务器来接受远程日志记录消息吗 我启动了 Syslogd 但似乎它不接受远程消息 如果我执行 netstat an 看起来 udp 端口 514 正在侦听 但是 如果我使用 nmap 从笔记本
  • 使用 VBA 的下拉菜单

    我需要使用 VBA 从下拉菜单中选择特定选项 我怎样才能做到这一点 链接到我们试图从中提取的网页 IE document getElementsByName down count click 我尝试过的代码 Full Module Priv
  • xlwings: 删除一个列 | Excel 中的行

    如何删除 Excel 中的一行 wb xw Book Shipment xlsx wb sheets Page1 1 range 1 1 clear clear 用于删除内容 我想删除该行 我很惊讶 clear 函数有效 但 delete
  • 在 Excel 表格中选择多列的代码

    我是 Excel VBA 新手 我需要修改我的代码 以便我能够进一步进行 我想在 Excel 表格中选择多个表格列 这是我的代码 Dim ws As Worksheet Dim tbl As ListObject Set ws Sheets
  • VBA Shell 并等待退出代码

    我正在打包一个办公应用程序 VBA 它调用 C 控制台应用程序来执行应用程序 大型模拟程序 的一些繁重工作 我希望能够让 VBA 应用程序等待控制台应用程序完成并从控制台应用程序检索退出代码 我已经能够做到前者 但尚未能够从应用程序中检索退
  • 合并和颜色样式不适用于 Apache POI excel 2003 格式

    在 Apache POI 中 我为某些单元格应用了一些样式并合并了这些单元格 当我在 2010 年或 2007 年打开时 它工作正常 但在 2003 年 格式样式消失了 每次保存 2003 Excel 文件之前都会弹出兼容性检查对话框 请参
  • 如何在 Excel 中对一组数据进行排序以匹配另一组数据?

    我有一个不按字母或数字顺序排列的数据列表 我想对同一日期的第二个列表进行排序以匹配第一个列表 我无法更改数据的顺序 我的目标是将第二组中的附加数据粘贴回第一个数据集中 DATA SET A DATA SET B 22350 BH160 10
  • 最近打开的应用程序[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 有什么方法可以获取最近打开的应用程序 例如 4 个 的列表吗 如果是这样 怎么办 可可麦克 看看LaunchServices LSSh

随机推荐