[RESOLVED] Attach sheet in email from workbook...
I have this code to send e-mail.
First i copy the sheet TEMPLATE as workbook from the active workbook with: Worksheets("TEMPLATE").Copy
(i think this is the first operation to attach the sheet into email, or not?)
and when the code go in: .Attachments.Add ActiveWorkbook have error?????
Code:
Sub INVIO_STATISTICA()
Dim OutApp As Object
Dim OutMail As Object
Dim TEMPNAME As String
Dim DATA As Date, WS_T As Worksheet
Dim DRIVER As String, DATA_SALVA As String
Dim GIORNO As String, MESE As String, ANNO As String
On Error GoTo errore
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
DATA = Format(CDate((Now)), "DD/MM/YYYY")
GIORNO = Left(DATA, 2)
MESE = Mid(DATA, 4, 2)
ANNO = Right(DATA, 4)
DATA_SALVA = GIORNO & "-" & MESE & "-" & ANNO
Worksheets("TEMPLATE").Copy
With OutMail
.To = "AAAAA"
.CC = ""
.BCC = ""
.Subject = "DDDDDDDD"
.HTMLBody = STRBODYTEXT_4
'.Body = ""
.Attachments.Add ActiveWorkbook
.Display
'.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing '
Application.ScreenUpdating = True
Exit Sub
errore:
MsgBox "Errore Numero: " & CStr(Err.Number) & vbCrLf & _
"Descrizione: " & Err.Description & vbCrLf & _
"Sorgente dell'Errore: " & Err.Source
Err.Clear
End Sub
Re: Attach sheet in email from workbook...
I have amended your code...and it works for me... Amend as possible
vb Code:
Sub INVIO_STATISTICA()
Dim OutApp As Object, OutMail As Object
Dim DATA_SALVA As String, sAttachment As String
On Error GoTo errore
'~~> Ensure that the workbook is saved
ActiveWorkbook.Save
sAttachment = ThisWorkbook.Path & "\" & ThisWorkbook.Name
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
'~~> Replace "/" by "-" in one go
DATA_SALVA = Replace(Format(CDate((Now)), "DD/MM/YYYY"), "/", "-")
'Worksheets("TEMPLATE").Copy
With OutMail
.CC = ""
.BCC = ""
.Subject = "DDDDDDDD"
.Attachments.Add sAttachment '<~~ Need to give entire path
.Display
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing '
Application.ScreenUpdating = True
Exit Sub
errore:
MsgBox "Errore Numero: " & CStr(Err.Number) & vbCrLf & _
"Descrizione: " & Err.Description & vbCrLf & _
"Sorgente dell'Errore: " & Err.Source
Err.Clear
End Sub
Re: Attach sheet in email from workbook...
Quote:
Originally Posted by
koolsid
I have amended your code...and it works for me... Amend as possible
vb Code:
Sub INVIO_STATISTICA()
Dim OutApp As Object, OutMail As Object
Dim DATA_SALVA As String, sAttachment As String
On Error GoTo errore
'~~> Ensure that the workbook is saved
ActiveWorkbook.Save
sAttachment = ThisWorkbook.Path & "\" & ThisWorkbook.Name
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
'~~> Replace "/" by "-" in one go
DATA_SALVA = Replace(Format(CDate((Now)), "DD/MM/YYYY"), "/", "-")
'Worksheets("TEMPLATE").Copy
With OutMail
.CC = ""
.BCC = ""
.Subject = "DDDDDDDD"
.Attachments.Add sAttachment '<~~ Need to give entire path
.Display
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing '
Application.ScreenUpdating = True
Exit Sub
errore:
MsgBox "Errore Numero: " & CStr(Err.Number) & vbCrLf & _
"Descrizione: " & Err.Description & vbCrLf & _
"Sorgente dell'Errore: " & Err.Source
Err.Clear
End Sub
OK NOW IT WORK!
but not possible to attach without save the wbook?
and tks for the suggestion: '~~> Replace "/" by "-" in one go
Re: Attach sheet in email from workbook...
Quote:
but not possible to attach without save the wbook?
If it is a newly created workbook then "no" as you need to specify path and filename to add as an attachment.