• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10465
  • Last Modified:

HOWTO PRINT FROM VISUAL BASIC 6

I need to know howto print from visual basic 6. I already know about the paintpicture method. I want to print a textfile.
0
tim102096
Asked:
tim102096
5 Solutions
 
vinnyd79Commented:
You could use ShellExecute API:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
Call ShellExecute(0, "Print", ("C:\Somefile.txt"), "", "", 0)
End Sub
0
 
PreeceCommented:
Shamelessly copied from another question:

See the microsoft KB article:

HOWTO: Use ShellExecute to Launch Associated File (32-bit)
http://support.microsoft.com/support/kb/articles/Q170/9/18.asp?LNG=ENG&SA=MSDN&FR=1 


In the example you will see the line:

   StartDoc = ShellExecute(Scr_hDC, "Open", DocName, _
          "", "C:\", SW_SHOWNORMAL)
     

If you change the "Open" to "Print" in this line, it will print the document to the default printer using the native application that the file belongs to.  For example, a .DOC file is associated with Word.  This will print the document through word...


By the way, if you dont want to have the application visible, you would change that line to:

   StartDoc = ShellExecute(Scr_hDC, "Print", DocName, _
          "", "C:\", 0)



Cheers!
 
0
 
BurbbleCommented:
You mentioned the PaintPicture method; does that mean that you want to Print text, as in draw it on the screen?

You can do this using a Picture Box control, similar to the PaintPicture method except with text. First load the text you want into a file:

Dim strText as String
Dim FileNo as Integer

Open App.Path & "\Text.txt" for Input as #FileNo
    strText = Input(LOF(FileNo), #FileNo)
Close #FileNo

...and then you can print it to a Picture Box:

Picture1.CurrentX = 0
Picture1.CurrentY = 0
Picture1.Print strText

You can also set the foreground colors, font, etc.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PaulHewsCommented:
You can print simple text as well as print pictures using the Printer object:

http://www.freevbcode.com/ShowCode.asp?ID=3646

Here's some simple code to dump a text file line by line to the printer

Private Sub PrintTextFile(strPath As String)
    Dim hFile As Integer
    Dim strLine As String

    hFile = FreeFile
    Open strPath For Input As #hFile
    Do While Not EOF(hFile)
        Line Input #hFile, strLine
        If Printer.CurrentY + Printer.TextHeight(strLine) > Printer.ScaleHeight Then
            Printer.NewPage
        End If
        Printer.Print strLine
    Loop
    Printer.EndDoc
    Close #hFile
End Sub
0
 
VIMALCHANDCommented:
Function PrintFile(FileName) 'filename is the name of the file with its path c:/autoexec.bat"
open filename for input as #1
open "lpt1" for output as #1 'printer port
do while not eof(1)
lineinput #1,xx
print #2,xx
loop
close #1
close #2
end function
0
 
tim102096Author Commented:
Thankyou. This is exactly what i wanted.
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now