By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,154 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,154 IT Pros & Developers. It's quick & easy.

Printing using VB/IE - Getting Error "Unable to set shared printer settings"

P: 1
Public Function SetPrinterDefaultsW(ByVal sPrinterName As String, _
ByVal nPaperSize As Long, ByVal nOrientation As Long) As Boolean
Dim Prn As Printer
Dim hPrinter As Long
Dim pd As PRINTER_DEFAULTS
Dim pinfo2 As PRINTER_INFO_2
Dim pinfo8 As PRINTER_INFO_8
Dim pinfo9 As PRINTER_INFO_9
Dim dm As DEVMODEW
Dim yDevModeData() As Byte
Dim ypinfoMemory() As Byte
Dim ypinfoMemory8() As Byte
Dim ypinfoMemory9() As Byte
Dim nBytesNeeded As Long
Dim nRet As Long, nJunk As Long
Dim szDriverName As String
Dim nDMSize As Long
Dim hDC As Long
Dim querySetReg As Long
Dim SetReg As Long

' On Error GoTo cleanup
querySetReg = SETDEVMODEINREG
SetReg = 0&

'If nDuplexSetting < 1 Or nDuplexSetting > 3 Then
' MsgBox "Error: Duplex Setting is invalid.", vbExclamation
' Exit Function
'End If

' Find the printer to get correct driver name
For Each Prn In Printers
If Prn.DeviceName = sPrinterName Then
szDriverName = Prn.DriverName
End If
Next

' You must set PD with PRINTER_INFO_2. Note that you cannot request
' more rights than you have as a User.
' Also PRINTER_ACCESS_ADMINISTER is unnecessary for level 9.
pd.DesiredAccess = PRINTER_ALL_ACCESS ' PRINTER_ACCESS_USE Or STANDARD_RIGHTS_REQUIRED
nRet = OpenPrinterW(StrPtr(sPrinterName), hPrinter, pd)
If (nRet = 0) Or (hPrinter = 0) Then
If Err.LastDllError = 5 Then
MsgBox "Access denied."
Else
MsgBox " Cannot open " & sPrinterName & _
"(make sure the printer name is correct)."
End If
GoTo cleanup
End If

nDMSize = DocumentPropertiesW(0, hPrinter, ByVal StrPtr(sPrinterName), 0, 0, 0)
If (nDMSize < 0) Then
MsgBox "Cannot get the size of the DEVMODE structure."
GoTo cleanup
End If

ReDim yDevModeData(nDMSize) As Byte
nRet = DocumentPropertiesW(0, hPrinter, ByVal StrPtr(sPrinterName), _
VarPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)
If (nRet < 0) Then
MsgBox "Cannot get the DEVMODE structure."
GoTo cleanup
End If

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

If Not ((dm.dmFields And DM_ORIENTATION) = DM_ORIENTATION) Then
MsgBox "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
If Not ((dm.dmFields And DM_PAPERSIZE) = DM_PAPERSIZE) Then
MsgBox "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
' Use Or to combine fields
dm.dmFields = DM_PAPERSIZE + DM_ORIENTATION ' What fields are changing?
dm.dmPaperSize = nPaperSize
dm.dmOrientation = nOrientation
Call CopyMemory(yDevModeData(0), dm, Len(dm))

nRet = DocumentPropertiesW(0, hPrinter, ByVal StrPtr(sPrinterName), _
VarPtr(yDevModeData(0)), VarPtr(yDevModeData(0)), _
DM_IN_BUFFER Or DM_OUT_BUFFER)

If (nRet < 0) Then
MsgBox "Unable to change the setting to this printer."
GoTo cleanup
End If

' If you use level 2, you will need Full Control permissions
' on the driver. This usually fails for network printers.
Call GetPrinterW(hPrinter, 2, 0, 0, nBytesNeeded)
If (nBytesNeeded = 0) Then GoTo cleanup
ReDim ypinfoMemory(nBytesNeeded) As Byte
nRet = GetPrinterW(hPrinter, 2, ypinfoMemory(0), nBytesNeeded, nJunk)
If (nRet = 0) Then
MsgBox "Unable to get shared printer settings."
GoTo cleanup
End If
Call CopyMemory(pinfo2, ypinfoMemory(0), Len(pinfo2))
nRet = SetPrinterW(hPrinter, 2, ypinfoMemory(0), 0)
If (nRet = 0) Then
MsgBox "Unable to set shared printer settings. (Level 2) Error code: " & Err.LastDllError
End If

' Level 9 is for the current user profile (preferences).
ReDim ypinfoMemory9(sizeofDWORD) As Byte
pinfo9.pDevMode = VarPtr(yDevModeData(0)) ' pinfo2.pDevMode
Call CopyMemory(ypinfoMemory9(0), pinfo9, Len(pinfo9))
nRet = SetPrinterW(hPrinter, 9, ypinfoMemory9(0), 0)
If (nRet = 0) Then
'The following line commented by satyam(sathish) on 10/04/2006
'MsgBox "Unable to set shared printer settings. (Level 9) Error code: " & Err.LastDllError
End If

' Level 8 is global; for all users.
ReDim ypinfoMemory8(sizeofDWORD) As Byte
pinfo8.pDevMode = VarPtr(yDevModeData(0)) ' pinfo2.pDevMode
Call CopyMemory(ypinfoMemory8(0), pinfo8, Len(pinfo8))
nRet = SetPrinterW(hPrinter, 8, ypinfoMemory8(0), 0)
If (nRet = 0) Then
MsgBox "Unable to set shared printer settings. (Level 8) Error code: " & Err.LastDllError
End If

hDC = CreateDCW(StrPtr(szDriverName), StrPtr(sPrinterName), 0, 0)
If hDC = 0 Then
MsgBox Err.LastDllError
Exit Function
End If
' For the QUERYESCSUPPORT printer escape the return value is zero
' if the escape is not implemented. A return value less than zero
' indicates an error.
nRet = ExtEscapeStr(hDC, QUERYESCSUPPORT, sizeofDWORD, _
querySetReg, sizeofDWORD, SetReg)
If nRet <> 0 Then ' the Escape code is supported
nRet = ExtEscapeDM(hDC, SETDEVMODEINREG, nDMSize, _
VarPtr(yDevModeData(0)), sizeofDWORD, SetReg)
If nRet > 0 Then ' success!
SetPrinterDefaultsW = 0
Else ' failure!
SetPrinterDefaultsW = Err.LastDllError
End If
End If

nRet = DeleteDC(hDC)
If nRet = 0 Then
MsgBox Err.LastDllError
End If
SetPrinterDefaultsW = CBool(nRet) ' 0 is False

'Exit Function
cleanup:
If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)
If Err.Number <> 0 Then SetPrinterDefaultsW = False
' MsgBox "Error: " & Err.Number & " " & Err.Description & " Code: " & Err.LastDllError
End Function
May 3 '06 #1
Share this Question
Share on Google+
1 Reply


P: 2
Hello,

It is al long time ago when you
posted this question.

If you have a solution for changing
the duplex property from a network printer
please tell me about is.

Thanks...

Greatings Hans
Sep 25 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.