Email PDF: change vba code from multiple worksheets to one template worksheet

DUI

Board Regular
Joined
Jul 3, 2006
Messages
109
Hi Everyone,
We are redesigning our "Purchase Order generator" to work from one worksheet instead on multiple worksheets. Currently, we have 90 worksheets feeding from a MasterPage.
The below code runs through each worksheet and creates a PDF and attaches it to an email ready to send.
Code:
Sub Email_PDF_PO_TEMPLATE()
'Working only in 2007 and up
    Dim sh As Worksheet
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileName As String

    'Temporary path to save the PDF files
    TempFilePath = Range("Save_Path").Text
    
    'Loop through every worksheet
    For Each sh In ThisWorkbook.Worksheets
        FileName = ""



        'Test C14 for a mail address
        If sh.Range("C14").Value Like "?*@?*.?*" Then
            

                'If there is a mail address in C14 create the file name and the PDF
                TempFileName = TempFilePath & sh.Name & " " _
                            & sh.Range("H5").Value & ".pdf"

                    FileName = RDB_Create_PDF(sh, TempFileName, True, False)


                'If publishing is OK create the mail
                If FileName <> "" Then
                    RDB_Mail_PDF_Outlook FileName, sh.Range("C14").Value, "Purchase Order", _
                                         "<font size=""2"" face=""Calibri"">" & "Please find attached the Purchase Order: " & sh.Name & " " & sh.Range("H5").Value, False


                Else
                    MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
                           "Microsoft Add-in is not installed" & vbNewLine & _
                           "The path to Save the file is not correct" & vbNewLine & _
                           "You didn't want to overwrite the existing PDF if it exists"
                End If
            End If

    Next sh
End Sub
I have redesigned a one page template using INDEX,MATCH, & VLOOKUPS, to replace all 90 woorksheets, but I'm now stuck on how to modify the above code to cycle through the template.
The Template uses the PO_Number to populate the sheet.
I have created a list on the sheet of all the PO_Numbers using the Advanced Filter unique values from the MasterPage... not sure if this is needed.

From the MasterPage, I can double click the PO_Number to populate the TEMPLATE
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If Not Intersect(Target, Range("PO_Column")) Is Nothing Then

    If Sheets("BOQ").Range("AY1") = "Purchase Order Summary" Then

        If ActiveCell.Value = "Generate PDFs" Then
            MsgBox "This will generate the PDFs once the code is linked"
            Range("A1").Select
            Exit Sub
        
        
        
        ElseIf ActiveCell.Value = isblank Then
            MsgBox "You must insert a 'Save Path' first"
            ActiveCell.Offset(0, 2).Select
            Exit Sub

        End If
    
    Sheets("PO TEMPLATE").Range("PO_Number").Value = ActiveCell.Value
    Sheets("PO TEMPLATE").Activate
    
    End If

End If
End Sub

Can I use the "Generate PDFs" Double Click event to cycle the ActiveCell down the column running the "Email_PDF_PO_TEMPLATE()" macro until it reaches another "Generate PDFs" value or BLANK and then Exit Sub?

When the MasterPage is set to "Purchase Order Summary", the table is filtered to show only the PO_Numbers for each stage in the PO_Column.
For Example:

Generate PDFs
1.1.1
1.1.2
1.1.3
Generate PDFs
1.2.1
1.2.5
1.2.6
(Cell blank) - no Save Path set
1.3.1
1.3.3

The 'Save Path' is one cell to the right of the "Generate PDFs". The "Generate PDFs" will not display if there is no value in the 'Save Path'
Sorry for such a long post... I hope this makes sense.
Any help would be greatly appreciated.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Just incase anyone finds this post, I have finally worked it out...
Any recommended improvements would be much appreciated.

