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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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