Hi Everyone,
I have come across the below thread which has the relevant code (which works) to open a PDF which has been added to an excel spreadsheet (as an object) and print to the default printer.
Need to Print Embedded PDFs
From my understanding the /t prints to the default printer. I have been reading up and it appears possible to be able to specify a specific printer when using the /t but I am unable to get it to work with below code from the above thread.
I previously had code to change the default printer in windows, but the code itself is too slow (trying to find the default printer before changing, changing, and then changing back). So I was hoping to pass the printer name through to Adobe as it is executing.
Thanks in advance.
I have come across the below thread which has the relevant code (which works) to open a PDF which has been added to an excel spreadsheet (as an object) and print to the default printer.
Need to Print Embedded PDFs
From my understanding the /t prints to the default printer. I have been reading up and it appears possible to be able to specify a specific printer when using the /t but I am unable to get it to work with below code from the above thread.
VBA Code:
Sub PrintEmbeddedPDFs_03()
' ZVI:2013-08-01 http://www.mrexcel.com/forum/excel-questions/717204-need-print-embedded-pdfs-please-help.html
' ZVI:2014-05-14 Added subrotines for printing sheets and embedded PDFs
' ZVI:2014-06-28 Fixed the incorrect finding of last "%%EOF" in PDF with comments
Dim a() As Byte, b() As Byte, i As Long, j As Long, k As Long, n As Long
Dim FN As Integer, f As String, p As Variant, obj As OLEObject
Dim PDFPrint As String
PDFPrint = "PDFCreator"
p = ActiveWorkbook.Path
' Print all PDFs embedded into the active sheet
For Each obj In ActiveSheet.OLEObjects
i = 0: hwnd = 0: Size = 0: Ptr = 0
If obj.progID Like "Acro*.Document*" And obj.OLEType = 1 Then
obj.Copy
If OpenClipboard(0) Then
hwnd = GetClipboardData(49156)
If hwnd Then Size = GlobalSize(hwnd)
If Size Then Ptr = GlobalLock(hwnd)
If Ptr Then
ReDim a(1 To CLng(Size))
CopyMemory a(1), ByVal Ptr, Size
Call GlobalUnlock(hwnd)
i = InStrB(a, StrConv("%PDF", vbFromUnicode))
If i Then
' --> ZVI:2014-06-28
k = InStrB(i, a, StrConv("%%EOF", vbFromUnicode))
While k
j = k - i + 7
k = InStrB(k + 5, a, StrConv("%%EOF", vbFromUnicode))
Wend
' <--
ReDim b(1 To j)
For k = 1 To j
b(k) = a(i + k - 1)
Next
Ptr = 0
End If
End If
Application.CutCopyMode = False
CloseClipboard
If i Then
n = n + 1
f = p & "\_printed_.pdf"
If Len(Dir(f)) Then Kill f
FN = FreeFile
Open f For Binary As #FN
Put #FN, , b
Close #FN
CreateObject("wscript.shell").Run "AcroRd32.exe /N /T """ & f & """", , True
Kill f
End If
Else
Application.CutCopyMode = False
End If
End If
Next
' Inform how many Embedded PDFs were printed
MsgBox "Amount of the printed PDFs: " & n, vbInformation, "PrintEmbeddedPDFs"
exit_:
If Err Then MsgBox Err.Description, vbCritical, "Error #" & Err.Number
End Sub
I previously had code to change the default printer in windows, but the code itself is too slow (trying to find the default printer before changing, changing, and then changing back). So I was hoping to pass the printer name through to Adobe as it is executing.
Thanks in advance.