excel - MacOS excel的这段代码相当于什么?
问题描述
有人可以建议在Excel for Mac中使用的等效代码会产生与以下在 Windows 中相同的结果吗?
Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
ActiveWorkbook.SaveAs Path & "CAD DATA.xlsx"
解决方案
使用类似下面的函数
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
推荐阅读
- mongodb - 如何在与旧的损坏实例相同的盒子上创建新的 mongodb 实例
- angular - 如何在文件夹结构和 TypeScript 中构建 Express 服务器并将其部署到 Heroku
- flutter - 如何在 Init 状态下访问提供者值
- android - StreamConfigurationMap.getOutputSizes 返回的预期大小
- javascript - 如何使用 Fetch 将附加数据从客户端 Stripe Checkout 传递到服务器
- python - 如何用变量替换填充数字以填充格式或 f 字符串中的宽度?
- freebsd - 如何解决“pkg:没有受信任的证书”
- angular - 使用 Angular 8 在表中添加一行
- elasticsearch - 如何执行基于聚合的搜索?
- node.js - 成员必须满足正则表达式模式:[\\S]+