Objective: Print text to AdobePDF from multiple sources in Access using WinAPI GDI Print functions. Specifically,
- Get user printer preferences using
PrintDlgEx
- Use either of these below to call
StartDoc
PRINTDLGEX.hDC
ORhDC = CreateDC()
, using theDEVMODE
outputted from thePrintDlgEx
- Use either of these below to call
Problem: StartDoc
always returns -1 for hDCs (for Adobe PDF) created using a DEVMODE
and returns > 0 for all other printers chosen. Is the DEVMODE
different size or format for Adobe PDF?
Info: Here's what I've tried and the results thus far:
- NEW INFO 8/22/14 Works for PDF when Adobe PDF is not my default printer. Works for other printers, regardless of default setting. Why would it only fail when Adobe PDF is default but not fail for other printers?
- MORE NEW INFO 8/22/14 When Adobe PDF is default, if users clicks another printer and then back on Adobe PDF in the
PrintDlgEx
, it works. WTF?!? This is getting ridiculous. - Identical code works for other printers.
- IsValidDevMode = True.
- Tried multiple different versions of the
DEVMODE
type, all of which seem to work for other printers, but not Adobe PDF. - When
StartDoc
does return -1,Err.LastDllError
returns 0. - When using
CreateDC
with typeDEVMODE
for last parameter, code works for all printers except Adobe PDF. - Declaring
CreateDC
with type Long for theDEVMODE
parameter and passing 0 works. But this doesn't incorporate the user's options. The printer defaults are always used. - Using the
CreateDC
above and thenResetDC
using theDEVMODE
fromPrintDlgEx
causes same problem. - Setting
Application.Printer
settings manually, instead of all of this. Doesn't work. - Using different
DOCINFO
parameters when Adobe PDF is chosen.
Code:
Public Function printRawData(objRTB As Object, Optional strFilename As String = "cBasePrint") As Boolean
Dim hDCPrinter As Long, sDeviceName As String, lJob As Long
Dim udtDevMode As DEVMODE, udtDocInfo As DOCINFO
If printPrintDialog(udtDevMode, hDCPrinter) Then
Debug.Print hDCPrinter, IsValidDevMode(udtDevMode, LenB(udtDevMode)), udtDevMode.dmOrientation, udtDevMode.dmDeviceName
If hDCPrinter <> 0 Then
udtDocInfo.pDocName = strFilename
udtDocInfo.pOutputFile = vbNullString
udtDocInfo.pDatatype = "RAW"
udtDocInfo.fType = 0
udtDocInfo.sSize = LenB(udtDocInfo)
lJob = StartDoc(hDCPrinter, udtDocInfo)
Debug.Print lJob, Err.LastDllError
If lJob > 0 Then
objRTB.SelPrint hDCPrinter, 0
EndDoc (hDCPrinter)
End If
DeleteDC (hDCPrinter)
End If
End If
End Function
Related Questions:
Raw Printing Set DevMode options (Orientation, copies, margins, default source, etc.)
How to show printer properties/preferences dialog and save changes?
References:
Lessan Vaezi: Printer Settings Nearly identical code to mine.
MS KB: Saving DEVMODE Properties Don't want to do this. My PrintDlgEx
call works and returns a valid DEVMODE
for other printers.
CodeGuru: Printing-using-Win32-Printer Identical issue from what I can tell. No answer.
Adobe PDF Community Most of the way down the thread, people are having the same issues.