I picked up some code from http://www.vbaexpress.com/forum/showthread.php?t=2276&highlight=excel
This code works very well, but I needed to modify it for my needs. I am having a problem running the code more than once. The first time it runs very nicely. After that I get an error: Run-time error '1004': Copy method of Worksheet class failed. The line of code ActiveSheet.Copy is highlighted in yellow. Could someone take a look at this? I am still a novice, and as such have probably done something really rooky like. One thing that I thought might be a problem is the VBA sheet name gets changed every time. Not the (Sheet2) but the internal Sheet2 that VBA uses and can't be changed by renaming a tab. Well here is what I have so far:
Private Sub CommandButton1_Click()
' this macro saves the active sheet to a cell value as seen below (5, 9) = Cell I5
'Variable declaration
Dim oApp As Object, _
oMail As Object, _
wb As Workbook, _
FileName As String
'Turns off screen updating
Application.ScreenUpdating = False
Range("A1:F369").Select
Selection.Copy
Sheets("Sheet1").Select
Sheets.Add
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveSheet.Columns.AutoFit
ActiveSheet.Rows.AutoFit
'Makes a copy of the active sheet and save it to
'a temporary file
ActiveSheet.Copy
Set wb = ActiveWorkbook
' this macro saves the active sheet to a cell value as seen below (5, 9) = Cell I5
FileName = Cells(5, 9).value & " .xls"
On Error Resume Next
Kill "C:\" & FileName
On Error GoTo 0
wb.SaveAs FileName:="C:\Documents and Settings\me you\My Documents\" & FileName
'Creates and shows the outlook mail item
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
'Uncomment the line below to hard code a To recipient
.To = "someone@somewhere.com"
'Uncomment the line below to hard code a CC recipient
.CC = "someone@somewhere.com;someone@somewhere.com"
'Uncomment the line below to hard code a Bnn recipient
'.Bcc = "someone@somewhere.com"
'Uncomment the line below to hard code a subject
.Subject = " Please review " & Cells(5, 9).value
.Body = "I have attached to this email " & Cells(5, 9).value
.Attachments.Add wb.FullName
.Display
ActiveWorkbook.Close
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
End With
'Restores screen updating and release Outlook
Application.ScreenUpdating = True
Set oMail = Nothing
Set oApp = Nothing
End Sub
Thanks for any help you can offer