首页 > 解决方案 > MacOS excel的这段代码相当于什么?

问题描述

有人可以建议在Excel for Mac中使用的等效代码会产生与以下在 Windows 中相同的结果吗?

Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
ActiveWorkbook.SaveAs Path & "CAD DATA.xlsx"

标签: excelvbamacos

解决方案


使用类似下面的函数

Function GetDesktopPath() As String
    #If Mac Then
        GetDesktopPath = Mid(MacScript("tell application ""Finder""" & vbLf & "return desktop as alias" & vbLf & "end tell"), 7) 
    #Else
        GetDesktopPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    #End If
End Function

在您的代码中使其在 Mac 和 Windows 上都能正常工作

Path = GetDesktopPath & Application.PathSeparator
ActiveWorkbook.SaveAs Path & "CAD DATA.xlsx"

确保ActiveWorkbook实际上是您想要使用的。您可能打算使用ThisWorkbook

  • ActiveWorkbook是此代码运行时具有焦点(位于顶部)的工作簿。这可以通过简单的鼠标单击或任何其他干扰轻松更改。
  • ThisWorkbook是运行此代码的工作簿。这更加可靠,因为它永远不会因任何用户交互而改变。

MacScript 源码:http ://www.vbaexpress.com/forum/showthread.php?54852-Returning-the-Desktop-Path


推荐阅读