Option Explicit
[COLOR="SeaGreen"]'This procedure will send the ActiveSheet in a new workbook
'For more sheets use : Sourcewb.Sheets(Array("Sheet1", "Sheet3")).Copy
[/COLOR]
Sub CDO_Mail_ActiveSheet_Or_Sheets()
[COLOR="SeaGreen"]'Working in 97-2007[/COLOR]
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim iMsg As Object
Dim iConf As Object
Dim Flds As Variant
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
[COLOR="SeaGreen"]'Copy the ActiveSheet to a new workbook[/COLOR]
ActiveSheet.Copy
[COLOR="SeaGreen"] 'Or if you want to copy more than one sheet use:
'Sourcewb.Sheets(Array("Sheet1", "Sheet3")).Copy[/COLOR]
Set Destwb = ActiveWorkbook
[COLOR="SeaGreen"] 'Determine the Excel version and file extension/format[/COLOR]
With Destwb
If Val(Application.Version) < 12 Then
[COLOR="SeaGreen"]'You use Excel 97-2003[/COLOR]
FileExtStr = ".xls": FileFormatNum = -4143
Else
[COLOR="SeaGreen"] 'You use Excel 2007
'We exit the sub when your answer is NO in the security dialog that you only
'see when you copy a sheet from a xlsm file with macro's disabled.[/COLOR]
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog"
Exit Sub
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
[COLOR="SeaGreen"]' 'Change all cells in Destwb to values if you want
' For Each sh In Destwb.Worksheets
' sh.Select
' With sh.UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
' Next sh
' Destwb.Worksheets(1).Select
'Save the new workbook/Mail it/Delete it[/COLOR]
TempFilePath = Environ$("temp") & "\"
TempFileName =[COLOR="Red"] "Extrait de" & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")[/COLOR]
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
.Close savechanges:=False
End With
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 [COLOR="SeaGreen"]' CDO Source Defaults[/COLOR]
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = [COLOR="Red"]"smtp.tonserveursmtp.fr"[/COLOR]
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
With iMsg
Set .Configuration = iConf
.To =[COLOR="Red"]"toto@machin.fr"[/COLOR] [COLOR="SeaGreen"]‘adresse du destinataire[/COLOR]
.CC = ""
.BCC = ""
.From =[COLOR="Red"] """bocaramel""<bocaramel@truc.fr >"[/COLOR] [COLOR="SeaGreen"]‘ton adresse[/COLOR]
.Subject = [COLOR="Red"]"Essai d’envoi feuille XL"[/COLOR]
.TextBody = [COLOR="Red"]"Quoi de neuf, docteur?"[/COLOR]
.AddAttachment TempFilePath & TempFileName & FileExtStr
.Send
End With
[COLOR="SeaGreen"] 'Delete the file you have send[/COLOR]
Kill TempFilePath & TempFileName & FileExtStr
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub