'What is the equivalent of this code for MacOS excel?

Can someone advise the equivalent code to use in Excel for Mac that would create the same result as the below does in Windows?

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


Solution 1:[1]

Use something like the following function

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

in your code to make it work on both Mac and Windows

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

Make sure ActiveWorkbook is actually what you want to use. You probably meant to use ThisWorkbook:

  • ActiveWorkbook is the workbook that has the focus (is on top) while this code runs. This can easily change by a simple mouse click or any other interference.
  • ThisWorkbook is the workbook this code runs at. This is much more reliable because it never changes by any user interaction.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1