【问题标题】:How to check if the excel workbook is currently working on a MAC or Windows如何检查 excel 工作簿当前是否在 MAC 或 Windows 上运行
【发布时间】:2017-08-12 08:45:32
【问题描述】:

我有一个工作簿,其中包含一些自动化来重新配置路径和命运,因为它以许多不同的文件结构打开。

(例如,在 '//user/dropbox' 和 //user/documents/dropbox' 处打开同一个工作簿)等

对于每个用户,它有不同的初始路径。 (例如,其中一个自动化在“/dropbox/comercial/project number”内创建文件夹结构。

它在 windows 中运行良好,但是当我尝试在 mac 中运行它时,它会出错。 显然,文件路径是正确的,但“/”在另一边“\”。 所以结果类似于“\user\documents\dropbox/comercial/project number”

我不确定这是否是问题,但是,如果是,我该如何解决?

我正在考虑制作一个宏来检查它是否在 mac 或 windows 上运行,并为每个宏“重新配置”具有正确符号的路径。

我不确定我是否足够清楚。如果不是,请询问,我会尽力解释!

谢谢!

【问题讨论】:

    标签: windows macos excel vba


    【解决方案1】:

    两个选项:

    1) 使用Application.PathSeparator 而不是“/” - 就像在函数中一样:

    Function BuildPath(sections As Variant) As String
        BuildPath = Join$(sections, Application.PathSeparator)
    End Function
    
    '// Example use
    '
    '   Dim sections As Variant
    '   sections = Array("documents", "projects", "files", "myfile.xlsx")
    '   
    '   Debug.Print BuildPath(sections)
    '//
    ' ~~> output (e.g. Windows): documents\projects\files\myfile.xlsx 
    

    2) 使用条件编译检查环境:

    #If Mac Then
        Const ps As String = "/"
    #Else
        Const ps As String = "\"
    #End If
    
    '// rest of code here, using "ps" as path separator
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 2011-03-28
      • 2015-10-12
      • 2018-02-15
      相关资源
      最近更新 更多