Print to PDF Macro, or possibly more

Cliffork

New Member
Joined
Feb 12, 2020
Messages
43
Office Version
  1. 365
I have a compilation file that we fill in calibration forms for a device on. There are a handful of sheets in the workbook, but every since one has a sheet named "Cal Form" that I am trying to automate printing a PDF of, with the name being pulled from two cells in said sheet (C8 and K8).

If possible I'm also trying to find some way to print PDF's of dozens of existing Cal Forms as my customers request them all the time, and getting them is tedious. I'm not sure what the best way to do this would be.
I have hundreds of these files and it would honestly be convenient to just be able to print every single one to PDF automatically somehow.

I've tried searching the forums and just googling but it seems everyone is trying to do something differently and I've tried 2 so far with no luck on my end. I've also tried recording a macro of saving it as a PDF but it doesn't appear to function.

Code for my recorded macro that doesn't seem to work is
VBA Code:
Sub PrintToPDF()
'
' PrintToPDF Macro
'
' Keyboard Shortcut: Ctrl+q
'
    ChDir "Z:\Shared\EEW\Lab\Cal Cert PDF"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Z:\Shared\EEW\Lab\Compilation Documents\Envision\Meter Repair Compilation File -Template.pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub


Sheet is below in case it helps.

Meter Repair Compilation File -TemplateTEST.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
2Testing and Calibration Report
3
4
7
8Date:10/29/2024Meter SN#: 1234567BModel #:ENV200RMA#:0500-1234
9Lab Testing Performed
10 Bluetooth Battery Charger Available Pressure CH4 Sensor H2S Sensor
11 Firmware Battery health Impact Pressure CO2 Sensor CO Sensor
12 Thermistor Static Pressure Seal Integrity O2 Sensor H2 Sensor
13
14Calibration Results:
15GasTarget Value 1Analyzer Value 1Target Value 2Analyzer Value 2Calibration Gas Bottle Lot#Analytical Uncertainty
16A20.9% O220.9% O20.00--32-401209938-1± 0.02% abs
17B11% O211% O20.00--32-401919744-1± 0.02% abs
18C50% CH435% CO250% CH40.0035% CO20.00141-403023316-1± 0.02% abs
19D15% CH415% CO215% CH40.0015% CO20.00141-402762057-1± 2% rel
20E40% CH460% CO240% CH40.0060% CO20.00141-403007833-1± 1% rel
21F100% CO2--100% CO20.0025-401897982-1- 0.01% abs
22G100% CH4100% CH40.00--141-402357134-1- 0.5% abs
27
28Zero Gas ValueCH4CO2O2H2H2SCO
29L99.999% N20.000.000.000.000.000.00
30Calibration Gas directly traceable to NIST ASTM Class 1 weights and/or NIST gas mixture reference materials
31Pressure Sensors: (All measurements made in"WC)
32Low PressuresHigh Pressures
33SensorTarget Value 1Analyzer Value 1Target Value 2Analyzer Value 2SensorTarget Value 1Analyzer Value 1Target Value 2Analyzer Value 2
34Applied0.0000.00-4.500.00Available0.0000.00-100.000.00
35Diff.0.0000.0004.5000.000Diff.0.0000.0020.0000.00
36Applied0.0000.000-100.000.000
37This device has passed testing and calibration procedures as specified by Elkins Earthworks, LLC.
38
39Calibrated by (Signature):KC
40
41At Elkins Earthworks we appreciate your business. We strive for a standard 10 working day turn arround on all calibrations and repairs from the time we have received your instrument. Your instrument has been serviced in 32559 days. We hope you appreciate our dedication to service and quality.
42
43
44Thanks,Notes as to why service exceeded 10 day turn
45
46
47
48
49President
50Elkins Earthworks, LLC.
51
52
53
54
Cal Form
Cell Formulas
RangeFormula
K8K8=('Customer Info'!B15)
R8R8='Customer Info'!B16
Y8Y8=('Customer Info'!B13)
B10B10=IF(ISNUMBER(SEARCH("X",'Final Insp.'!A13)),"P", "")
B11B11=IF(ISNUMBER(SEARCH("X",'Final Insp.'!A11)),"P", "")
B12B12=IF(ISNUMBER(SEARCH("X",'Final Insp.'!A68)),"P", "")
H10H10=IF(ISNUMBER(SEARCH("X",'Final Insp.'!A10)),"P", "")
H11H11=IF(ISNUMBER(SEARCH("",'Final Insp.'!A28)),"P", "")
H12H12=IF(ISNUMBER(SEARCH("",'Final Insp.'!A28)),"P", "")
N10N10=IF(ISNUMBER(SEARCH("",'Final Insp.'!A28)),"P", "")
N11N11=IF(ISNUMBER(SEARCH("",'Final Insp.'!A28)),"P", "")
N12N12=IF(ISNUMBER(SEARCH("X",'Final Insp.'!A52)),"P", "")
V10V10=IF(ISNUMBER(SEARCH("",'Final Insp.'!A53)),"P", "")
V11V11=IF(ISNUMBER(SEARCH("",'Final Insp.'!A53)),"P", "")
V12V12=IF(ISNUMBER(SEARCH("",'Final Insp.'!A53)),"P", "")
AA10AA10=IF(ISNUMBER(SEARCH("",'Final Insp.'!A53)),"P", "")
AA11AA11=IF(ISNUMBER(SEARCH("",'Final Insp.'!A53)),"P", "")
AA12AA12=IF(ISNUMBER(SEARCH("",'Final Insp.'!A53)),"P", "")
K16K16='Final Insp.'!F35
K17K17='Final Insp.'!F42
K18:K20,K22K18='Final Insp.'!D37
Q18:Q21Q18='Final Insp.'!E37
H29H29='Final Insp.'!D36
L29L29='Final Insp.'!E36
P29P29='Final Insp.'!F36
T29T29='Final Insp.'!D47
X29X29='Final Insp.'!F48
AB29AB29='Final Insp.'!E46
G34:G35G34='Final Insp.'!D25
M34:M35M34='Final Insp.'!I25
W34W34='Final Insp.'!E24
W35W35='Final Insp.'!E26
W36W36='Final Insp.'!E25
AC34AC34='Final Insp.'!J24
AC35AC35='Final Insp.'!J26
AC36AC36='Final Insp.'!J25
A41A41="At Elkins Earthworks we appreciate your business. We strive for a standard 10 working day turn arround on all calibrations and repairs from the time we have received your instrument. Your instrument has been serviced in " & NETWORKDAYS('Customer Info'!B14,C8,'Drop Down lists'!M2:M105)-1 & " days. We hope you appreciate our dedication to service and quality."
Cells with Conditional Formatting
CellConditionCell FormatStop If True
AA10:AA12Expression=RIGHT(UPPER('Customer Info'!$B$15),1)="D"textNO
T28:AE29Expression=RIGHT(UPPER('Customer Info'!$B$15),1)="D"textNO
T28:AE28Expression=RIGHT(UPPER('Customer Info'!$B$15),1)="D"textNO
AA10:AE12,T28:AE29Expression=RIGHT(UPPER('Customer Info'!$B$15),1)="D"textNO
I39Cellcontains a blank value textNO
C8Cellcontains a blank value textNO
Cells with Data Validation
CellAllowCriteria
I39:Z39List='Drop Down lists'!$A$1:$A$6
 

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)
Update: I got it to partially work recording it differently. Now I have the code below. I can open this file and run the macro in any file I want to make a PDF for, but it keeps the name as the original file name for some reason. If I could make the name pull from C8/K8 I think I'd be mostly set(unless someone can help me think of a way to export all my files this way in one fell swoop!)

