Automatically create PDF-invoices with Excel

bjornbjorn

New Member
Joined
Jan 14, 2018
Messages
1
Hi,

I am looking for a way to automatically create PDF invoices using Excel. I have a standardized excel-sheet with customer information and I'd like to be able to push a button and the customer information to be gathered into an invoice template that I've created. I.e. for each line of customer information a new PDF-invoice should be created. Does anyone have any ideas how to create this? I assume I need to create a macro to be able to create one invoice per customer.

Any help is appreciated.

Thanks,
Björn
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I'm sorry you said you wanted to skip if the Invoice amount is blank. What column is the Invoice amount in?
 
Upvote 0
suresh,

code has been updated to not include a row if either Column F or H are blank.
For your last request to only export new records you will need to let me know how to determine if a record is new. Is there a date field or something like that I could key on to make that determination?

Code:
Option Explicit


Sub InvoiceGeneration()


Dim cfws, ctws As Worksheet
Dim lastrow, i As Long
Dim fileloc, filename, Fname As String


Set cfws = Worksheets("Raw-Data")
lastrow = cfws.Cells(cfws.Rows.Count, "B").End(xlUp).Row + 1
fileloc = "C:\Test\"


For i = 1 To lastrow
    If cfws.Range("L" & i).Value = vbNullString Then
        Set ctws = Worksheets("Tax Invoice-2")
    Else
        Set ctws = Worksheets("Tax Invoice-1")
    End If
    If cfws.Range("F" & i).Value <> vbNullString And cfws.Range("H" & i).Value <> vbNullString Then
        filename = "Invoice -" & cfws.Range("B" & i).Value
        ctws.Range("A9").Value = "Invoice No : " & cfws.Range("B" & i).Value
        ctws.Range("B23").Value = cfws.Range("H" & i).Value
        Fname = fileloc & filename & ".pdf"
        cfws.Hyperlinks.Add Anchor:=cfws.Range("M" & i), Address:=Fname, TextToDisplay:=filename
            
        With ctws
            .ExportAsFixedFormat Type:=xlTypePDF, filename:=Fname
        End With
    End If
    
Next i
        
End Sub
 
Upvote 0
frank_AL,

The below line creates hyperlinks in raw-data sheet. so, is it possible to run the macro for the cells in raw-data sheet which don't have hyperlinks( i.e new records)?

Code:
cfws.Hyperlinks.Add Anchor:=cfws.Range("M" & i), Address:=Fname, TextToDisplay:=filename

Thanks
 
Upvote 0
suresh,

The code now works on when Column F and H are not blank and M is blank.

Code:
Option Explicit


Sub InvoiceGeneration()


Dim cfws, ctws As Worksheet
Dim lastrow, i As Long
Dim fileloc, filename, Fname As String


Set cfws = Worksheets("Raw-Data")
lastrow = cfws.Cells(cfws.Rows.Count, "B").End(xlUp).Row + 1
fileloc = "C:\Test\"


For i = 1 To lastrow
    If cfws.Range("L" & i).Value = vbNullString Then
        Set ctws = Worksheets("Tax Invoice-2")
    Else
        Set ctws = Worksheets("Tax Invoice-1")
    End If
    If cfws.Range("F" & i).Value <> vbNullString And cfws.Range("H" & i).Value <> vbNullString And cfws.Range("M" & i) = vbNullString Then
        filename = "Invoice -" & cfws.Range("B" & i).Value
        ctws.Range("A9").Value = "Invoice No : " & cfws.Range("B" & i).Value
        ctws.Range("B23").Value = cfws.Range("H" & i).Value
        Fname = fileloc & filename & ".pdf"
        cfws.Hyperlinks.Add Anchor:=cfws.Range("M" & i), Address:=Fname, TextToDisplay:=filename
            
        With ctws
            .ExportAsFixedFormat Type:=xlTypePDF, filename:=Fname
        End With
    End If
    
Next i
        
End Sub
 
Upvote 0
I've been using something similar to this to generate PDFs of invoices for my customers but I'd like to add a step after the pdf is created where the pdf is sent as an email attachment to an address also on the data worksheet. Can anyone help with this please? Thanks
 
Upvote 0
hell im new here, if i implement this into my raw data + invoice, i have 200+ rows of enteries that i need invoices for, will this create one for each one? or would i need to do this manually?
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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