Pastafarian
New Member
- Joined
- Feb 21, 2012
- Messages
- 30
Hello,
I have a sheet that gets saved as PDF (currently using the .ExportAsFixedFormat method), works flawlessly. I'd like to add a PDF-background to this PDF, like a watermark. Reason for this is that the header and footer settings in Excel just aren't advanced enough and can't offer real high quality pictures. I've searched the internet far and wide for an solution but couldn't find any for this, many questions regarding this were unanswered.
As far as I know this isn't possible in Excel itself and will require a 3rd party program. I've been looking into PDFCreator, and PDFCreator2.0 and 2.1 support adding PDF backgrounds to your PDF. You can setup the program to automatically add a PDF as a background to all your PDF's, however v2.0 and v2.1 do not support Excel VBA. Version 1.7.3 does support Excel VBA, but this version doesnt support adding watermarks.
The code I have so far for saving a sheet as PDF in PDF Creator 1.7.3 is as following, it works flawlessly but only saves your sheet to PDF and doesnt add a watermark:
Anyone know any way to alter this to put a PDF watermark/background behind it or any other way for putting PDF's as background?
Much appreciated!
I have a sheet that gets saved as PDF (currently using the .ExportAsFixedFormat method), works flawlessly. I'd like to add a PDF-background to this PDF, like a watermark. Reason for this is that the header and footer settings in Excel just aren't advanced enough and can't offer real high quality pictures. I've searched the internet far and wide for an solution but couldn't find any for this, many questions regarding this were unanswered.
As far as I know this isn't possible in Excel itself and will require a 3rd party program. I've been looking into PDFCreator, and PDFCreator2.0 and 2.1 support adding PDF backgrounds to your PDF. You can setup the program to automatically add a PDF as a background to all your PDF's, however v2.0 and v2.1 do not support Excel VBA. Version 1.7.3 does support Excel VBA, but this version doesnt support adding watermarks.
The code I have so far for saving a sheet as PDF in PDF Creator 1.7.3 is as following, it works flawlessly but only saves your sheet to PDF and doesnt add a watermark:
Code:
#If VBA7 Then Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#Else
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End If
Public Function PrintThisDoc(Formname As Long, FileName As String)
On Error Resume Next
Dim X As Long
X = ShellExecute(Formname, "Print", FileName, 0&, 0&, 1)
End Function
Code:
Option ExplicitSub PrintToPDF_Early()
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: Print to PDF file using PDFCreator
' (Download from http://sourceforge.net/projects/pdfcreator/)
' Designed for early bind, set reference to PDFCreator
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
Dim bRestart As Boolean
'/// Change the output file name here! ///
sPDFName = "testPDF.pdf"
sPDFPath = ActiveWorkbook.path & Application.PathSeparator
'Check if worksheet is empty and exit if so
If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
'Activate error handling and turn off screen updates
On Error GoTo EarlyExit
Application.ScreenUpdating = False
Set pdfjob = New PDFCreator.clsPDFCreator
'Check if PDFCreator is already running and attempt to kill the process if so
Do
bRestart = False
Set pdfjob = New PDFCreator.clsPDFCreator
If pdfjob.cStart("/NoProcessingAtStartup") = False Then
'PDF Creator is already running. Kill the existing process
Shell "taskkill /f /im PDFCreator.exe", vbHide
DoEvents
Set pdfjob = Nothing
bRestart = True
End If
Loop Until bRestart = False
'Assign settings for PDF job
With pdfjob
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Delete the PDF if it already exists
If Dir(sPDFPath & sPDFName) = sPDFName Then Kill (sPDFPath & sPDFName)
'Print the document to PDF
ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
'Wait until the file shows up before closing PDF Creator
Do
DoEvents
Loop Until Dir(sPDFPath & sPDFName) = sPDFName
Cleanup:
'Release objects and terminate PDFCreator
Set pdfjob = Nothing
Shell "taskkill /f /im PDFCreator.exe", vbHide
On Error GoTo 0
Application.ScreenUpdating = True
Exit Sub
EarlyExit:
'Inform user of error, and go to cleanup section
MsgBox "There was an error encountered. PDFCreator has" & vbCrLf & _
"has been terminated. Please try again.", _
vbCritical + vbOKOnly, "Error"
Resume Cleanup
End Sub
Anyone know any way to alter this to put a PDF watermark/background behind it or any other way for putting PDF's as background?
Much appreciated!
