VBA Duplex - Access Denied - Can only read settings - VB6 can set Duplex

P

Peter McLennan

Hi All,

I have read most posts about duplexing in VBA by using OpenPrinter,
and DocumentProperties. I have used the example, and I know the work
around for "Access Denied" is to add a network printer as a local
printer.

I know I can use SendKeys to open the Print Dialog and then got the
properties window to set Duplex on or off.

I have tried to reference a VB6 DLL that changes Duplex mode, but it
does not appear to change the setting for Word.

My question is that VB6 can programmatically set Duplex on or off, so
VB6 must be addressing the "Access Denied" via a different way.

Has anyone been able to change duplex to simplex and vice versa for a
network printer without using any of the above work arounds?

Is there a direct method to the print driver API's that VB6 uses?

Regards

Peter McLennan
 
S

SA

Peter:

It would help if you articulated what you goal is here. You mention Word.
Are you trying to duplex print a word document from VB6?

You should be able to administer a network printer with the Open Printer,
GetPrinter and SetPrinter api to set the duplex dev mode element, even under
user level log on privileges, using a printer_info_9 access level, which
addresses the per-user default printer settings under Windows 2000 and Xp.
Even a User should be able to access this without an Access denied result,
as compared to a printer_info_2 structure. Under NT, this will not work
since that level was not available. (However if you really want to get down
and dirty you can do it on NT using the raw per user dev mode information
stored in the reg key HKEY_CURRENT_USER\Printers\DevModePerUser and byte
manipulation of that data.) There's no reason to use SendKeys with the
Print dialog to set the duplex element.
 
P

Peter McLennan

Thanks SA for the tid bit,

Unfortunately there is no example of printer_info_9 in use in the
wild.

Do you have the VB declarations for the printer_info_9 which points to
a devmode structure which I assume is different to MSDN Q230743.

And yes I am trying to set the printer to Duplex within WORD VBA.

Regards

Peter McLennan
 
J

Jonathan West

Peter McLennan said:
Thanks SA for the tid bit,

Unfortunately there is no example of printer_info_9 in use in the
wild.

Do you have the VB declarations for the printer_info_9 which points to
a devmode structure which I assume is different to MSDN Q230743.

And yes I am trying to set the printer to Duplex within WORD VBA.

Take a look at this article.

Controlling the Printer from Word VBA
Part 2: Using VBA to control Duplex, Color Mode and Print Quality
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=116


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
P

Peter McLennan

I have tried printer_info_9 and it still fails on the SetPrinter line.

Any ideas on how to set to Duplex mode when the user does not have
administer rights to a Network printer?


Option Explicit

Public Type PRINTER_DEFAULTS

pDatatype As Long
pDevmode As Long
DesiredAccess As Long
End Type

Public Type PRINTER_INFO_2
pServerName As Long
pPrinterName As Long
pShareName As Long
pPortName As Long
pDriverName As Long
pComment As Long
pLocation As Long
pDevmode As Long ' Pointer to DEVMODE
pSepFile As Long
pPrintProcessor As Long
pDatatype As Long
pParameters As Long
pSecurityDescriptor As Long ' Pointer to SECURITY_DESCRIPTOR
Attributes As Long


Priority As Long
DefaultPriority As Long
StartTime As Long
UntilTime As Long
Status As Long
cJobs As Long
AveragePPM As Long
End Type

Public Type DEVMODE
dmDeviceName As String * 32

dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * 32
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
dmICMMethod As Long
dmICMIntent As Long
dmMediaType As Long
dmDitherType As Long
dmReserved1 As Long
dmReserved2 As Long
End Type

Public Type PRINTER_INFO_9
pDevmode As Long ' Pointer to DEVMODE
End Type

Public Const DM_DUPLEX = &H1000&
Public Const DM_IN_BUFFER = 8

Public Const DM_OUT_BUFFER = 2
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

Public Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long
Public Declare Function DocumentProperties Lib "winspool.drv" _
Alias "DocumentPropertiesA" (ByVal hwnd As Long, _
ByVal hPrinter As Long, ByVal pDeviceName As String, _
ByVal pDevModeOutput As Long, ByVal pDevModeInput As Long, _
ByVal fMode As Long) As Long
Public Declare Function GetPrinter Lib "winspool.drv" Alias _
"GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
pPrinter As Byte, ByVal cbBuf As Long, pcbNeeded As Long) As Long
Public Declare Function OpenPrinter Lib "winspool.drv" Alias _
"OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long,
_
pDefault As PRINTER_DEFAULTS) As Long
Public Declare Function SetPrinter Lib "winspool.drv" Alias _
"SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
pPrinter As Byte, ByVal Command As Long) As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
_
(pDest As Any, pSource As Any, ByVal cbLength As Long)

Public Declare Function GetLastError Lib "kernel32" () As Long

Public Enum DuplexSetting
dsSimplex = 0
dsDuplexLongEdge = 1
dsDuplexShortEdge = 2
End Enum
' ==================================================================
' SetPrinterDuplex
'
' Programmatically set the Duplex flag for the specified printer
' driver's default properties.
'
' Returns: True on success, False on error. (An error will also

' display a message box. This is done for informational value
' only. You should modify the code to support better error
' handling in your production application.)
'
' Parameters:
' sPrinterName - The name of the printer to be used.
'
' nDuplexSetting - One of the following standard settings:
' 1 = None
' 2 = Duplex on long edge (book)
' 3 = Duplex on short edge (legal)
'
' ==================================================================
Public Function SetGetPrinterDuplex(ByRef lPrevDuplexSetting As
DuplexSetting, _
Optional sPrinterName As String, _
Optional nDuplexSetting As DuplexSetting, _
Optional bSet As Boolean, _
Optional sErrMsg As String) As Boolean

