MacOS excel的这段代码相当于什么?

问题描述 投票:1回答:1

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

Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
ActiveWorkbook.SaveAs Path & "CAD DATA.xlsx"
excel vba macos excel-vba-mac
1个回答
2
投票

使用类似以下功能的东西

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

© www.soinside.com 2019 - 2024. All rights reserved.