The first section works down the MasterPage
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If Not Intersect(Target, Range("PO_Column")) Is Nothing Then

    If Sheets("BOQ").Range("AY1") = "Purchase Order Summary" Then
    Application.ScreenUpdating = False

        If ActiveCell.Value = "Generate PDFs" Then
            Dim TempFilePath As String
            
            'Temporary path to save the PDF files needs to be done now before the ActiveCell changes location
            TempFilePath = ActiveCell.Offset(0, 2).Text
        
            'Select next VISIBLE cell down from the "Generate PDFs" cell to start with the first Purchase Order
            ActiveCell.Offset(1, 0).Select
                Do Until ActiveCell.EntireRow.Hidden = False
                ActiveCell.Offset(1, 0).Select
                Loop
            
            Application.Run "Email_PDF"
            Exit Sub
            
        ElseIf ActiveCell.Value = isblank Then
            MsgBox "You must insert a 'Save Path' first"
            ActiveCell.Offset(0, 2).Select
            Exit Sub
    
        End If
    
    'ActiveCell must be a Purchase Order number therefore insert number and goto TEMPLATE
    Sheets("PO TEMPLATE").Range("PO_Number").Value = ActiveCell.Value
    Sheets("PO TEMPLATE").Activate
    
    End If

End If

Application.ScreenUpdating = True
End Sub

The second section creates the PDFs and attaches them to emails
Code:
Private Sub Email_PDF()
Application.ScreenUpdating = False

'Generate each Purchase Order down the column until the next stage is reached
Do Until ActiveCell = isblank Or ActiveCell = "Generate PDFs"

    'Update PO Template with each Purchase Order number as it moves down the column
    Sheets("PO TEMPLATE").Range("PO_Number").Value = ActiveCell.Value
            
        'Test for a mail address
        If Not Sheets("PO TEMPLATE").Range("PO_Email").Value Like "?*@?*.?*" Then
            MsgBox "Purchase Order " & Sheets("PO TEMPLATE").Range("PO_Number").Value & _
            " could not be created because the Email address is not valid."
        Else
            
            Dim TempFileName As String
            Dim FileName As String

            TempFileName = TempFilePath & "\" & Sheets("PO TEMPLATE").Range("PO_Number").Value & ".pdf"

            FileName = RDB_Create_PDF(Source:=Sheets("PO TEMPLATE").Range("D5:L97"), _
                                    FixedFilePathName:=TempFileName, _
                                    OverwriteIfFileExist:=True, _
                                    OpenPDFAfterPublish:=False)

                If FileName <> "" Then
                    RDB_Mail_PDF_Outlook FileNamePDF:=FileName, _
                                     StrTo:=Sheets("PO TEMPLATE").Range("PO_Email").Value, _
                                     StrCC:="", _
                                     StrBCC:="", _
                                     StrSubject:="Purchase Order" & " " & Sheets("PO TEMPLATE").Range("PO_Number").Value, _
                                     Signature:=True, _
                                     Send:=False, _
                                     StrBody:="<br>" & _
                                              "******>Please find attached Purchase Order" & " " & _
                                              Sheets("PO TEMPLATE").Range("PO_Number").Value & " for the " & _
                                              Sheets("PO TEMPLATE").Range("G17").Value
                    
                    'insert today's date in the "Date Updated" column
                    ActiveCell.Offset(0, 1).Value = Date
                
                Else
                    MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
                           "Microsoft Add-in is not installed" & vbNewLine & _
                           "The path to Save the file is not valid"
                End If

        End If

    'Move down the "PO Number" Column to the next visible cell
    ActiveCell.Offset(1, 0).Select
        Do Until ActiveCell.EntireRow.Hidden = False
        ActiveCell.Offset(1, 0).Select
        Loop
Loop

Application.ScreenUpdating = True
End Sub

A massive thanks to Ron de Bruin at Create and Mail PDF files with Excel 2007/2016
Without his work, I would never have got this going.
 
Upvote 0

Forum statistics

Threads
1,215,676
Messages
6,126,168
Members
449,296
Latest member
tinneytwin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top