+ Reply to Thread
Results 1 to 11 of 11

Modify VBA to send email with attachment

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Modify VBA to send email with attachment

    Hi,

    romperstomper wrote this nice piece of code for me which I adjusted slightly.

    ' adjust to whatever output folder you want
    Const mc_strOUTPUT_PATH As String = "F:\Reports\"
    Sub ReportPDF()
    Dim lngLastRow As Long
        Dim lngRow As Long
        Dim wksCase As Worksheet
        Dim wksReport As Worksheet
        Dim rngOutput As Range
        Dim varMatch
        
        Set wksCase = Sheets("CaseCounter")
        Set wksReport = Sheets("Report")
        
        Set rngOutput = Sheets("Output").Range("A2")
        
        With wksCase
            lngLastRow = .Cells(.Rows.Count, "S").End(xlUp).Row
            For lngRow = 2 To lngLastRow
                If .Cells(lngRow, "S").Value > 0 Then
                    ' populate feeder cell with value from column A
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("B:B"), 0)
                    If Not IsError(varMatch) Then
                        rngOutput.Value = Sheets("Sheet2").Cells(varMatch, "A").Value
                        Do While Application.CalculationState = xlCalculating
                            DoEvents
                        Loop
                        wksReport.ExportAsFixedFormat xlTypePDF, mc_strOUTPUT_PATH & rngOutput.Value & "-Jun12" & ".pdf"
                    ' send email here
                    Else
                        MsgBox "Cannot find person code " & .Cells(lngRow, "A").Value
                    End If
                End If
            Next lngRow
        End With
    End Sub
    Basically it will generate a report for anyone who has a 1 in the VBAtest column of the CaseCounter sheet

    I'm trying to get this to work with emailing now, which he has kindly built-in, but the problem I have is that I want the email nicely formatted too, rather than just 1 line of text, and I don't know how to do this.

    Subject line: Report June 2012
    Body:
    Dear Mr. NAME,
    
    Please find your personal report for the month of June attached.
    Should you have any questions, please let me know.
    XXXX
    YYYY
    ZZZZ
    
    Best wishes
    
    dip11
    ---
    EMAIL OUTLOOK SIGNATURE
    Note the part where it says NAME, that's the associated NAME value on Sheet2 (the same place you draw the CODE, NUMBER and EMAIL from)

    It has to send it to the EMAIL value associated to that same person, for whom the report was generated by the code.

    Thanks in advance
    Attached Files Attached Files
    Last edited by dip11; 07-04-2012 at 08:14 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Re: Modify VBA to send email with attachment

    b u m p

    I'm guessing it needs to be an adjustment of this:
    http://www.rondebruin.nl/pdf.htm

    Just couldn't get it to work the way I wanted
    Last edited by dip11; 07-06-2012 at 06:26 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Re: Modify VBA to send email with attachment

    OK I think I made some progress:


    ' adjust to whatever output folder you want
    Const mc_strOUTPUT_PATH As String = "F:\Reports\"
    
    Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, StrSubject As String, StrBody As String, Send As Boolean)
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .To = StrTo
            .CC = ""
            .BCC = ""
            .Subject = StrSubject
            .Body = StrBody
            .Attachments.Add FileNamePDF
            If Send = True Then
                .Send
            Else
                .Display
            End If
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Function
    
    Sub ReportPDF()
    Dim lngLastRow As Long
        Dim lngRow As Long
        Dim wksCase As Worksheet
        Dim wksReport As Worksheet
        Dim rngOutput As Range
        Dim varMatch
        Dim FileName As String
        Dim EAddress As String
        Dim PName As String
        
        Set wksCase = Sheets("CaseCounter")
        Set wksReport = Sheets("Report")
        
        Set rngOutput = Sheets("Output").Range("A2")
        
        With wksCase
            lngLastRow = .Cells(.Rows.Count, "S").End(xlUp).Row
            For lngRow = 2 To lngLastRow
                If .Cells(lngRow, "S").Value > 0 Then
                    ' populate feeder cell with value from column A
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("D:D"), 0)
                    If Not IsError(varMatch) Then
                        EAddress = Sheets("Sheet2").Cells(varMatch, "A").Value
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("C:C"), 0)
                    If Not IsError(varMatch) Then
                        PName = Sheets("Sheet2").Cells(varMatch, "A").Value
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("B:B"), 0)
                    If Not IsError(varMatch) Then
                        rngOutput.Value = Sheets("Sheet2").Cells(varMatch, "A").Value
                        Do While Application.CalculationState = xlCalculating
                            DoEvents
                        Loop
                        wksReport.ExportAsFixedFormat xlTypePDF, mc_strOUTPUT_PATH & rngOutput.Value & "-June12" & ".pdf"
                        FileName = rngOutput.Value & "-June12"
                    ' send email here
                        RDB_Mail_PDF_Outlook FileName, EAddress, "June 2012", _
                                 "Dear " & PName & "," & vbNewLine & "Please find your personal report for the month of June attached." & vbNewLine & _
                               "Best wishes" & vbNewLine & "dip11", False
                    Else
                        MsgBox "Cannot find person code " & .Cells(lngRow, "A").Value
                    End If
                End If
            Next lngRow
        End With
    End Sub
    But for some strange reason I now keeps throwing an error "Compile Error: Next without for"

    Considering it used to be happy with the Next without the added email code, what is causing this and how do I fix it?

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Modify VBA to send email with attachment

    You are missing 2 more end ifs before the Next lngRow.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Re: Modify VBA to send email with attachment

    Ahhhhh, thanks for that, didn't notice!

    Now this kind of works, except that it brings up the Outlook new message dialog without "To", "Name" and "Attachment"
    So essentially it is not using the EAddress, PName and FileName/FileNamePDF strings to fill those in and send the email :/

    Any ideas?

  6. #6
    Forum Contributor
    Join Date
    04-03-2011
    Location
    India
    MS-Off Ver
    Excel 2015
    Posts
    122

    Re: Modify VBA to send email with attachment

    its not converting to pdf ...

  7. #7
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Re: Modify VBA to send email with attachment

    It is converting to pdf just fine - I can see the pdfs being produced in the folder.

  8. #8
    Forum Contributor
    Join Date
    04-03-2011
    Location
    India
    MS-Off Ver
    Excel 2015
    Posts
    122

    Re: Modify VBA to send email with attachment

    Can u share the code how the reports are generating in pdf

  9. #9
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Re: Modify VBA to send email with attachment

    Can you be more specific? the code and workbook are all posted, minus the 2 end ifs that arlu spotted, which I have included below.
    ' adjust to whatever output folder you want
    Const mc_strOUTPUT_PATH As String = "F:\Reports\"
    
    Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, StrSubject As String, StrBody As String, Send As Boolean)
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .To = StrTo
            .CC = ""
            .BCC = ""
            .Subject = StrSubject
            .Body = StrBody
            .Attachments.Add FileNamePDF
            If Send = True Then
                .Send
            Else
                .Display
            End If
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Function
    
    Sub ReportPDF()
    Dim lngLastRow As Long
        Dim lngRow As Long
        Dim wksCase As Worksheet
        Dim wksReport As Worksheet
        Dim rngOutput As Range
        Dim varMatch
        Dim FileName As String
        Dim EAddress As String
        Dim PName As String
        
        Set wksCase = Sheets("CaseCounter")
        Set wksReport = Sheets("Report")
        
        Set rngOutput = Sheets("Output").Range("A2")
        
        With wksCase
            lngLastRow = .Cells(.Rows.Count, "S").End(xlUp).Row
            For lngRow = 2 To lngLastRow
                If .Cells(lngRow, "S").Value > 0 Then
                    ' populate feeder cell with value from column A
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("D:D"), 0)
                    If Not IsError(varMatch) Then
                        EAddress = Sheets("Sheet2").Cells(varMatch, "A").Value
                    End If
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("C:C"), 0)
                    If Not IsError(varMatch) Then
                        PName = Sheets("Sheet2").Cells(varMatch, "A").Value
                    End If
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("B:B"), 0)
                    If Not IsError(varMatch) Then
                        rngOutput.Value = Sheets("Sheet2").Cells(varMatch, "A").Value
                        Do While Application.CalculationState = xlCalculating
                            DoEvents
                        Loop
                        wksReport.ExportAsFixedFormat xlTypePDF, mc_strOUTPUT_PATH & rngOutput.Value & "-June12" & ".pdf"
                        FileName = rngOutput.Value & "-June12"
                    ' send email here
                        RDB_Mail_PDF_Outlook FileName, EAddress, "June 2012", _
                                 "Dear " & PName & "," & vbNewLine & "Please find your personal report for the month of June attached." & vbNewLine & _
                               "Best wishes" & vbNewLine & "dip11", False
                    Else
                        MsgBox "Cannot find person code " & .Cells(lngRow, "A").Value
                    End If
                End If
            Next lngRow
        End With
    End Sub
    Attached Files Attached Files
    Last edited by dip11; 07-08-2012 at 12:55 PM.

  10. #10
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Re: Modify VBA to send email with attachment

    I've managed to fix it myself.

    Turns out I was using application.match incorrectly and should have used range B:B and jsut mod the number after the comma. And for filename I just needed to add the filepath to the filename...

  11. #11
    Valued Forum Contributor
    Join Date
    06-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    341

    Re: Modify VBA to send email with attachment

    For some reason this doesn't work properly anymore even though I haven't changed anything (other than reboot my PC).

    It seems to use the rng value for PName and generate some random email address (I get bounce backs for failed senders) from my organization.

    ' adjust to whatever output folder you want
    Const mc_strOUTPUT_PATH As String = "F:\Reports\"
    
    Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, StrSubject As String, StrBody As String, Send As Boolean)
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .To = StrTo
            .CC = ""
            .BCC = ""
            .Subject = StrSubject
            .Body = StrBody
            .Attachments.Add FileNamePDF
            If Send = True Then
                .Send
            Else
                .Display
            End If
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Function
    
    Sub ReportPDF()
    Dim lngLastRow As Long
        Dim lngRow As Long
        Dim wksCase As Worksheet
        Dim wksReport As Worksheet
        Dim rngOutput As Range
        Dim varMatch
        Dim FileName As String
        Dim EAddress As String
        Dim PName As String
        
        Set wksCase = Sheets("CaseCounter")
        Set wksReport = Sheets("Report")
        
        Set rngOutput = Sheets("Output").Range("A2")
        
        With wksCase
            lngLastRow = .Cells(.Rows.Count, "S").End(xlUp).Row
            For lngRow = 2 To lngLastRow
                If .Cells(lngRow, "S").Value > 0 Then
                    ' populate feeder cell with value from column A
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("B:B"), 3)
                    If Not IsError(varMatch) Then
                        EAddress = Sheets("Sheet2").Cells(varMatch, "A").Value
                    End If
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("B:B"), 2)
                    If Not IsError(varMatch) Then
                        PName = Sheets("Sheet2").Cells(varMatch, "A").Value
                    End If
                    varMatch = Application.Match(.Cells(lngRow, "A").Value, Sheets("Sheet2").Range("B:B"), 0)
                    If Not IsError(varMatch) Then
                        rngOutput.Value = Sheets("Sheet2").Cells(varMatch, "A").Value
                        Do While Application.CalculationState = xlCalculating
                            DoEvents
                        Loop
                        wksReport.ExportAsFixedFormat xlTypePDF, mc_strOUTPUT_PATH & rngOutput.Value & "-June12" & ".pdf"
                        FileName = rngOutput.Value & "-June12"
                    ' send email here
                        RDB_Mail_PDF_Outlook FileName, EAddress, "June 2012", _
                                 "Dear " & PName & "," & vbNewLine & "Please find your personal report for the month of June attached." & vbNewLine & _
                               "Best wishes" & vbNewLine & "dip11", False
                    Else
                        MsgBox "Cannot find person code " & .Cells(lngRow, "A").Value
                    End If
                End If
            Next lngRow
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1