VBA Save Worksheets as values and draft emails

willastrowalker

New Member
Joined
Aug 28, 2015
Messages
9
there seem to be lots of codes for this out there and I'm really close to getting it figured out.. I have a workbook that has 26 sheets, I want to create a macro that saves all sheets as hardcodes (completed) and creates a draft email in outlook with each sheet as an attachment.

Created a loop to save all sheets as hardcodes, so that works fine, just need help with what I think is a minor tweak to create individual emails.

Everything works, but the attachments are all placed in the same email, and not in separate email drafts.. So I want 26 drafts with one attachment, not one draft with 26 attachments.

Code:
Sub Saveandemail()


    Application.DisplayAlerts = False
    
Dim sname As String
    Dim wsht As Worksheet
    Dim wbnew As Workbook
    Dim todaysdate As String
    Dim wkshtname As String
    Dim OutApp As Object
    Dim Outmail As Object
    Dim Emailaddress as string
            
    Set OutApp = CreateObject("Outlook.Application")
    Set Outmail = OutApp.CreateItem(0)
        
    todaysdate = Format(Now, "MM-DD-YY")
    For Each wsht In ActiveWorkbook.Sheets
        
        wkstname = ActiveSheet.name
        wsht.Copy
        With ActiveSheet.UsedRange
            .Value = .Value
        End With
        Set wbnew = ActiveWorkbook
        sname = Range("g60").Value
        emailaddress = Range("g61").value
    
        With wbnew
            .SaveAs "C:\filepath\" & wsht.name & "_" & todaysdate & ".xlsx"
            With Outmail
                .to = emailaddress
                .CC = ""
                .BCC = ""
                .Subject = "emailing" & wkshtname
                .Body = "Hi there"
                .Attachments.Add wbnew.FullName
                .Save
            End With
       
                 
        wbnew.Close True
        
        End With
    
    Next wsht
        
    Application.DisplayAlerts = True
    
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Here, try this, changes marked in red

Code:
Sub Saveandemail()

[B][COLOR=#ff0000]With Application[/COLOR][/B]
[B][COLOR=#ff0000]    .ScreenUpdating = False[/COLOR][/B]
[B][COLOR=#ff0000]    .DisplayAlerts = False[/COLOR][/B]
[B][COLOR=#ff0000]End With[/COLOR][/B]
    
Dim sname As String
    Dim wsht As Worksheet
    Dim wbnew As Workbook
    Dim todaysdate As String
    Dim wkshtname As String
    Dim OutApp As Object
    Dim Outmail As Object
    Dim Emailaddress As String
            
    Set OutApp = CreateObject("Outlook.Application")
    Set Outmail = OutApp.CreateItem(0)
    
    todaysdate = Format(Now, "MM-DD-YY")
    For Each wsht In ActiveWorkbook.Sheets
        
        wkstname = ActiveSheet.Name
        wsht.Copy
        With ActiveSheet.UsedRange
            .Value = .Value
        End With
        Set wbnew = ActiveWorkbook
        sname = Range("g60").Value
        Emailaddress = Range("g61").Value
    
        With wbnew
            .SaveAs "C:\filepath\" & wsht.name & "_" & todaysdate & ".xlsx"
            With Outmail
[B][COLOR=#ff0000]                .Display[/COLOR][/B]
                .To = Emailaddress
                .CC = ""
                .BCC = ""
                .Subject = "emailing" & wkshtname
                .Body = "Hi there"
                .Attachments.Add wbnew.FullName
                .Save
[B][COLOR=#ff0000]                .Close (xlSave)[/COLOR][/B]
            End With
         
        wbnew.Close True
        
        End With
    
    Next wsht
        
[B][COLOR=#ff0000]With Application[/COLOR][/B]
[B][COLOR=#ff0000]    .ScreenUpdating = True[/COLOR][/B]
[B][COLOR=#ff0000]    .DisplayAlerts = True[/COLOR][/B]
[B][COLOR=#ff0000]End With[/COLOR][/B]
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,812
Members
449,095
Latest member
m_smith_solihull

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