VBA - Customizing Excel file name using named range and attaching to email

AmeliaBedelia

New Member
Joined
Apr 8, 2018
Messages
19
I have figured out how to customize an excel file name and attach it to an email, but cannot seem to get the code to work using a named range in my workbook. I want to use a named range so the file name is automatically adjusted based on information within the workbook.

This is the code that creates the temporary file name of "FeedbackForm.xlsx":
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\FeedbackForm.xlsx"

I tried doing the following:
ActiveWorkbook.SaveAs ThisWorkbook.Path & "" & Range("FormName").Value & ".xlsx"

When doing this I also altered the following lines of code to match this new name:
.Attachments.Add (ThisWorkbook.Path & "" & Range("FormName").Value & ".xlsx")
Kill ThisWorkbook.Path & "" & Range("FormName").Value & ".xlsx"


But it produces the following error: "Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed" and highlights:
ActiveWorkbook.SaveAs ThisWorkbook.Path & "" & Range("FormName").Value & ".xlsx"


Here is the code that produces an excel attachment called "FeedbackForm.xlsx", but how do I get it to customize the file name based off a named Range (FormName)?


Dim OutApp As Object
Dim OutMail As Object
Dim FileName As String
Dim myFile As Variant

'Copy sheet to new workbook
Sheets("Form").Select
Sheets("Form").Copy



ActiveWorkbook.SaveAs ThisWorkbook.Path & "\FeedbackForm.xlsx"

' Open Outlook and start a new mail item
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.logon
Set OutMail = OutApp.CreateItem(0)

'Build email item and send
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.body = "Please see the attached document for feedback." & _
vbCrLf & "Thank you." & vbNewLine & vbCrLf & vbNewLine & "(signature of BEA)"
.Attachments.Add (ThisWorkbook.Path & "\FeedbackForm.xlsx")
.Display
End With

'Delete temporary Excel file
ActiveWorkbook.Close SaveChanges = True
Kill ThisWorkbook.Path & "\FeedbackForm.xlsx"

'Memory cleanup
Set OutMail = Nothing
Set OutApp = Nothing
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The back-slash does not seem to appear once I saved. There should be a back-slash in the following statements:

I tried doing the following:
ActiveWorkbook.SaveAs ThisWorkbook.Path & "[back-slash]" & Range("FormName").Value & ".xlsx"

When doing this I also altered the following lines of code to match this new name:
.Attachments.Add (ThisWorkbook.Path & "[back-slash]" & Range("FormName").Value & ".xlsx")
Kill ThisWorkbook.Path & "[back-slash]" & Range("FormName").Value & ".xlsx"


But it produces the following error: "Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed" and highlights:
ActiveWorkbook.SaveAs ThisWorkbook.Path & "[back-slash]" & Range("FormName").Value & ".xlsx"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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