How to save a file to the Desktop in VBA (Mac)

Viewed 5634

I am writing a Macro in VBA that needs to be compatible for both Windows and Mac machines. I have everything working up until the end when I need to save the file to the users Desktop as a Tab Delimited Text File. Below is the code I have tried, but I keep getting an error. Any help is appreciated. Thanks!

'Export doc as Delimited Text

Dim OS As String
OS = Application.OperatingSystem

Dim un As Variant

un = (Environ$("Username"))

Dim wb As Workbook
Set wb = Workbooks.Add
ThisWorkbook.Sheets("dataExport").Copy Before:=wb.Sheets(1)

If InStr(OS, "Windows") > 0 Then

wb.SaveAs Filename:="C:\Users\" & un & "\Desktop\" & CaseNum & ".txt", FileFormat:=xlText

Else

wb.SaveAs Filename:="/Users/" & un & "/Desktop/" & CaseNum & ".txt", FileFormat:=xlText

End If
1 Answers

On a Mac in Excel you must use the : as separator:

wb.SaveAs Filename:="Macintosh HD:Users:" & un & ":Desktop:" & CaseNum & ".txt", FileFormat:=xlText 
Related