?
Solved

Printing a text file automatically

Posted on 2003-03-25
16
Medium Priority
?
179 Views
Last Modified: 2013-12-25
Hi
I am trying to print(into a printer) a text file as it arrives from my visual basic application.

My application saves a couple of text files every now and then. Later in the day i am sending all those files to printer manually using print file option. But i would like to get it automatically done by specifying it in the VB code.
Can some one help me out please?

Thanks
ani
0
Comment
Question by:netianil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 200 total points
ID: 8203182
' add to declarations area
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(hwnd, "Print", ("C:\Textfile.txt"), "", App.Path, 0)
End Sub
0
 

Author Comment

by:netianil
ID: 8203613
Hi vinny,
I appreciate ur help. There is an error stating "Can't find DLL entry point ShellExecute in shell32.dll".
Can u solve it for me please?
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8205038
what OS are you running?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:vinnyd79
ID: 8205956
give this a try:

Private Sub Command1_Click()
PrintTextFile "C:\Somefile.txt"
End Sub


Private Sub PrintTextFile(TextFile As String)
Dim ff As Integer
Dim pl As String
ff = FreeFile
Open TextFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, pl
Printer.Print pl
Loop
Close #ff
Printer.EndDoc
End Sub
0
 
LVL 1

Expert Comment

by:babypris
ID: 8223533
Just use commondialog to print. =) Easiest way...
0
 

Author Comment

by:netianil
ID: 8225055
vinny i appreciate ur help.great response.
i do have another question. can i go ahead?
Thanks,
Ani
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8225074
sure,whats your question.
0
 

Author Comment

by:netianil
ID: 8225191
Hi vinny,
1)I am using Winsock control to connect to a camera using TCP/IP.  i am able to store .bmp files using the command DVTSID.saveimage (file name). I want my file path to be flexible by assigning in a textbox (user can change). i tried assigning a string but not fruitful. i am using VB6.0

2)i am saving a .bmp file and .txt file. I want to keep the .bmp file only if it matches the condition(varies everytime) in the .txt file or else delete it.

Thanks for taking your time in helping me out.
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8226513
have you tried something like this:

FileName = "somefile.bmp"
If Right(Text1.Text, 1) <> "\" Then
    DVTSID.saveimage Text1.Text & "\" & FileName
Else
    DVTSID.saveimage Text1.Text & FileName
End If


To check the Textfile I would write a function to return true or false. Maybe something like this:


Private Function CheckCondition(TextFile As String) As Boolean
Dim Ln As String
Dim ff As Integer
' open file
ff = FreeFile
Open TextFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
' check condition here
    If condition Is met Then
        CheckCondition = True
        Close #ff
        Exit Function
    End If
Loop
Close #ff
CheckCondition = False
End Function


Then you can use something like this to call the Function:

Private Sub Command1_Click()
If CheckCondition("C:\Somefile.txt") = True Then
    ' save the image
Else
    ' Delete it
End If
End Sub
0
 

Author Comment

by:netianil
ID: 8227717
Hi vinny,
The first one was ok.Reg the second one,my application captures images as they arrive(say around 8 pictures/min)from the camera. At the same time it opens a text file and adds line by line and those lines contain a number and a PASS or FAIL.
Ex: 0001 FAIL
0002 PASS
0003 PASS and so on.
Simultaneously a .bmp file is also saved with the file name as 0001,0002,0003.bmp and so on.

So now i want to store the .bmp file only if the txt file says the number and FAIL. Do u think it is possible?

Thanks for ur propmt replies
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8227836
Yes,definately possible.
I modified the CheckCondition Function to return "PASS" or "FAIL". To use it just call it with the name of the text file and the name of the picture.


example:

Private Sub Command1_Click()
If CheckCondition("C:\Somefile.txt", "C:\Somelocation\0001.bmp") = "FAIL" Then
    ' store the image
Else
   ' Delete it
End If
End Sub


Private Function CheckCondition(TextFile As String, PicFileName As String) As String
Dim Ln As String
Dim ff As Integer
Dim arrPic() As String
arrPic = Split(PicFileName, "\")
PicFileName = arrPic(UBound(arrPic))
PicFileName = Left(PicFileName, Len(PicFileName) - 4)
' open file
ff = FreeFile
Open TextFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
' check condition here
   If InStr(1, Ln, PicFileName) > 0 Then
       CheckCondition = "PASS"
       Close #ff
       Exit Function
   End If
Loop
Close #ff
CheckCondition = "FAIL"
End Function


Let me know if this works for you.
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8227864
Okay,I forgot to check for pass or fail in the Function.Change the above Function to this:

Private Function CheckCondition(TextFile As String, PicFileName As String) As String
Dim Ln As String
Dim ff As Integer
Dim arrPic() As String
arrPic = Split(PicFileName, "\")
PicFileName = arrPic(UBound(arrPic))
PicFileName = Left(PicFileName, Len(PicFileName) - 4)
' open file
ff = FreeFile
Open TextFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
' check condition here
   If InStr(1, Ln, PicFileName) > 0 Then
       ' pic name found,now check for pass or fail
       If InStr(1, Ln, "PASS") > 0 Then
            CheckCondition = "PASS"
       Else
            CheckCondition = "FAIL"
       End If
       Close #ff
       Exit Function
   End If
Loop
Close #ff
' return fail if pic name not found in file
CheckCondition = "FAIL"
End Function
0
 

Author Comment

by:netianil
ID: 8228114
As i mentioned earlier, i will be receiving images as they are taken by the camera, so i added the Private Command1_Click() code u mention in the following one but it shows an error "Compile error: Expected array".

Private Sub socket_DataArrival(byVal bytesTotal as Long)
0
 

Author Comment

by:netianil
ID: 8228184
As i mentioned earlier, i will be receiving images as they are taken by the camera, so i added the Private Command1_Click() code u mention in the following one but it shows an error "Compile error: Expected array".

Private Sub socket_DataArrival(byVal bytesTotal as Long)
0
 

Author Comment

by:netianil
ID: 8228189
Do u want to look at the whole code?
if so do give me ur mail address
Thanks
0
 

Author Comment

by:netianil
ID: 8228223
Do u want to look at the whole code?
if so do give me ur mail address
Thanks
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question