Temp File Folder in MAC vs Windows

LeanneBG

Board Regular
Joined
Jun 20, 2016
Messages
157
Hi Experts!

I have a line item in my vba that saves an excel file in a temporary folder and then attach in an outlook email. It perfectly works on a normal laptop but when my co-worker tried it in his mac, the vba is debugging showing a run time error '1004': Microsoft Excel cannot access the file. Something with the file name or path doesn't exist.. Is there a way to tweak below code that will be able to work even on mac laptops, or maybe if it detected that it's a mac laptop, just save the file on the desktop instead of accessing temp file folder?

Code:
Sub EmailWorkbook_Issue()
'PURPOSE: Create email message with ActiveWorkbook attached
'SOURCE: www.TheSpreadsheetGuru.com


Dim SourceWB As Workbook
Dim DestinWB As Workbook
Dim OutlookApp As Object
Dim OutlookMessage As Object
Dim TempFileName As Variant
Dim TempFilePath As String
Dim FirstName As String
Dim LastName As String
Dim c As Long


Set SourceWB = ActiveWorkbook


'Determine Temporary File Path and File Name
  TempFilePath = Environ$("temp") & "\"
  TempFileName = "Access Issue from " & Application.UserName & " - " & Format(DateTime.Now, "MMM dd, yyyy")
  
'Save Temporary Workbook
[B]  SourceWB.SaveCopyAs TempFilePath & TempFileName & ".xls" -- this is where i encountered the error[/B]
  Set DestinWB = Workbooks.Open(TempFilePath & TempFileName & ".xls")

'Save Changes
  DestinWB.Save


  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.DisplayAlerts = False


'Create Instance of Outlook
  On Error Resume Next
    Set OutlookApp = GetObject(class:="Outlook.Application") 'Handles if Outlook is already open
  Err.Clear
    If OutlookApp Is Nothing Then Set OutlookApp = CreateObject(class:="Outlook.Application") 'If not, open Outlook
    
    If Err.Number = 429 Then
      MsgBox "Outlook could not be found, aborting.", 16, "Outlook Not Found"
      GoTo ExitSub
    End If
  On Error GoTo 0


'Create a new email message
  Set OutlookMessage = OutlookApp.CreateItem(0)


c = InStr(Application.UserName, ",")
FirstName = Right(Application.UserName, c + 1)
LastName = Left(Application.UserName, c - 1)


'Create Outlook email with attachment
  On Error Resume Next
    With OutlookMessage
     .To = "bmssecurity.im@pg.com"
     .CC = ""
     .BCC = ""
     .Subject = TempFileName
     .Body = "Hi BMS Security," & vbNewLine & vbNewLine & "Please help me with my access issue. See attached." & vbNewLine & vbNewLine & "Thanks," & vbNewLine & FirstName & " " & LastName
     
     .Attachments.Add DestinWB.FullName
     .Display
    End With
  On Error GoTo 0


'Close & Delete the temporary file
  DestinWB.Close SaveChanges:=False
  Kill TempFilePath & TempFileName & ".xls"


'Clear Memory
  Set OutlookMessage = Nothing
  Set OutlookApp = Nothing
  
'Optimize Code
ExitSub:
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.DisplayAlerts = False


End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Not got a mac in front of me but this line

Code:
TempFilePath = Environ$("temp") & "\"
definitely needs changing the path separator to
Code:
TempFilePath = Environ$("temp") & [COLOR="#FF0000"]Application.PathSeparator[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top