|
-
Sep 8th, 2009, 09:44 AM
#1
Thread Starter
PowerPoster
[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
-
Sep 8th, 2009, 10:04 AM
#2
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
Last edited by Siddharth Rout; Sep 8th, 2009 at 10:07 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 8th, 2009, 10:27 AM
#3
Thread Starter
PowerPoster
Re: Attach sheet in email from workbook...
 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
-
Sep 8th, 2009, 10:41 AM
#4
Re: Attach sheet in email from workbook...
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.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|