VBA Help with Email File errors

NeoSez

Board Regular
Joined
Aug 14, 2020
Messages
210
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I found this code on this forum, but I can not get it to work properly. It keeps stopping and giving me errors.
I want to email only a certain range and not the entire worksheet from that multi-sheet workbook.
The new attachment filename should be the sheet name, but it currently names it after the Entire Workbook.
Can someone help? I've been looking at this all week and it's driving me mado_O
Thanks for your help.

Gives error here---> What am I supposed to have here so it doesn't keep giving error?

VBA Code:
FileName = ("FileName") & "xlsx"


VBA Code:
Sub EmailandSaveCellValue()

'Variable declaration
Dim oApp As Object, _
oMail As Object, _
WB As Workbook, _
FileName As String, MailSub As String, MailTxt As String

'************************************************* ********
'Set email details; Comment out if not required
'Const MailTo = "someone@someone.com"
'Const MailCC = ""
'Const MailBCC = ""
'MailSub = "My Subject Line "
'MailTxt = "Body of Email"
'************************************************* ********

'Turns off screen updating
Application.ScreenUpdating = False

'Makes a copy of the active sheet and save it to
'a temporary file
ActiveWorkbook.Copy
ActiveSheet.Range ("A1:K100").copy
Set WB = ActiveWorkbook
FileName = ("FileName") & "xlsx"
On Error Resume Next
Kill "C:\" & FileName
On Error GoTo 0
WB.SaveAs FileName:="C:\" & FileName

'Creates and shows the outlook mail item
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = someone@someone.com
'.Cc = MailCC
'.Bcc = MailBCC
.Subject = "My Subject Line "
.Body = "Insert text"
.Attachments.Add WB.FullName
.Display
End With

'Deletes the temporary file
WB.ChangeFileAccess Mode:=xlReadOnly
Kill WB.FullName
WB.Close SaveChanges:=False

'Restores screen updating and release Outlook
Application.ScreenUpdating = False
Set oMail = Nothing
Set oApp = Nothing
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this. I changed the target destination to C:\temp. I don't use Outlook. So, can't help on that part and parts after that. Specifically, ".Display" doesn't work for me, probably because I don't have Outlook installed or it's not running or something.

VBA Code:
Sub EmailandSaveCellValue_new()

    'Variable declaration
    Dim oApp As Object
    Dim oMail As Object
    Dim wb As Workbook, ws As Worksheet
    Dim FileName As String, MailSub As String, MailTxt As String

    '************************************************* ********
    'Set email details; Comment out if not required
    'Const MailTo = "someone@someone.com"
    'Const MailCC = ""
    'Const MailBCC = ""
    'MailSub = "My Subject Line "
    'MailTxt = "Body of Email"
    '************************************************* ********

    'Turns off screen updating
    Application.ScreenUpdating = False

    Set ws = ActiveSheet
    'add a new workbook
    Set wb = Workbooks.Add
    'copy range to the new workbook
    ws.Range("A1:K100").Copy Destination:=wb.Sheets(1).Range("A1")
    'set file name to sheet name
    FileName = ws.Name & ".xlsx"
    On Error Resume Next
    Kill "C:\temp\" & FileName
    On Error GoTo 0
    wb.SaveAs FileName:="C:\temp\" & FileName

    'Creates and shows the outlook mail item
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(0)
    With oMail
        .To = "someone@someone.com"
        '.Cc = MailCC
        '.Bcc = MailBCC
        .Subject = "My Subject Line "
        .Body = "Insert text"
        '.Attachments.Add wb.FullName
        .Attachments "C:\temp\" & FileName
        .Display
    End With

    'Deletes the temporary file
    wb.ChangeFileAccess Mode:=xlReadOnly
    Kill wb.FullName
    wb.Close savechanges:=False

    'Restores screen updating and release Outlook
    Application.ScreenUpdating = False
    Set oMail = Nothing
    Set oApp = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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