473,385 Members | 1,409 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,385 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 6081
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.