Results 1 to 4 of 4

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

  1. #1
    Frenzied Member
    Join Date
    Mar 05
    Posts
    1,071

    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
    Super Moderator koolsid's Avatar
    Join Date
    Feb 05
    Location
    Mumbai, India
    Posts
    11,406

    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 = "siddharthrout@liontel.com"
    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 koolsid; 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

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  3. #3
    Frenzied Member
    Join Date
    Mar 05
    Posts
    1,071

    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 = "siddharthrout@liontel.com"
    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
    Super Moderator koolsid's Avatar
    Join Date
    Feb 05
    Location
    Mumbai, India
    Posts
    11,406

    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

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •