Solved

Weird default Printer Problems

Posted on 1998-09-02
5
158 Views
Last Modified: 2010-04-30
I am trying to select a printer from the printers installed on my machine which happens to be a label printer, and print a few details to it. However, My vb program seems always to print to the default printer no matter what printer i select. I am using the same code as given in the vb help files, but this doesn't seem to work.

Here is the code im using:

Dim ret As Integer
ret = MsgBox("Are You Sure You Want To Print Label?", vbQuestion + vbYesNo, "Print Confirm")
If ret = vbYes Then
'the following code will select the correct printer'
Dim X As Printer
For Each X In Printers
    If X.DeviceName = "Label Printer" Then
        Set Printer = X
        Exit For
   End If
Next X

'Print the details to the printer
Printer.Print "Line 1"
Printer.Print "Line 2"
Printer.Print "Line 3"
Printer.EndDoc

Can anyone see the problem?
When i set the label printer as default, this works fine!
0
Comment
Question by:Coates
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
waty earned 30 total points
ID: 1432541
Several solutions :
   - Install Service Pack 3 (Microsoft told, it corrects the problem)
   - Use those function :

Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lparam As String) As Long

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

Public Type OSVERSIONINFO
   dwOSVersionInfoSize  As Long
   dwMajorVersion       As Long
   dwMinorVersion       As Long
   dwBuildNumber        As Long
   dwPlatformId         As Long
   szCSDVersion         As String * 128
End Type

Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
Public Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal Command As Long) As Long
Public Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long
Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Any) As Long
Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Function GetLastError Lib "kernel32" () As Long

' *** constants for DEVMODE structure
Public Const CCHDEVICENAME = 32
Public Const CCHFORMNAME = 32

' *** constants for DesiredAccess member of PRINTER_DEFAULTS
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

' *** constant that goes into PRINTER_INFO_5 Attributes member to set it as default
Public Const PRINTER_ATTRIBUTE_DEFAULT = 4

Public Type DEVMODE
   dmDeviceName         As String * CCHDEVICENAME
   dmSpecVersion        As Integer
   dmDriverVersion      As Integer
   dmSize               As Integer
   dmDriverExtra        As Integer
   dmFields             As Long
   dmOrientation        As Integer
   dmPaperSize          As Integer
   dmPaperLength        As Integer
   dmPaperWidth         As Integer
   dmScale              As Integer
   dmCopies             As Integer
   dmDefaultSource      As Integer
   dmPrintQuality       As Integer
   dmColor              As Integer
   dmDuplex             As Integer
   dmYResolution        As Integer
   dmTTOption           As Integer
   dmCollate            As Integer
   dmFormName           As String * CCHFORMNAME
   dmLogPixels          As Integer
   dmBitsPerPel         As Long
   dmPelsWidth          As Long
   dmPelsHeight         As Long
   dmDisplayFlags       As Long
   dmDisplayFrequency   As Long
   dmICMMethod          As Long        ' // Windows 95 only
   dmICMIntent          As Long        ' // Windows 95 only
   dmMediaType          As Long        ' // Windows 95 only
   dmDitherType         As Long        ' // Windows 95 only
   dmReserved1          As Long        ' // Windows 95 only
   dmReserved2          As Long        ' // Windows 95 only
End Type

Public Type PRINTER_INFO_5
   pPrinterName               As String
   pPortName                  As String
   Attributes                 As Long
   DeviceNotSelectedTimeout   As Long
   TransmissionRetryTimeout   As Long
End Type

Public Type PRINTER_DEFAULTS
   pDatatype            As Long
   pDevMode             As DEVMODE
   DesiredAccess        As Long
End Type

Public Sub SetDefaultPrinter(ByVal PrinterName As String, ByVal DriverName As String, ByVal PrinterPort As String)
   
   Dim DeviceLine As String
   Dim r As Long
   Dim l As Long
   DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort
   ' *** Store the new printer information in the [WINDOWS] section of
   ' *** the WIN.INI file for the DEVICE= item
   r = WriteProfileString("windows", "Device", DeviceLine)
   
   ' ***  Cause all applications to reload the INI file:
   l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")

End Sub

Public Sub Win95SetDefaultPrinter(sPrinter As String)
   
   Dim Handle           As Long              ' handle to printer
   Dim PrinterName      As String
   Dim pd               As PRINTER_DEFAULTS
   Dim x                As Long
   Dim need             As Long             ' bytes needed
   Dim pi5              As PRINTER_INFO_5   ' your PRINTER_INFO structure
   Dim LastError        As Long
   
   ' determine which printer was selected
   PrinterName = sPrinter
   
   ' none - exit
   If PrinterName = "" Then Exit Sub
   
   ' set the PRINTER_DEFAULTS members
   pd.pDatatype = 0&
   pd.DesiredAccess = PRINTER_ALL_ACCESS
   
   ' Get a handle to the printer
   x = OpenPrinter(PrinterName, Handle, pd)
   
   ' failed the open
   If x = False Then Exit Sub
     
   ' Make an initial call to GetPrinter, requesting Level 5  (PRINTER_INFO_5)
   ' information, to determine how many bytes  you need
   x = GetPrinter(Handle, 5, ByVal 0&, 0, need)
   
   ' don 't want to check GetLastError here - it's supposed to fail
   ' with a 122 - ERROR_INSUFFICIENT_BUFFER
   ' redim t as large as you need
   
   ReDim t((need \ 4)) As Long
   
   ' and call GetPrinter for keepers this time
   x = GetPrinter(Handle, 5, t(0), need, need)
   
   ' failed the GetPrinter
   If x = False Then Exit Sub
     
   ' set the members of the pi5 structure for use with SetPrinter.
   ' PtrCtoVbString copies the memory pointed at by the two    string
   ' pointers contained in the t() array into a Visual Basic string.
   ' The other three elements are just DWORDS (long integers) and
   ' don 't require any conversion
   pi5.pPrinterName = PtrCtoVbString(t(0))
   pi5.pPortName = PtrCtoVbString(t(1))
   pi5.Attributes = t(2)
   pi5.DeviceNotSelectedTimeout = t(3)
   pi5.TransmissionRetryTimeout = t(4)
   
   ' this is the critical flag that makes it the default printer
   pi5.Attributes = PRINTER_ATTRIBUTE_DEFAULT
   
   ' call SetPrinter to set it
   x = SetPrinter(Handle, 5, pi5, 0)
   
   ' failed the SetPrinter
   If x = False Then Exit Sub
   
   ' and close the handle
   ClosePrinter (Handle)
   
End Sub

0
 

Author Comment

by:Coates
ID: 1432542
re service packs: This is a windows 98 machine, do i need to install the service pack?
0
 
LVL 14

Expert Comment

by:waty
ID: 1432543
You need the service Pack 3 of VB or use the function  Win95SetDefaultPrinter wich work under Win95,NT,98.

One better solution is installing SVPack 3 and use anyway the function.
0
 

Author Comment

by:Coates
ID: 1432544
I have recently got a copy of VB 6.0. Is a service pack needed for this?
0
 
LVL 14

Expert Comment

by:waty
ID: 1432545
I don't think. I have not yet tested VB6.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now