Dim hPrinter As Long
Dim pd As PRINTER_DEFAULTS
Dim pinfo As PRINTER_INFO_9
Dim dm As DEVMODE

Dim yDevModeData() As Byte
Dim yPInfoMemory() As Byte
Dim nBytesNeeded As Long
Dim nRet As Long, nJunk As Long
Dim iFirstRight As Integer
Dim i As Integer

On Error GoTo cleanup

If (nDuplexSetting < dsSimplex) Or (nDuplexSetting >
dsDuplexLongEdge) Then
sErrMsg = "Error: dwDuplexSetting is incorrect."
Exit Function
End If

If Len(sPrinterName) = 0 Then
sPrinterName = ActivePrinter
sPrinterName = Trim$(Left$(ActivePrinter, _
InStr(ActivePrinter, " on ")))

If InStr(1, sPrinterName, " on ", vbTextCompare) > 0 Then
sPrinterName = Trim(Mid(sPrinterName, 1, InStr(1,
sPrinterName, " on ", vbTextCompare)))
End If

End If
pd.DesiredAccess = PRINTER_ACCESS_USE
sPrinterName = sPrinterName & Chr(0)
nRet = OpenPrinter(sPrinterName, hPrinter, pd)
If (nRet = 0) Or (hPrinter = 0) Then
If Err.LastDllError = 5 Then
sErrMsg = "Access denied -- See the article for more
info."
Else
sErrMsg = "Cannot open the printer specified " & _
"(make sure the printer name is correct)."
End If
Exit Function
End If

nRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)
If (nRet < 0) Then
sErrMsg = "Cannot get the size of the DEVMODE structure."
GoTo cleanup
End If

ReDim yDevModeData(nRet + 100) As Byte
nRet = DocumentProperties(0, hPrinter, sPrinterName, _
VarPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)
If (nRet < 0) Then
sErrMsg = "Cannot get the DEVMODE structure."
GoTo cleanup
End If

Call CopyMemory(dm, yDevModeData(0), Len(dm))

If Not CBool(dm.dmFields And DM_DUPLEX) Then
sErrMsg = "You cannot modify the duplex flag for this printer
" & _
"because it does not support duplex or the driver " & _
"does not support setting it from the Windows API."
GoTo cleanup
End If

lPrevDuplexSetting = dm.dmDuplex
dm.dmDuplex = nDuplexSetting
If Not bSet Then
GoTo cleanup
End If

Call CopyMemory(yDevModeData(0), dm, Len(dm))

nRet = DocumentProperties(0, hPrinter, sPrinterName, _
VarPtr(yDevModeData(0)), VarPtr(yDevModeData(0)), _
DM_IN_BUFFER Or DM_OUT_BUFFER)

If (nRet < 0) Then
sErrMsg = "Unable to set duplex setting to this printer."
GoTo cleanup
End If

Call GetPrinter(hPrinter, 2, 0, 0, nBytesNeeded)
If (nBytesNeeded = 0) Then GoTo cleanup

ReDim yPInfoMemory(nBytesNeeded + 100) As Byte

nRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), nBytesNeeded,
nJunk)
If (nRet = 0) Then
sErrMsg = "Unable to get shared printer settings."
GoTo cleanup
End If

Call CopyMemory(pinfo, yPInfoMemory(0), Len(pinfo))
pinfo.pDevmode = VarPtr(yDevModeData(0))
'pinfo.PI. .pSecurityDescriptor = 0
Call CopyMemory(yPInfoMemory(0), pinfo, Len(pinfo))

nRet = SetPrinter(hPrinter, 2, yPInfoMemory(0), 0)
If (nRet = 0) Then
sErrMsg = "Unable to set shared printer settings. " &
GetLastError
End If


SetGetPrinterDuplex = CBool(nRet)

cleanup:
If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)

End Function
 
P

Peter McLennan

Thanks Jonathan,

Many thanks. I had actually tried your example a while ago, and I
thought it did not wor. I was looking at the return code, and not
rechecking the status of Duplex. The printer is being set to duplex
or simplex, but the return code is 0 and according to MSDN that is an
error. There is possibly a time lag for the duplex setting.

Thanks again

Peter McLennan
 
P

Peter McLennan

Add this line after the close printer statment and WORD will accept
the settings. Otherwise you have to have a sleep setting, and 5
seconds was the amount that took mine to work

If bSet Then
' this application
hwndApp = FindWindow("OpusApp", ActiveWindow.Caption & " - "
& Application.Caption)
If hwndApp = 0 Then
' cant miss with this one unless there are multiple
instances of Word
hwndApp = FindWindow("OpusApp", vbNullString)
End If
If hwndApp <> 0 Then
Call SendMessageTimeout(hwndApp, WM_SETTINGCHANGE, _
0, 0, SMTO_NORMAL, 500, ByVal 0&)
End If
End If
 
M

mike control

Gentlemen

Just a correction to your code that will make it workable:

'nRet = SetPrinter(hPrinter, 2, yPInfoMemory(0), 0)
nRet = SetPrinter(hPrinter, 9, yPInfoMemory(0), 0)

According to MSDN, in SetPrinter function the second parameter should be the
same "N" as in PRINTER_INFO_"N" .
Hope this helps...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top