Results 1 to 4 of 4

Thread: [RESOLVED] Attach sheet in email from workbook...

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,934

    Resolved [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

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Attach sheet in email from workbook...

    I have amended your code...and it works for me... Amend as possible

    vb Code:
    1. Sub INVIO_STATISTICA()
    2.     Dim OutApp As Object, OutMail As Object
    3.     Dim DATA_SALVA As String, sAttachment As String
    4.    
    5.     On Error GoTo errore
    6.    
    7.     '~~> Ensure that the workbook is saved
    8.     ActiveWorkbook.Save
    9.     sAttachment = ThisWorkbook.Path & "\" & ThisWorkbook.Name
    10.  
    11.     Application.ScreenUpdating = False
    12.  
    13.     Set OutApp = CreateObject("Outlook.Application")
    14.     OutApp.Session.Logon
    15.     Set OutMail = OutApp.CreateItem(0)
    16.  
    17.     '~~> Replace "/" by "-" in one go
    18.     DATA_SALVA = Replace(Format(CDate((Now)), "DD/MM/YYYY"), "/", "-")
    19.    
    20.     'Worksheets("TEMPLATE").Copy
    21.  
    22.     With OutMail
    23.         .To = "[email protected]"
    24.         .CC = ""
    25.         .BCC = ""
    26.         .Subject = "DDDDDDDD"
    27.         .Attachments.Add sAttachment '<~~ Need to give entire path
    28.         .Display
    29.         .Send
    30.     End With
    31.  
    32.     Set OutMail = Nothing
    33.     Set OutApp = Nothing    '
    34.  
    35.     Application.ScreenUpdating = True
    36.  
    37.     Exit Sub
    38.  
    39. errore:
    40.     MsgBox "Errore Numero: " & CStr(Err.Number) & vbCrLf & _
    41.            "Descrizione: " & Err.Description & vbCrLf & _
    42.            "Sorgente dell'Errore: " & Err.Source
    43.     Err.Clear
    44. 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

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,934

    Re: Attach sheet in email from workbook...

    Quote Originally Posted by koolsid View Post
    I have amended your code...and it works for me... Amend as possible

    vb Code:
    1. Sub INVIO_STATISTICA()
    2.     Dim OutApp As Object, OutMail As Object
    3.     Dim DATA_SALVA As String, sAttachment As String
    4.    
    5.     On Error GoTo errore
    6.    
    7.     '~~> Ensure that the workbook is saved
    8.     ActiveWorkbook.Save
    9.     sAttachment = ThisWorkbook.Path & "\" & ThisWorkbook.Name
    10.  
    11.     Application.ScreenUpdating = False
    12.  
    13.     Set OutApp = CreateObject("Outlook.Application")
    14.     OutApp.Session.Logon
    15.     Set OutMail = OutApp.CreateItem(0)
    16.  
    17.     '~~> Replace "/" by "-" in one go
    18.     DATA_SALVA = Replace(Format(CDate((Now)), "DD/MM/YYYY"), "/", "-")
    19.    
    20.     'Worksheets("TEMPLATE").Copy
    21.  
    22.     With OutMail
    23.         .To = "[email protected]"
    24.         .CC = ""
    25.         .BCC = ""
    26.         .Subject = "DDDDDDDD"
    27.         .Attachments.Add sAttachment '<~~ Need to give entire path
    28.         .Display
    29.         .Send
    30.     End With
    31.  
    32.     Set OutMail = Nothing
    33.     Set OutApp = Nothing    '
    34.  
    35.     Application.ScreenUpdating = True
    36.  
    37.     Exit Sub
    38.  
    39. errore:
    40.     MsgBox "Errore Numero: " & CStr(Err.Number) & vbCrLf & _
    41.            "Descrizione: " & Err.Description & vbCrLf & _
    42.            "Sorgente dell'Errore: " & Err.Source
    43.     Err.Clear
    44. 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

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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
  •  



Click Here to Expand Forum to Full Width