473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
1 6075
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

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

Similar topics

10
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
2
by: Jenna Schmidt | last post by:
I know that one of the benefits of using "Shared" methods is you do not explicitly have to Dim as New object to access the method. Are there some other implications with memory and concurrency...
6
by: Ross | last post by:
MyWebProject.MyWebForm1.someset.somedata is a datatable within a dataset. Displays quite nicely, too. Now I want to use the same data in MyWebProject.MyWebForm2. Being a old, er, experienced Java...
1
by: Dave | last post by:
I am getting te following error in a ASP.Net app that is running on Win XP Pro (SP2): Server cannot access application directory 'C:\Documents and Settings\dave\My Documents\My Visual Studio...
2
by: Simon Verona | last post by:
I have a receipt printer that is set up using a generic/text print driver. I want to send a print to it. The standard vb.net printing methodology seems a little bit of overkill. Is there any...
2
by: pramodrepaka | last post by:
I am trying to print a form with the help of below code form1.printform but the above code gives me an error as "printer error"with an error no 482 printer is connected to lan and it is...
1
by: =?Utf-8?B?T21lZ2FTcXVhcmVk?= | last post by:
have customized PageSetup and Print dialogs that emulate the standard dialogs. I would like to be able to open the "Printer Preferences" dialog by clicking a button (just like can be done on the...
2
by: ladams1949 | last post by:
In Excel, I need to 1. Select "PDF printer" from the printer list when printing a page 2. Recognize when the "Save As" window comes up 3. And then Sendkeys "finename~" to save the PDF file. ...
3
by: mohsinews | last post by:
Hey, Hope you are all doing good. I am a new bee here. I have recently transfer a website from one hosting server to another and got the error message "Fatal error: Unable to read 4761 bytes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.