Email to named attached File as "Financial Data"

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
i have he following macro to create email in Outlook and attach several sheets

I get subscript out of range and the code below is highlighted

Code:
 .Attachments.Add Environ("TMP") & "\" & ThisWorkbook.Sheets("Financial Data for BR1").Name & ".xlsx"

See full code below

Code:
 Sub EmailSheets()

Dim Ztext As String

Dim Zsubject As String


'ThisWorkbook.Activate 'start in THIS workbook

Ztext = [bodytext] 'read in text from named cell

Sheets(Array("Statement of accounts", "Statistical Information")).Copy

With Range("A1:O150")

.Value = .Value

End With



With ActiveWorkbook

Application.DisplayAlerts = False

.SaveAs Environ("TMP") & "\" & ThisWorkbook.Sheets("Statement of Financial Position").Name & ".xlsx"



Application.DisplayAlerts = True

.Close (True)

End With



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

.To = Join(Application.Transpose(Sheets("index").Range("S1:S3").Value), ";")

.Subject = "Financial Info"

.Body = Ztext



.Attachments.Add Environ("TMP") & "\" & ThisWorkbook.Sheets("Financial Data for BR1").Name & ".xlsx"

.Display

End With 
end Sub


It would be appreciated if someone could kindly amend my code
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
VBA Code:
ThisWorkbook.Sheets("Financial Data for BR1").Name & ".xlsx"
This creates the string "Financial Data for BR1.xlsx"

But your code is saving the file as "Statement of Financial Position.xlsx" so it can't find the file "Financial Data for BR1.xlsx" and generates the error

Untested, however, try:
VBA Code:
Sub EmailSheets()

    Dim Ztext As String
    Dim Zsubject As String

    Dim file_save_name As String

    'ThisWorkbook.Activate 'start in THIS workbook
    Ztext = [bodytext].Value 'read in text from named cell
   
    Sheets(Array("Statement of accounts", "Statistical Information")).Copy
    With Range("A1:O150")
        .Value = .Value
    End With

    Application.DisplayAlerts = False
   
    With ActiveWorkbook
        file_save_name = "Statement of Financial Position.xlsx"
        .SaveAs Environ("TMP") & "\" & file_save_name
        .Close True
    End With
   
    Application.DisplayAlerts = True

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

        .To = Join(Application.Transpose(Sheets("index").Range("S1:S3").Value), ";")
        .Subject = "Financial Info"
        .Body = Ztext
           
        .Attachments.Add Environ("TMP") & "\" & file_save_name
        .Display
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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