Macro for printing same form multiple times in pdf

VTirolla

New Member
Joined
Mar 5, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello!

I am working in a tool to generate reports. I have created a form in one worksheet "Report" and when a change a cell values related to the tag, this form is be populated with the information related to that tag number.
I created the following code, but after the macro finish running, the pdf files are not saved anywhere.

NrTags is the total amount of reports that shall be printed.

Sub PrintWFCReport()
'
' PrintWFCReport Macro
' Prints the WFC Report for all Tags in .pdf file
'
Application.Calculation = xlAutomatic
Dim NrTags As Integer
Dim i As Integer
NrTags = Worksheets("Configurator").Range("B12").Value

Do
Worksheets("Report").Range("L3").Value = i
i = i + 1
Sheets("Report").Range("A1:I53").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\" & (Environ$("Username")) & "\Desktop\WFC Report - " & Sheets("Figures").Range("B3").Value _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False

Loop Until Worksheets("Report").Range("L3").Value = NrTags

Worksheets("Report").Range("L3").Value = 0
'
End Sub


The way this code should work is that i would get one report per tag. Also, if you can advise me in how to print all the reports in a single .pdf file it would be amazing!

Thanks!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The way this code should work is that i would get one report per tag. Also, if you can advise me in how to print all the reports in a single .pdf file it would be amazing!
Try this macro.
VBA Code:
Public Sub Create_WFC_Report()

    Dim PDFfullName As String
    Dim PDFsheet As Worksheet
    Dim reportDestCell As Range, reportRange As Range
    Dim NrTags As Integer
    Dim i As Integer
    
    PDFfullName = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\WFC Report.pdf"
   
    With ActiveWorkbook
   
        'Add temporary sheet for PDF output
       
        Set PDFsheet = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
        Set reportDestCell = PDFsheet.Range("A1")
       
        'Range to be copied to temporary sheet for each report value
       
        Set reportRange = .Worksheets("Report").Range("A1:I53")
       
    End With
    
    NrTags = Worksheets("Configurator").Range("B12").Value
    i = 0
    Do
        Worksheets("Report").Range("L3").Value = i
        i = i + 1
        
        'Copy cell formats, column widths, the picture and cell values to next cell in temporary PDF sheet
       
        reportRange.Copy
        reportDestCell.Select
        reportDestCell.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        reportDestCell.Worksheet.Paste
        reportDestCell.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
   
        'Use Format Painter to apply row heights
       
        reportRange.EntireRow.Copy
        With PDFsheet
            .Range(reportDestCell, .Cells(.UsedRange.Rows.Count, 1)).EntireRow.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        End With
        
        'Add page break and update destination cell for next report
       
        With PDFsheet
            .HPageBreaks.Add Before:=.Rows(.UsedRange.Rows.Count + 1)
            Set reportDestCell = .Cells(.UsedRange.Rows.Count + 1, 1)
        End With
        
    Loop Until Worksheets("Report").Range("L3").Value = NrTags
    
    Worksheets("Report").Range("L3").Value = 0
       
    'Save temporary sheet as PDF then delete it
   
    With PDFsheet
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfullName, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Application.DisplayAlerts = False
        .Delete
        Application.DisplayAlerts = True
    End With
    
    MsgBox "Created " & PDFfullName, vbInformation
   
End Sub
 
Upvote 0
Try this macro.
VBA Code:
Public Sub Create_WFC_Report()

    Dim PDFfullName As String
    Dim PDFsheet As Worksheet
    Dim reportDestCell As Range, reportRange As Range
    Dim NrTags As Integer
    Dim i As Integer
   
    PDFfullName = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\WFC Report.pdf"
  
    With ActiveWorkbook
  
        'Add temporary sheet for PDF output
      
        Set PDFsheet = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
        Set reportDestCell = PDFsheet.Range("A1")
      
        'Range to be copied to temporary sheet for each report value
      
        Set reportRange = .Worksheets("Report").Range("A1:I53")
      
    End With
   
    NrTags = Worksheets("Configurator").Range("B12").Value
    i = 0
    Do
        Worksheets("Report").Range("L3").Value = i
        i = i + 1
       
        'Copy cell formats, column widths, the picture and cell values to next cell in temporary PDF sheet
      
        reportRange.Copy
        reportDestCell.Select
        reportDestCell.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        reportDestCell.Worksheet.Paste
        reportDestCell.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
  
        'Use Format Painter to apply row heights
      
        reportRange.EntireRow.Copy
        With PDFsheet
            .Range(reportDestCell, .Cells(.UsedRange.Rows.Count, 1)).EntireRow.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        End With
       
        'Add page break and update destination cell for next report
      
        With PDFsheet
            .HPageBreaks.Add Before:=.Rows(.UsedRange.Rows.Count + 1)
            Set reportDestCell = .Cells(.UsedRange.Rows.Count + 1, 1)
        End With
       
    Loop Until Worksheets("Report").Range("L3").Value = NrTags
   
    Worksheets("Report").Range("L3").Value = 0
      
    'Save temporary sheet as PDF then delete it
  
    With PDFsheet
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfullName, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Application.DisplayAlerts = False
        .Delete
        Application.DisplayAlerts = True
    End With
   
    MsgBox "Created " & PDFfullName, vbInformation
  
End Sub
Thanks John, appreciate the help!
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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