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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print to USB port

Status
Not open for further replies.

stilley

Programmer
Jan 16, 2003
52
0
0
CA
Hi,

I am using VB 6 to create a simple program to print labels. I have no problem printing to an lpt1 or tcp port but am not successful in printing to a USB port. Any suggestions?

Thanks,

Shannon
 
Can anything else (notepad) print to the printer?

You may be working at too low a level. Try using the Printer object in VB and let the Windows printer driver do the work.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks, I have it working now but also I would like to be able to set my VB program to only print to the USB printer. Even though another tcp or lpt1 printer may be the default printer on the PC, I only want my VB app to print to the USB thermal printer. Is there code I could hard-code to tell it to print there only?
 
Hardcode" is such an ugly word ...

You could enumerate the Printers collection and check the "DeviceName" property which is structured as

"PrinterName, DriverName, PORT"

then

Split(Pr.DeviceName,",")(2)

Should tell you which port it's on and you can then select the printer with a port beginning with "USB" to do your printing.
 
I'm new at this, not really sure what you mean?

 
OK. VB has a built in collection called "Printers" and it contains all the printers defined in the system. "Enumerating" a collection means that you cycle through each of the printers and select one of them to do your printing. Something like this
Code:
Dim Pr As Printer
Dim myUSBPrinter As Printer
For Each Pr In Printers
   If Instr ( Pr.Port, "USB" ) > 0 Then
      [COLOR=Green]' Found a USB Printer[/color]
      Set myUSBPrinter = Pr
      Exit For
   End If
Next Pr

If myUSBPrinter Is Nothing Then
   Msgbox "No USB Printer Found"
Else
   [COLOR=Green]' Now you can print to myUSBPrinter[/Color]
   Dim OldDefaultPrinter As String
   OldDefaultPrinter = SetPrinterAsDefault(   myUSBPrinter.DeviceName )

   myUSBPrinter.Print "This is a String to Print"

   myUSBPrinter.EndDoc

   SetPrinterAsDefault( OldDefaultPrinter )

End If

And SetPrinterAsDefault is

Code:
Public Function SetPrinterAsDefault(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
    
    [COLOR=green]' Windows wants "Device Name,Driver Name,Port" in the string to set
    ' a new default printer.[/color]
    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

    [COLOR=green]' Get the path of the Windows\System directory.[/color]
    strWinPath = Space$(MAX_FILENAME_LEN)
    String_Len = GetWindowsDirectory(strWinPath, 256)
    strWinPath = Left$(strWinPath, String_Len)
    strSysFileName = strWinPath & "\win.ini"

    [COLOR=green]' Get the current default printer
    ' This will be of the form 'DeviceName,DriverName,Port'
    ' For example "iDP3210 Full Cut,iDP3210,LPT1:"[/color]
    strRet = Space$(256)
    String_Len = GetPrivateProfileString("windows", "device", "", strRet, 256, strSysFileName)
    Current_Default = Left$(strRet, String_Len)
    
    [COLOR=green]' If he didn't provide a new printer name then just return the current default.[/color]
    If Len(New_Printer) = 0 Then
        nC = InStr(1, Current_Default, ",")
        If nC = 0 Then nC = Len(Current_Default) + 1
        SetPrinterAsDefault = Left$(Current_Default, nC - 1)
    End If
End Function
 
Thanks, this is a big help and most of this works when stepping through the code except:

For Each Pr In Printers
If Instr ( Pr.Port, "USB" ) > 0 Then
' Found a USB Printer
Set myUSBPrinter = Pr
Exit For
End If
Next Pr

it still detects it as "nothing"

for some reason it doesn't see the USB001 port.
 
Send Pr.Port to the console via a Debug.print and see what it finds. It could be under a different name.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
it says "USB001" but line "Set myUSBPrinter = Pr" still passes it as "nothing"

and even when I change my code to look like this:

For Each Pr In Printers
If Instr ( Pr.Port, "USB001" ) > 0 Then
' Found a USB Printer
Set myUSBPrinter = Pr
Exit For
End If
Next Pr


still nothing...
 
Works on my system but XP is a bit weird about printers. Try

DoEvents
Set myUSBPrinter = Pr

... he muttered, grasping at straws ...
 
You might try stepping through the for loop, and looking at all of the values for each printer as you go.

Bob
 
Ya I tried stepping through the loop and I saw every printer on my system but for some very odd reason the USB001 value is not getting passed. Very strange.
 
Is there an ocx or something that I should be using so VB 6 can see my USB port? What am I missing?
 
In addition to Pr.Port, check the Pr.DeviceName property to see if the printer you want is being found. If you have renamed or redirected ports it's possible that the printer knows the port as something other than USB001.
 
I just realized this code is not working for any of my printers, LPT1, BLP2 etc... It finds them all even the USB but won't set the myUSBPrinter value to be it.
 
Hi Golom, Thanks for all your help! I was able to get this working to a certain point. I was wondering if you could post your code for the 2 procedures: GetWindowsDirectory and GetPrivateProfileString ?

Thanks,

Shannon
 
Those aren't VB procedures. They are Windows API calls. You will also need "WritePrivateProfileString" and "SendMessage". Just paste these in the declarations section of a module.
Code:
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
 
Thanks,

Now it is telling me that MAX_FILENAME_LEN variable is not defined. Is this something I need to define?
 
Sorry ...

Add the line

Public Const MAX_FILENAME_LEN = 256

to your module.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top