Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Setting properties for the default printer
Message
From
14/04/2004 21:37:33
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Setting properties for the default printer
Miscellaneous
Thread ID:
00894995
Message ID:
00894995
Views:
54
Hi

We are doing OLE with word to create batch document that must be printed. These document must be printed with some special setting to the default printer. We find a way to set the default printer properties before launching the batch (word is opening a template, filing some part of the template with information about a client and printing the document, close the document and begin the same all of our client). The code we found is a VB code that find the default printer and set the properties. I put the code at the end of the message. We are trying to set the dmDuplex mode to printer using the long side. When we are setting the hp printer this is working. After the VB code is run the default printer is set with the right duplex mode. If we open the document with word and print the setting is allright. If we do the same with a lexmark postscript we open the document with word and print the setting is not working. When we run the VB code to set the lexmark and open the control panel and check the properties we just try to set the properties is ok but seem not to be set in the system. We to click the OK button to close the properties window now the properties will really be set and the document will print correctly. I explain again, we run VB code to set the duplex properties for the lexmark but this seem not to work, we open the properties window for that printer and the properties seem to be set correctly (but in fact is not working) now we just clic OK to close the properties window and the properties is now effective(we didn't make any change in the properties window we just open and close with the OK button and after the system know about the change). Is there a way to force XP to catch the new setting or another way more simple to set this propertie.


sPrinterName = Printer.DeviceName
sPrinterPort = Printer.Port

If sPrinterPort = "LPT1:" Then
pd.DesiredAccess = PRINTER_NORMAL_ACCESS
Else
pd.pDatatype = 0
pd.pDevmode = 0
pd.DesiredAccess = SERVER_ALL_ACCESS
End If

iRet = OpenPrinter(sPrinterName, hPrinter, pd)
If (iRet = 0) Or (hPrinter = 0) Then
'Can't access current printer. Bail out doing nothing
Exit Function
End If

'Get the size of the DEVMODE structure to be loaded
iRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)
If (iRet < 0) Then
'Can't access printer properties.
GoTo cleanup
End If

'Make sure the byte array is large enough
'Some printer drivers lie about the size of the DEVMODE structure they
'return, so an extra 100 bytes is provided just in case!
ReDim yDevModeData(0 To iRet + 128) As Byte

'Load the byte array
iRet = DocumentProperties(0, hPrinter, sPrinterName, _
VarPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)
If (iRet < 0) Then
GoTo cleanup
End If

'Copy the byte array into a structure so it can be manipulated
Call CopyMemory(dm, yDevModeData(0), Len(dm))

If dm.dmFields And iPropertyType = 0 Then
'Wanted property not available. Bail out.
GoTo cleanup
End If

'Set the property to the appropriate value
Select Case iPropertyType
Case DM_ORIENTATION
dm.dmOrientation = iPropertyValue
Case DM_PAPERSIZE
dm.dmPaperSize = iPropertyValue
Case DM_PAPERLENGTH
dm.dmPaperLength = iPropertyValue
Case DM_PAPERWIDTH
dm.dmPaperWidth = iPropertyValue
Case DM_DEFAULTSOURCE
dm.dmDefaultSource = iPropertyValue
Case DM_PRINTQUALITY
dm.dmPrintQuality = iPropertyValue
Case DM_COLOR
dm.dmColor = iPropertyValue
Case DM_DUPLEX
** under is the propertie we want to set
dm.dmDuplex = iPropertyValue
End Select

'Load the structure back into the byte array
Call CopyMemory(yDevModeData(0), dm, Len(dm))

'Tell the printer about the new property
iRet = DocumentProperties(0, hPrinter, sPrinterName, _
VarPtr(yDevModeData(0)), VarPtr(yDevModeData(0)), _
DM_IN_BUFFER Or DM_OUT_BUFFER)

If (iRet < 0) Then
GoTo cleanup
End If

'The code above *ought* to be sufficient to set the property
'correctly. Unfortunately some brands of Postscript printer don't
'seem to respond correctly. The following code is used to make
'sure they also respond correctly.
Call GetPrinter(hPrinter, 2, 0, 0, iBytesNeeded)
If (iBytesNeeded = 0) Then
'Couldn't access shared printer settings
GoTo cleanup
End If

'Set byte array large enough for PRINTER_INFO_2 structure
ReDim yPInfoMemory(0 To iBytesNeeded + 128) As Byte

'Load the PRINTER_INFO_2 structure into byte array
iRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), iBytesNeeded, iJunk)
If (iRet = 0) Then
'Couldn't access shared printer settings
GoTo cleanup
End If

'Copy byte array into the structured type
Call CopyMemory(pinfo, yPInfoMemory(0), Len(pinfo))

'Load the DEVMODE structure with byte array containing
'the new property value
pinfo.pDevmode = VarPtr(yDevModeData(0))

'Set security descriptor to null
pinfo.pSecurityDescriptor = 0

'Copy the PRINTER_INFO_2 structure back into byte array
Call CopyMemory(yPInfoMemory(0), pinfo, Len(pinfo))

'Send the new details to the printer
iRet = SetPrinter(hPrinter, 2, yPInfoMemory(0), 0)

'Indicate whether it all worked or not!
SetPrinterProperty = CBool(iRet)
Next
Reply
Map
View

Click here to load this message in the networking platform