Maxcro to Attsch File

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the month and year ain Cell D1 on sheet "Email" in format mmm-yyyy


I have code below to create and email in Outlook and to save the file name to be attached as Reports Outstanding +the Month and year in Cell D1.xlsx

I get a run time error and yhe code below is highlighted


Code:
 .Attachments.Add Environ("TMP") & "t" & ThisWorkbook.Sheets("Reports Outstanding").Name & " " & Format(ThisWorkbook.Sheets("Email").Range("D1").Value, "MMM-yyyy") & ".xlsx"

It would be appreciated if someone could amend my code

Code:
Sub emailOneItem()
Dim Ztext1 As String
Dim Zsubject1 As String


Sheets("Reports Outstanding").Activate
'ThisWorkbook.Activate                           'start in THIS workbook
Ztext1 = [bodytext1]                              'read in text from named cell
Zsubject1 = [subjectText1]
     Sheets("Reports Outstanding").Copy
    With ActiveWorkbook
        '.Windows(1).Visible = False
        Application.DisplayAlerts = False

.SaveAs Environ("TMP") & "\" & "Reports Outstanding" & Format(ThisWorkbook.Sheets("Email").Range("D1").Value, "mmm-yyyy") & ".xlsx"

        Application.DisplayAlerts = True
        .Close (True)
    End With
    
    With CreateObject("Outlook.Application").CreateItem(0)
        .To = Join(Application.Transpose(Sheets("Reports Outstanding").Range("D2:D20").Value), ";")
        .Subject = Zsubject1
        .Body = Ztext1
        
      
 .Attachments.Add Environ("TMP") & "\" & ThisWorkbook.Sheets("Reports Outstanding").Name & " " & Format(ThisWorkbook.Sheets("Email").Range("D1").Value, "MMM-yyyy") & ".xlsx"



        .Display
        '.send
    End With
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I have managed to amend the code and it works perfectly


Code:
 Sub emailOneItem()

Dim Ztext1 As String

Dim Zsubject1 As String



Ztext1 = [bodytext1] 'read in text from named cell

Zsubject1 = [subjectText1]



'Save Reports Outstanding sheet to a new workbook

Sheets("Reports Outstanding").Copy

With ActiveWorkbook

'.Windows(1).Visible = False

Application.DisplayAlerts = False

.SaveAs Environ("TMP") & "\" & "Reports Outstanding " & Format(ThisWorkbook.Sheets("Email").Range("D1").Value, "MMM-yyyy") & ".xlsx"

Application.DisplayAlerts = True

.Close (True)

End With



'Create and send email with attachment

With CreateObject("Outlook.Application").CreateItem(0)

.To = Join(Application.Transpose(Sheets("Reports Outstanding").Range("D2:D20").Value), ";")

.Subject = Zsubject1

.Body = Ztext1

.Attachments.Add Environ("TMP") & "\" & "Reports Outstanding " & Format(ThisWorkbook.Sheets("Email").Range("D1").Value, "MMM-yyyy") & ".xlsx"

.Display

'.send

End With

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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