Examples


Top  Previous  Next


Below are a few examples that demonstrate how to use the Raw Data Printer object to print from your VB applications, ASP pages, HTML pages and other programming languages such as VB.NET, C#.NET, ASP.NET, VBA, VC++, Delphi, MS Office (Word, Excel, Access, PowerPoint), etc., using just a single line of code!

For ready-to-use examples and sample code that demonstrate the use of all Raw Data Printer object features, please refer to the demo projects supplied with the component. Other sample code or projects may be available on the VBGold web site.

Hint: All the examples below can be used almost 'as is' in any programming language (VB, VB.NET, ASP, HTML, VB Script, Java Script, etc.). You will just need to modify the line that creates the object. For example:


VB, VBA & VB Script:   
Set RDP = CreateObject("RawDataPrinter.Printer")

VB.NET:         
RDP = CreateObject("RawDataPrinter.Printer")
ASP
:            
Set RDP = Server.CreateObject("RawDataPrinter.Printer") 
Java Script:      var RDP = new ActiveXObject('RawDataPrinter.Printer')




red_bullet Example 1: Sending raw data (text) and printer commands to the printer, without ejecting the page  


'Declare and create an object instance of Raw Data Printer   
'(alternatively, you can use the CreateObject method to create an object instance)  
Dim RDP As New RawDataPrinter.Printer  
 
'Print some raw data string  
RetVal = RDP.PrintRawData "This raw data is printed by Raw Data Printer!"  
 
'Send an escape command (set bold printing) with some raw data string (ESC E + text)   
RetVal = RDP.PrintRawData Chr(27) & "E" & "This raw data is printed by Raw Data Printer!"  
 
'Clean up  
Set RDP = Nothing  
 



red_bullet Example 2: Printing raw data to a specific printer attached to the system, and eject the page  


'Declare and create an object instance of Raw Data Printer  
Dim RDP As New RawDataPrinter.Printer  
 
'Print some raw data string to a specific printer and send a FormFeed to eject the paper  
'Note that the printer name must be specified exactly as it is returned by the GetPrinters method,  
'which actually is the same name that appears in the Windows Control Panel (Printers and Faxes)  
RetVal = RDP.PrintRawData "This raw data is printed by Raw Data Printer!", , "EPSON Stylus Photo R200 Series", , True  
 
'Clean up  
Set RDP = Nothing  
 
 


red_bullet Example 3: Printing raw data from a text file and ejecting the page  


 
'Declare and create an object instance of Raw Data Printer  
Dim RDP As New RawDataPrinter.Printer  
 
'Print the contents of a text file (as raw data) and send a FormFeed to eject the paper  
RetVal = RDP.PrintRawData , "C:\MyFile.txt", , , True  
 
'Clean up  
Set RDP = Nothing  
 



red_bullet Example 4: Printing data using both the Data method argument and from a file in just a single call to the PrintRawData method  


'Declare and create an object instance of Raw Data Printer  
Dim RDP As New RawDataPrinter.Printer  
 
'Print data supplied from code in the Data argument and then print the contents of a text file (as raw data)  
RetVal = RDP.PrintRawData "This data is supplied from code in the Data argument!" & vbCrLf & vbCrLf, "C:\MyFile.txt"  
 
'Clean up  
Set RDP = Nothing  
 



red_bullet Example 5: Printing customer orders from an ASP page running on a web server, to a network printer on the server. Orders are submitted from the client web browser (from an HTML form)  


'Create the Raw Data Printer object  
Set RDP = Server.CreateObject("RawDataPrinter.Printer")  
 
'Get data from the appropriate HTML form fields  
CustomerName = Request.Form("CustomerName")  
CustomerAddress = Request.Form("CustomerAddress")  
CustomerPhone = Request.Form("CustomerPhone")  
ItemNumber = Request.Form("ItemNumber")  
ItemDescription = Request.Form("ItemDescription")  
UnitPrice = Request.Form("UnitPrice")  
Qty = Request.Form("Qty")  
TotalPrice = Request.Form("TotalPrice")  
 
           
'Compose the raw data string from the appropriate HTML fields  
OrderData = CustomerName & vbCrLf & CustomerAddress & vbCrLf & CustomerPhone & vbCrLf & ItemNumber & vbCrLf & ItemDescription & vbCrLf & UnitPrice & vbCrLf & Qty & vbCrLf & TotalPrice  
           
