VBA: Execute Macro from WB1, and email WB2

sachavez

Active Member
Joined
May 22, 2009
Messages
469
Office 365

I cobbled this macro together and it almost works as planned. The macro copies data from a pivot table to a new workbook, saves the new workbook, and emails the original workbook to the designated email address. How can I modify the code to email the new workbook?

VBA Code:
Sub PVAL()

    Application.ScreenUpdating = False
    Range("Art").Select
    Selection.ShowDetail = True
    ActiveSheet.Name = "AG"
    Selection.Copy
    Workbooks.Add
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns.AutoFit
    With ActiveSheet.Sort
     .SortFields.Clear
     .SortFields.Add Key:=Range("A1"), Order:=xlAscending 'Date
     .SetRange ActiveSheet.UsedRange
     .Header = xlYes
     .Apply
    End With
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:="C:\Users\b144519\Documents\Art " & Format(DateAdd("d", -1, Date), "mmddYY") & ".xls", _
    FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
    Range("A1").Select
    Dim emailapp As Outlook.Application
    Set emailapp = New Outlook.Application
    Dim emailitem As Outlook.MailItem
    Set emailitem = emailapp.CreateItem(olMailItem)
    emailitem.To = "Art@YYYY.com"
    emailite.CC = "Steven@xxx.com"
    emailitem.Subject = "AutoPay Update"
    emailitem.HTMLBody = "Here is the updated file, updated through yesterday."
    Source = ThisWorkbook.FullName
    emailitem.Attachments.Add Source
    emailitem.Send
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
       
End Sub
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Disregard, I solved it by updating this line of code:

VBA Code:
emailitem.Attachments.Add ActiveWorkbook.FullName
 
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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