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

Sending an Array,string to printer

Status
Not open for further replies.

IknowMe

Programmer
Aug 6, 2004
1,214
US
I'm using the following Extra Basic code to attempt to send a screen scrape to a local network printer for printing.
Can anyone give some pointers in VBS as to how this could be done? There's not much EB help out there but i've been lucky so far in converting most VBS samples to work in EB with a few minor changes.
Code:
Global Sys As Object, Sess As Object, MySess As Object
'Global WshNetwork as Object,oPrinters as object, wshshell as object

Sub Main

'Set WshNetwork = CreateObject("WScript.Network")
'set WshShell = CreateObject("WScript.Shell")
'Set oPrinters = WshNetwork.EnumPrinterConnections


'For i = 0 to oPrinters.Count - 1 Step 2
'msgbox oPrinters.Item(i+1) 
'Next

'using this to see what printers are available
'still not sure how to tell which one is the default printer

Dim rc%
Dim MaxColumn%
Dim row%
Dim MaxRows%
Dim filenum%
Dim Screenbuf$
Dim linebuf$
Dim FileName$

' Get the necessary Session Object
Set Sys = CreateObject("ACCMGR.System")
Set Sess = Sys.ActiveSession
Set MySess = Sess.screen
    

If (Sess is Nothing) Then
    Msgbox "Could not create the Session object."
    STOP
End If
If Not Sess.Visible Then Sess.Visible = TRUE
Sess.Screen.WaitHostQuiet(g_HostSettleTime)
    
FileIn=FreeFile

MaxRows%=MySess.Rows()
MaxColumns%=MySess.Cols()
Screenbuf$=""

'Takes a "snapshot" of screen and stores it in variable Screenbuf$
For row%=1 to MaxRows%
    Screenbuf$=Screenbuf$+MySess.getstring(row%,1,MaxColumns%)+Chr$(13)+Chr$(10)
Next

filenum%=Freefile
FileName$="FRC014p2584417" 'name of your network printer
Open FileName$ For Output as filenum%
Print # filenum%,screenbuf$; chr$(12) 'supposed to send the scrape to printer
Close filenum%
End Sub
 
Problem solved, I had the code right the users didn't have privlidges to handle it. Anyway found a similar reference for VB. Posting it in case anyone stumbles in here looking for something similar.

Code:
'MS Windows API Function Prototypes
private 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

'---------------------------------------------------------------
' Retreive the vb object "printer" corresponding to the window's
' default printer.
'---------------------------------------------------------------
public Function GetDefaultPrinter() as Printer
    Dim strBuffer as string * 254
    Dim iRetValue as Long
    Dim strDefaultPrinterInfo as string
    Dim tblDefaultPrinterInfo() as string
    Dim objPrinter as Printer

    ' Retreive current default printer information
    iRetValue = GetProfileString("windows", "device", ",,,", _
                strBuffer, 254)
    strDefaultPrinterInfo = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
    tblDefaultPrinterInfo = Split(strDefaultPrinterInfo, ",")
    for Each objPrinter In Printers
        If objPrinter.DeviceName = tblDefaultPrinterInfo(0) then
            ' Default printer found !
            Exit for
        End If
    next

    ' If not found, return nothing
    If objPrinter.DeviceName <> tblDefaultPrinterInfo(0) then
        set objPrinter = nothing
    End If

    set GetDefaultPrinter = objPrinter
End Function


'Now to test the routine, place the following code into a Form_Load routine : 


private Sub Form_Load()
    Dim objPrinter as Printer

    set objPrinter = GetDefaultPrinter()
    MsgBox "Default printer is: " + objPrinter.DeviceName
    MsgBox "Driver name is: " + objPrinter.DriverName
    MsgBox "Port is: " + objPrinter.Port

    set objPrinter = nothing
    End
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top