'Send data to a network printer and issue a FormFeed to eject the page  
'Note, the printer name should be exactly as returned by the GetPrinters method  
RetVal = RDP.PrintRawData OrderData, , "\\ServreName\Network Printer Name Here", , True, , , ErrorNumber, ErrorDescription  
 
'Clean up  
Set RDP = Nothing  
     
'Notify the user for print completion (or any errors)  
'RetVal is True if printing was successful, False if an error occurs  
If RetVal = True Then     
   Response.Write "Your order was printing successfully on the server!"  
Else  
   Response.Write "Order print error!" & <br> & "Error Number= " & ErrorNumber & <br> & "Error Description= " & ErrorDescription  
End If  
 



red_bullet Example 6: Printing a sample ticket on a pre-printed ticket form, from an HTML page viewed in a web browser, using VB Script  


'Create the Raw Data Printer object  
Set RDP = CreateObject("RawDataPrinter.Printer")  
 
'Get ticket data from the appropriate HTML fields  
TicketCode = document.Form1.TicketCode.value  
TicketName = document.Form1.TicketName.value  
DeparturePort = document.Form1.DeparturePort.value  
ArrivalPort = document.Form1.ArrivalPort.value  
DepartureDate = document.Form1.DepartureDate.value  
ArrivalDate = document.Form1.ArrivalDate.value  
Price = document.Form1.Price.value   
           
'Compose the raw data string from the appropriate HTML fields  
'Also, because the printing is performed on a pre-printed ticket (form), some spaces should be appended  
'to the data parts, in order to print on the appropriate positions on the ticket  
StrToPrint = "           " & TicketCode & vbCrLf  
StrToPrint = StrToPrint & "           " & TicketName & vbCrLf  
StrToPrint = StrToPrint & "           " & DeparturePort & "            " & ArrivalPort & vbCrLf  
StrToPrint = StrToPrint & "           " & DepartureDate & "            " & ArrivalDate & vbCrLf  
StrToPrint = StrToPrint & "           " & Price  
           
'Send the document to the Raw Data Printer object and issue a FormFeed to eject the ticket  
'You can also specify the printer name, to which the document will be sent  
RetVal = RDP.PrintRawData StrToPrint, , "EPSON Stylus Photo R200 Series", , True, , , ErrorNumber, ErrorDescription  
 
'Clean up  
Set RDP = Nothing  
     
'Notify the user for print completion (or any errors)  
'RetVal is True if printing was successful, False if an error occurs  
If RetVal = True Then     
   MsgBox "Ticket Printing Completed Successfully!",,"Raw Data Printer"  
Else  
   MsgBox "Print Error!" & vbCrLf & "Error Number=" & ErrorNumber & vbCrLf & "Error Description=" & ErrorDescription,,"Raw Data Printer"  
End If  
 



red_bullet Example 7: Retrieving all the physical and logical printers connected to the system on the client machine from within the user's web browser, using VB Script in an HTML page  


'Create the Raw Data Printer object  
Set RDP = CreateObject("RawDataPrinter.Printer")  
 
'Query all physical printer devices attached to the system  
'This will also retrieve any logical printers (such as FAX printer driver, PDF printer driver, etc.)  
'The method returns a variant array (string) containing the names of the printers found  
Printers = RDP.GetPrinters(PrintersCount)  
 
P = PrintersCount & " printers were detected on your system:" & vbcrlf & vbcrlf  
     
For i=0 To UBound(Printers)  
   P = P & Printers(i) & vbcrlf  
Next  
 
'Clean up  
Set RDP = Nothing  
     
'Show the names of detected printers  
MsgBox P,,"Raw Data Printer"