Printing Text File in VB 6


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Printing Text File in VB 6

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Location
    Bangalore, India
    Posts
    5

    Exclamation Printing Text File in VB 6

    Hi,

    My program maintains a log file (text file) which records the activities done. I'm trying to use the Printer Object to print the file but it's not printing properly. I tried using the PrintDocument Component too but am getting lots of errors. There is a menu bar control which when invoked should print the entire contents of the file.

    I don't want the user to specify what to print. Instead, the control should directly print the entire file to the default printer. I'm & don't know what to do. Please help.

    Regards,

    thunder777

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I would use the ShellExecute function:
    http://msdn.microsoft.com/archive/en...n_shelexec.asp
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Jan 2006
    Location
    Bangalore, India
    Posts
    5

    Exclamation Re: Shell Execute

    Hi,

    I tried to use this in a std module :

    Public Const SW_SHOWNORMAL = 1
    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByValhwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    & dis in my form :

    Public Sub PrintThisFile(FileName As String)
    Dim X As Long
    X = ShellExecute(FormName, "Print", FileName, vbNullString, 0&, SW_SHOWNORMAL)
    End Sub

    Unfortunately, it doesn't throw up any error. It just keeps quiet & there's no printout.

    Can i have some sample code to print the text file ?

    Regards,

    thunder777

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    What are you passing in the first parameter (FormName)? It should be a window handle (a Long integer). Try this:

    X = ShellExecute(Me.hWnd, "Print", FileName, vbNullString, 0&, SW_SHOWNORMAL)

    If that still doesn't work, post the value of X.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Jan 2006
    Location
    Bangalore, India
    Posts
    5

    Thumbs up Re: Solved

    Hi,

    I got it. Now it works. Thanks a lot.

    Regards,

    thunder777

  6. #6
    Join Date
    Oct 2008
    Posts
    2
    May I have the entire detailed working code cuz I tried using it to print pdf docs but few of the docs are still skipped with out any error.

    Also I would like to know whether the behaviour of shell32 dll is machine dependent.

    Thanks for your help

  7. #7
    Join Date
    Oct 2008
    Posts
    2
    Below is the code that I am using :

    Private Sub PrintPDFs(PDFfile As String)
    Dim lng As Long

    lng = ShellExecuteEx(Me.hwnd, "Print", PDFfile, vbNullString, 0&, 1)
    Sleep (3500)

    Call TerminateProcess(lng, 0)
    CloseAcrobat
    End Sub

    where sleep function is

    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    terminate process is
    Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

    and shellExecute is
    Public Declare Function ShellExecuteEx 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

    and closeAcrobat is

    Private Sub CloseAcrobat()

    On Error Resume Next

    'On Error GoTo ErrHandler

    Dim sCmd As String '' DDE command

    txtAcrobatDDE.LinkMode = 0
    txtAcrobatDDE.LinkTopic = "acroview|control"
    txtAcrobatDDE.LinkTimeout = 300 ' 30 sec timeout delay.
    txtAcrobatDDE.LinkMode = 2

    sCmd = "[AppExit()]"
    txtAcrobatDDE.LinkExecute sCmd

    '' Close the DDE Connection
    txtAcrobatDDE.LinkMode = 0
    blnAcrobatLaunched = False

    Exit Sub

    'ErrHandler:
    'MsgBox "An error occured. Cannot Close Acrobat Reader cancelled." & " Error# " & Err.Number & vbCr & Err.Description, vbCritical, "Error: PrintPDFs"

    End Sub

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by padmajage123
    May I have the entire detailed working code cuz I tried using it to print pdf docs but few of the docs are still skipped with out any error.
    How do you know there is no error? If you use On Error Resume Next, the error will never display. Get rid of that and see if an error does occur. Then, we can fix it.
    Quote Originally Posted by padmajage
    Also I would like to know whether the behaviour of shell32 dll is machine dependent.
    That is a part of the Windows OS
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Reading and writing lines from a text file...
    By Jenks in forum VB Classic
    Replies: 6
    Last Post: 05-24-2005, 02:22 PM
  2. File Locking a Text File on a Network Drive
    By Miles Tones in forum VB Classic
    Replies: 1
    Last Post: 05-20-2005, 01:57 PM
  3. importing text file to SQL DB using DTS and VB
    By Ajay M in forum VB Classic
    Replies: 0
    Last Post: 02-11-2002, 02:25 AM
  4. They created J#, why couldn't they do VB#?
    By Thomas Eyde in forum .NET
    Replies: 290
    Last Post: 12-22-2001, 02:13 PM
  5. Replies: 0
    Last Post: 04-17-2000, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center