Avatar of Dave Evans
Dave Evans
 asked on

ActiveWorkbook.SaveAs in /TMP?

Please forgive what may be astonishing ignorance.

On a PC, it works, but I can't get it to work on a Mac - Goal is to save a temporary Excel with macros file (so it will be deleted thereafter) with a standardized name for emailing. I'd like to do so in the TEMP directory. Here it is for the PC:

ActiveWorkbook.SaveAs Filename:=Environ("temp") & "\" & Range("J112").Text & " - MCR " & Range("D112").Text, _
         FileFormat:=52, CreateBackup:=False

I've tested various combinations without success. What to do?
Thanks!
VBAMac OS XMicrosoft Excel

Avatar of undefined
Last Comment
Zack Barresse

8/22/2022 - Mon
Koen

i don't know how the save command needs to work on a mac...but good practice for debugging is creating a variable for your filename and save on the next line.
then when you step through your code, you can inspect the actual value of your MyFilename variable before the error hits...

Also when adding cell content to a filename you should consider error handling for illegal characters
I always use a function for that (that I found here on EE)

Function strLegalFileName(strFileNameIn As String)

'this function removes all illegal characters from a String used as sheetname and filename

    Dim i As Integer
    
    Const strIllegals = "\/|?*<>"":"
    strLegalFileName = strFileNameIn
    For i = 1 To Len(strIllegals)
        strLegalFileName = Replace(strLegalFileName, Mid$(strIllegals, i, 1), "_")
    Next i
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Zack Barresse

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Zack Barresse

Without additional information from the OP, this is the most succinct answer available.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck