save worksheets as a wb and email them

GS7CLW

Banned
Joined
Aug 10, 2010
Messages
168
Trying to "scroll" thru the worksheets in a wb and move them to a workbook and email taht work book. This code works for only the first sheet in the book then it hangs on:

ActiveWorkbook.Worksheets(I).Select

Code:
Sub EmailSheets()
Dim Worksheet
Dim RSID As String
Dim WS_Count As Integer
   Dim I As Integer
   ' Set WS_Count equal to the number of worksheets in the active
   ' workbook.
   WS_Count = ActiveWorkbook.Worksheets.Count
   ' Begin the loop.
   For I = 1 To WS_Count
    ActiveWorkbook.Worksheets(I).Select  '--- HANGS UP ON THIS LINE ---
    RSID = ActiveSheet.Name
    Dim OL              As Object
    Dim EmailItem       As Object
    Dim wb              As Workbook
    Set OL = CreateObject("Outlook.Application")
    Set EmailItem = OL.CreateItem(olMailItem)
    Set wb = ActiveWorkbook
    wb.Save
    
    Sheets(RSID).Select
    Sheets(RSID).Move
    ActiveWorkbook.SaveAs Filename:= _
        "[URL="file://\\Ac35dfsps02p\s3_data$\FLR"]\\Ac35dfsps02p\s3_data$\FLR[/URL] CDR\" & RSID & ".xls", FileFormat:=xlExcel8 _
        , Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
        CreateBackup:=False
    With EmailItem
        .Subject = "Unassigned Leads Report for " & RSID
        .Body = "  Unassigned Leads Report for " & RSID & " (" & RSID & ".xlsm) attached!" & vbCrLf & _
        "  " & vbCrLf & _
          "  "
        .To = RSID & "@SOMEWHERE.COM"
       ' .Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
        .Attachments.Add wb.FullName ' change this name for each rsid
        UserForm1.Label1.Caption = "Emailing your report to" & RSID
        UserForm1.Repaint
        .Send
    End With
    Set wb = Nothing
    Set OL = Nothing
    Set EmailItem = Nothing
   Next I
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Move will fail anyway when it tries to move the last sheet because a workbook must contain at least one sheet. Therefore try Sheets(I).Copy instead of the Move and after the SaveAs add:

ActiveWorkbook.Close savechanges:=False

I'm not sure if the .Attachments.Add wb.FullName is correct. Shouldn't it be the same as the file name specified in the SaveAs? Also the Selects are unnecessary; you can reference sheets directly, e.g. Sheets(I).Name.
 
Upvote 0
It does cycle thru the entire workbook NOW, and it does save each sheet as a wb. I believe you are right about the:

Attachments.Add wb.FullName

not being right as the emails go out with the original Unassignedleads.xlsx rather than the RSID.xlsx (i.e.5D2W.xlsx) attached to it..
 
Last edited:
Upvote 0
not sure how to go about naming the attachment here:

Attachments.Add wb.FullName

for each email I use
RSID@somewhere.com
RSID is also the name of the sheet and resulting file (i.e.):
5D2W.xls
 
Upvote 0
It does cycle thru the entire workbook NOW, and it does save each sheet as a wb
But you said that the code only works for the first sheet and then hangs (on the second sheet). Have you tried the changes I suggested?

not sure how to go about naming the attachment here:

Attachments.Add wb.FullName
The same as the SaveAs file name:

Code:
.Attachments.Add "\\Ac35dfsps02p\s3_data$\FLR CDR\" & RSID & ".xls"
 
Upvote 0
worked great!!!!!!!!!!

almost done with this project need to make some minor changes
but it's nothing I can't handle. Do want to thank everyone for ALL their help. I truly appreciate all of it. Hope evryone has a great week/end!!!

Thanks to all
cliff
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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