Private Sub CommandButton1_Click()
Dim WIn As Worksheet
Dim destinatario As String
Dim ws As Worksheet
Dim TestoEmail As String
TestoEmail = [C4]
Dim i As Integer
Dim j As Integer
Set WIn = ThisWorkbook.Worksheets("Richiesta") ' determino il foglio di lavoro
Set OutApp = CreateObject("Outlook.Application") ' determino il sistema di posta
ThisWorkbook.Activate
NomeFile = Format(Now(), "yyyy_mm_dd") & " _ " & WIn.Cells(11, 1) & " _ " & WIn.Cells(11, 2).Value ' determino il nome del file
ThisWorkbook.Windows(1).SelectedSheets.Copy ' copio il foglio attuale
ActiveWorkbook.ConflictResolution = xlLocalSessionChanges
ActiveWorkbook.Activate
Range(Cells(1, 1), (Cells(7, 15))).Select ' seleziono le celle che non mi servono
Selection.Delete ' le cancello
For Each ws In Worksheets
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Cells(1, 1).Select
Application.CutCopyMode = False
Next ws
ActiveWorkbook.SaveAs Filename:=WIn.Cells(2, 2).Value & NomeFile & ".xls", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False, ConflictResolution:=xlLocalSessionChanges ' salvo il file creato con il nome
Workbooks(NomeFile & ".xls").Close ' chiudo il file creato
' invio email
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = ComboBox1 ' A chi la mando
.CC = ComboBox2 ' CC copia in lettura
.Subject = NomeFile 'Oggetto della mail
.Body = TestoEmail ' testo della mail
.Attachments.Add (WIn.Cells(2, 2).Value & NomeFile & ".xls") 'da qui prendo l'allegato il percorso ed il nome del file
.Send 'per inviare subito la mail
'.Display 'per aprire e controllare la mail prima di inviarla manualmente
End With
Unload Me
MsgBox "Mail inviata con successo", vbInformation, "Avviso"
' end email
Set OutMail = Nothing
Set OutApp = Nothing
' cancella dati e chiudi
Range(Cells(28, 14), (Cells(17, 1))).Value = ""
Cells(12, 2).Value = ""
Cells(11, 2) = Cells(11, 2) + 1 ' incrementa numero di file
ActiveWorkbook.Save
Application.Quit
End Sub |