Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing in Win98 & XP 1

Status
Not open for further replies.

Golom

Programmer
Sep 1, 2003
5,595
CA
The problem is that the following (much condensed) code does what it's supposed to in Win98SE but not in XP.

The intent is to change the default printer to one specified in a setup table; change to a specific font that is an internal font for the printer (specifically font '15 cpi'); and print the contents of a file. The printer is a 40-column receipt printer.

As I step through the code on both operating systems (98 & XP) everything appears to work, That is, the printer is set as the default printer; the font is changed to '15 cpi' and printing proceeds. The difference is that, in Win98 appearance and fact coincide. The document is in fact printed with that font and all is well. In XP the document is also printed but using some other font which appears to be a downloaded font that is much slower. The font in XP is also a proportional font whereas '15 cpi' is non-proportional.

This happens regardless of the details of the connection port (serial or parallel).

If I open the file I'm printing in wordpad and change its font to 15 cpi then it prints successfully using the correct font on both systems.

Does XP have some particular magic associated with talking to printers that's different than earlier OS versions?

Code:
[COLOR=green]' Save the current default printer and pick the 
' specified printer for this print job.[/color]
Old_Default = SetPrinterAsDefault(PrinterName)

SelectAPrinter PrinterName, SelectedPrinter

Set Printer = SelectedPrinter

Printer.Font.Name = "15 cpi"

For n = 1 To nCopies

        [COLOR=green]' Open the file to be printed.[/color]
        nH_Input = FreeFile
        Open FileName For Input As #nH_Input

        [COLOR=green]' Set up Print Parameters.[/color]
        Do Until EOF(nH_Input)
            Line Input #nH_Input, cBuf
            Printer.Print cBuf
        Loop
        Close
        Printer.Print " "
        Printer.EndDoc

        [COLOR=green]' Code for cutting the paper omitted.[/color]

Next
 
THIS MAY HELP

it is not mine but 12 months ago fixd a printing
problem for me

I found this on Tek-tis but not sure where

Unfortunatly I do not have the code I used ...
( it is on Another computer than my Daughter *borrowed* )



++++++++++++++++++++++++++++++++++++++

yourvariable = activeprinter

++++++++++++++++++++++++++++++++++++


Public Const MAX_FILENAME_LEN = 256
Public Const HWND_BROADCAST = &HFFFF&
Public Const WM_WININICHANGE = &H1A

Public Declare Function GetWindowsDirectory Lib "kernel32" _
Alias "GetWindowsDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long


Public Declare Function GetPrivateProfileString _
Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

Public Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long


Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long


Public Function Set_Printer_As_Default(ByVal NewDefault As String) As String

Dim nC As Integer
Dim String_Len As Long
Dim strWinPath As String
Dim strSysFileName As String
Dim strRet As String
Dim Current_Default As String
Dim New_Printer As String
Dim Pr As Printer

' Windows wants "Device Name,Driver Name,Port" in the string to set
' a new default printer.
New_Printer = ""
For Each Pr In Printers
If Pr.DeviceName = NewDefault Then
New_Printer = Pr.DeviceName & "," & Pr.DriverName & "," & Pr.Port
Exit For
End If
Next

' Get the path of the Windows\System directory.
strWinPath = Space$(MAX_FILENAME_LEN)
String_Len = GetWindowsDirectory(strWinPath, MAX_FILENAME_LEN)
strWinPath = Left$(strWinPath, String_Len)
strSysFileName = strWinPath & "\win.ini"

' Get the current default printer
' This will be of the form 'DeviceName,DriverName,Port'
' For example "iDP3210 Full Cut,iDP3210,LPT1:"
strRet = Space$(MAX_FILENAME_LEN)
String_Len = GetPrivateProfileString("windows", "device", "", strRet, MAX_FILENAME_LEN, strSysFileName)
Current_Default = Left$(strRet, String_Len)

' If he didn't provide a new printer name then just return the current default.
If Len(New_Printer) = 0 Then
nC = InStr(1, Current_Default, ",")
If nC = 0 Then nC = Len(Current_Default) + 1
Set_Printer_As_Default = Left$(Current_Default, nC - 1)
Exit Function
End If

' Don't bother to set it if the new default is already the default.
If Current_Default <> New_Printer Then
' Set the selected printer as the default.
String_Len = WritePrivateProfileString("windows", "device", New_Printer, strSysFileName)
SendMessage HWND_BROADCAST, WM_WININICHANGE, 32767&, ByVal "windows"
End If

' Return the name of the previous default printer as the function value.
nC = InStr(1, Current_Default, ",")
If nC = 0 Then nC = Len(Current_Default) + 1
Set_Printer_As_Default = Left$(Current_Default, nC - 1)

End Function


Just call it with

Dim OldDefault As String
OldDefault = Set_Printer_As_Default NewPrinterName

' ... Do the Printing ...

Call Set_Printer_As_Default(OldDefault)



That will work fine on NT4 but isn't supported on other platforms.
Under W9x/ME use GetPrinter with a PRINTER_INFO5 structure,
set the Attributes member to PRINTER_ATTRIBUTE_DEFAULT then call
SetPrinter to update the change.

On W2000/XP use SetDefaultPrinter (new) which just requires the printer
name as a string.

There's an ActiveX DLL, PDXPro, that amongst other
things, gets/sets the default printer. It detects the Windows version
and wraps the API calls so you can manage the default printer with a
couple of lines of code.

I found PDXPro using google search
 
Thanks Peter. Unfortunately, you got that from me. That is what is behind my SetPrinterAsDefault code in the example I gave. ... and it does work in Win9x operating systems. Haven't tried it in ME however.

I do seem to have figured it out however.

It seems that Win XP needs a DoEvents to actually change the printer default or to a different printer or to set a different print font. Once that's done XP gets it's act together and prints as intended.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top