VBA Code:
Sub exportToPDF()
'
' exportToPDF Macro
'
' Keyboard Shortcut: Ctrl+w
'
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Z:\Shared\EEW\Lab\Cal Cert PDF\Meter Repair Compilation File -Template", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub
 
Last edited:
Upvote 0
I made a little more slight progress, I made the file name save as the serial number, but I still need to figure out how to make it save as the serial number and the date it was created so I can create one for multiple years without needing to rename them.

VBA Code:
Sub exportToPDF()
'
' exportToPDF Macro
'
' Keyboard Shortcut: Ctrl+q
'
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Z:\Shared\EEW\Lab\Cal Cert PDF\" & Range("K8").Value, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub
 
Upvote 0
Change the folder path to where all your files are located (in red) and the file extension of those files (in blue) to suit your needs.
Rich (BB code):
Sub exportToPDF()
    Application.ScreenUpdating = False
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Test\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource.ActiveSheet
                .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                "Z:\Shared\EEW\Lab\Cal Cert PDF\" & .Range("C8") & " " & .Range("K8"), _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
                :=False, OpenAfterPublish:=False
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Change the folder path to where all your files are located (in red) and the file extension of those files (in blue) to suit your needs.
Rich (BB code):
Sub exportToPDF()
    Application.ScreenUpdating = False
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Test\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource.ActiveSheet
                .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                "Z:\Shared\EEW\Lab\Cal Cert PDF\" & .Range("C8") & " " & .Range("K8"), _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
                :=False, OpenAfterPublish:=False
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
It seems to run fine, but it doesn't create any PDF's, unless I'm just looking in the wrong place. Looks like you called out to have them save in my Z: drive folder I had created for it, but nothing is there.

Also, is there anything in there that will tell it to specifically print the Sheet "Cal Form"?
 
Upvote 0
Did you change the file path in red to match the location of all your files?
 
Upvote 0
Did you change the file path in red to match the location of all your files?
My files are in a hundred different folders based on what customer owns them. I made a Test folder on my C: drive to match and put two test files in it, and changed the file type to .xlsm. That is the only change I made.

I'm also not sure the file name code is working correctly because I tried to add it into my individual macro to change the name to "date serial.pdf" but it just fails the macro every time, the .Range gets highlighted and gives a "compile error: Invalid or unqualified reference"
VBA Code:
Sub exportToPDF()
'
' exportToPDF Macro
'
' Keyboard Shortcut: Ctrl+q
'
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Z:\Shared\EEW\Lab\Cal Cert PDF\" & .Range("C8") & " " & .Range("K8"), _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub
 
Upvote 0
I'm also not sure the file name code is working correctly because I tried to add it into my individual macro to change the name to "date serial.pdf" but it just fails the macro every time, the .Range gets highlighted and gives a "compile error: Invalid or unqualified reference"
VBA Code:
Sub exportToPDF()
'
' exportToPDF Macro
'
' Keyboard Shortcut: Ctrl+q
'
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Z:\Shared\EEW\Lab\Cal Cert PDF\" & .Range("C8") & " " & .Range("K8"), _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub
I've found this is some kind of Syntax error on my end but it confuses me, it's apparently just getting a run time error 1004 when trying to reference cell C8 even though there's nothing special about that cell. I have no idea what's wrong with it.
 
Upvote 0
Try removing the dots to the left of “Range”.
 
Upvote 0
Try removing the dots to the left of “Range”.
I tried that as well. Still no good.
I think it's something to do with the fact that I have a date in cell C8. Maybe it doesn't like the / since file names can't have forward slashes. I tried formatting the date to 10-29-24 as well though and it still gives the same error.
It works if I just put a single letter in the cell.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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