Previous
- 1
- 2
-
Re,
what do you mean by layout, the width of the columns
the height of the line
the coloring of certain cells or font
--
A+
Mike-31
A period of failure is a perfect time to sow the seeds of knowledge. -
-
Re,
paste this code in place of the other, don't forget to specify the constants
Const Dest As Variant
Const Exped As Variant
Const C_Ent As Variant
and this one if the range to copy changes
Const Plage As Variant
Sub Envoi_Mail ()
Dim cdoBasic
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb, Destwb As Workbook
Dim TempFilePath, TempFileName As String
Dim Wb, iMsg, iConf As Object
Dim Flds As Variant
'--------------------------- Constants to fill in
Const Feuille As Variant = "FORMULAIRE" '------------- name of the sheet to copy
Const Plage As Variant = "A1:H10" '------------------- range to copy, (Cells.Copy whole sheet)
Const Dest As Variant = "wwwwwwwwwww@free.fr" '- recipient email address
Const Exped As Variant = "www.xxxxxxx@free.fr" '------ reply email address of sender
Const C_Ent As Variant = "SMTP.free.fr" '------------- address of the SMTP (incoming mail)
Const CC As Variant = "" '---------------------------- CC email address
Const BCC As Variant = "" '--------------------------- email address for BCC or BCI or CCI
Const NumPort As Variant = 25 '----------------------- port number of the outgoing server
Const Nom_envoi As Variant = "Sourcewb.Name" '----------- identical name of the sent file or replace with "Desired name"
'--------------------------- If the connection requires authentication
Const N_Messag As Variant = "False" '----------------- Username for messaging, otherwise put "False"
Const Pass As Variant = "False" '--------------------- password otherwise put in False
'--------------------------- SSL connection like gmail and hotmail etc... set the constant to "True" otherwise "False"
Const Typ_Conex As Variant = "False"
'--------------------------- Start of procedure
On Error GoTo errorHandler
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
Set Destwb = Workbooks.Add
'-- If the range to copy contains links isolate the first row and free up the rows below
Sourcewb.Sheets(Feuille).Range(Plage).Copy
Destwb.ActiveSheet.[A1].PasteSpecial Paste:=xlPasteFormats '---- copy formats
Destwb.ActiveSheet.[A1].PasteSpecial Paste:=xlPasteValues '----- copy values
Destwb.ActiveSheet.[A1].Select
'--------------------------- Determine the version of Excel and the file extension used
With Destwb
If Val(Application.Version) < 12 Then
'--------------------------- Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'--------------------------- Excel 2007-2010
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog box"
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
TempFilePath = Environ$("temp") & "\"
'-------------------------- Name of the sent workbook with date and time of sending
TempFileName = Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
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 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = C_Ent '-------------- Enter the SMTP of the outgoing server e.g. "smtp.free.fr"
.Item("http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress") = Exped 'reply email address of sender
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = NumPort '-------- port number of the outgoing server
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = Typ_Conex '---------- Specific connection
'--------------------------- If the connection requires authentication, free the 3 lines
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'or
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = N_Messag ' Username for messaging otherwise False
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Pass ' password otherwise False
.Update
End With
With iMsg
Set .Configuration = iConf
.To = Dest
.CC = CC
.BCC = BCC
.From = Exped
.Subject = [C1].Value
.TextBody = "Hello" & " " & [C2].Value & "," & vbCrLf & vbCrLf _
& "Please find attached the " & [C3].Value & "." & vbCrLf & vbCrLf _
& [C4].Value & vbCrLf _
& [C5].Value & vbCrLf _
& [C6].Value & vbCrLf _
& [C7].Value & vbCrLf _
& [C8].Value & vbCrLf _
& [C9].Value & vbCrLf _
& [C10].Value & vbCrLf & vbCrLf _
& [C11]
.AddAttachment TempFilePath & TempFileName & FileExtStr
.Send
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "The email has been sent successfully!" '------------- Optional, confirmation of sending
Exit Sub
'--------------------------- If error we exit the procedure
errorHandler:
'--------------------------- Description of the error that occurred
MsgBox Err.Description
'--------------------------- If error close the temporary copy
For Each Wb In Workbooks
If Left(Wb.Name, 1) <> "Workbook" And Wb.Name <> ThisWorkbook.Name Then
Wb.Close
End If
Next Wb
End Sub
--
A+
Mike-31
A period of failure is a perfect time to sow the seeds of knowledge. -
Re
I made a test, I have a message that says it's impossible to paste merged cells of different sizes.
Initially, I have columns from A to AA with a column width of 3 to 4, when I receive the file pasted by email, and I open it, I have columns with a width of 10.38
I uploaded the example file online, it might be simpler and better than my explanations:
http://cjoint.com/12oc/BJwrFPNiCOb.htm
@+ -
-
Const Dest As Variant = "wwwwwwwwww@free.fr"
Const Exped As Variant = "www.xxxxxxx@free.fr"
Const C_Ent As Variant = "SMTP.free.fr"
Si ça bloque il faudra regarder dans ta messagerie le N° du port sortant mais je te dirais plus tard, normalement c'est bien le 25
Option Explicit
Sub Envoi_Mail()
Dim cdoBasic
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb, Destwb As Workbook
Dim TempFilePath, TempFileName As String
Dim Wb, iMsg, iConf As Object
Dim Flds As Variant
'--------------------------- Constante à renseigner
Const Feuille As Variant = "FORMULAIRE" '------------- nom de la feuille à cpier
Const Plage As Variant = "Cells" '------------------- plage à copier, (Cells.Copie toute la feuille)
Const Dest As Variant = "wwwwwwwwww@free.fr" '- adresse mail du destinataire
Const Exped As Variant = "www.xxxxxxx@free.fr" '------ adresse mail de réponse de l'expéditeur
Const C_Ent As Variant = "SMTP.free.fr" '------------- adresse du SMTP (courrier entrant)
Const CC As Variant = "" '---------------------------- adresse mail CC
Const BCC As Variant = "" '--------------------------- adresse mail destinataire pour envoi BCC ou BCI ou CCI
Const NumPort As Variant = 25 '----------------------- n° port du serveur sortant
Const Nom_envoi As Variant = "Sourcewb.Name" '----------- nom identique du fichier expédié ou remplacer par "Nom souhaité"
'--------------------------- Si la connexion nécessite une authentification
Const N_Messag As Variant = "False" '----------------- Nom utilisateur messagerie, sinon mettre en "False"
Const Pass As Variant = "False" '--------------------- motdepasse sinon mettre en Fase
'--------------------------- Connexion en SSL comme gmail et hotmail etc... mettre la constante en "True" sinon "False"
Const Typ_Conex As Variant = "False"
'--------------------------- Début de procédure
On Error GoTo errorHandler
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
Set Destwb = Workbooks.Add
'-- Si la plage à copier contient des liaisons isoler la première ligne et libérer les lignes au dessous
Sourcewb.Sheets(Feuille).Cells.Copy Destwb.ActiveSheet.[A1]
' Sourcewb.Sheets(Feuille).Range(Plage).Copy Destwb.ActiveSheet.[A1]
' Sourcewb.Sheets(Feuille).Range(Plage).Copy
' Destwb.ActiveSheet.[A1].PasteSpecial Paste:=xlPasteFormats '---- copie les format
' Destwb.ActiveSheet.[A1].PasteSpecial Paste:=xlPasteValues '----- copie les valeurs
' Destwb.ActiveSheet.[A1].PasteSpecial Paste:=xlPasteColumnWidths '---- copie les formats colonnes
' Destwb.ActiveSheet.[A1].Select
'--------------------------- Déterminer la version d'Excel et d'extension du fichier utilisé
With Destwb
If Val(Application.Version) < 12 Then
'--------------------------- Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'--------------------------- Excel 2007-2010
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Votre réponse est NON dans la boîte de dialogue de sécurité"
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
TempFilePath = Environ$("temp") & "\"
'-------------------------- Nom du classeur expédié avec jour et heure d'envoi
TempFileName = Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
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 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = C_Ent '-------------- Saisir le SMTP du serveur sortant ex."smtp.free.fr"
.Item("http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress") = Exped 'adresse email de réponse expéditeur
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = NumPort '-------- n° port du serveur sortant
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = Typ_Conex '---------- Connexion particulière
'--------------------------- Si la connexion nécessite une authentification libérer les 3 lignes
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'ou
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = N_Messag ' Nom utilisateur messagerie sinon False
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Pass ' motdepasse sinon False
.Update
End With
With iMsg
Set .Configuration = iConf
.To = Dest
.CC = CC
.BCC = BCC
.From = Exped
.Subject = [C1].Value
.TextBody = "Bonjour" & " " & [C2].Value & "," & vbCrLf & vbCrLf _
& "Veuillez trouvez ci-joint le " & [C3].Value & "." & vbCrLf & vbCrLf _
& [C4].Value & vbCrLf _
& [C5].Value & vbCrLf _
& [C6].Value & vbCrLf _
& [C7].Value & vbCrLf _
& [C8].Value & vbCrLf _
& [C9].Value & vbCrLf _
& [C10].Value & vbCrLf & vbCrLf _
& [C11]
.AddAttachment TempFilePath & TempFileName & FileExtStr
.Send
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Le mail a été bien envoyé !" '------------- Facultatif, confirmation de l'envoi
Exit Sub
'--------------------------- Si erreur on sort de la procédure
errorHandler:
'--------------------------- Description de l'erreur survenue
MsgBox Err.Description
'--------------------------- Si erreur ferme la copie temporaire
For Each Wb In Workbooks
If Left(Wb.Name, 1) <> "Claseur" And Wb.Name <> ThisWorkbook.Name Then
Wb.Close
End If
Next Wb
End Sub
--
A+
Mike-31
Une période d'échec est un moment rêvé pour semer les graines du savoir. -
First of all, thank you to the people who explained and provided this macro.
It works very well for me, but instead of sending the workbook in .xls format, I would like to send it in PDF format. I tried replacing .xls with .pdf, but the person receiving the document cannot open it. So I would like to know how to proceed.
Additionally, I would also like to save the sent sheet (still as a PDF) in the folder "c: ....." to keep a record of my sendings.
Last request, and I'm sorry to be so demanding, the sheet I'm creating is a rent receipt. I can set the sending date automatically, but I would like to be able to write this in one or more cells automatically, like the date, example: period from xxx to xxx. The "xxx" will be replaced by the period covered by the receipt, which is from March 1, 2013, to March 31, 2013. It is therefore necessary to manage whether the month has 30 days, 31 days, or 28 days for February.
Thank you in advance for your insights.
Previous
- 1
- 2