We help IT Professionals succeed at work.

vb6 print via notepad

rjef
rjef asked
on
99 Views
Last Modified: 2020-08-24
any idea how I can make this not take focus when run

Private Sub Command1_Click()
Open App.Path & "\PrLab.bat" For Output As #1

Print #1, Chr(34) & "C:\Windows\Notepad.exe" & Chr(34) & " /pt " & Chr(34) & App.Path & "\Doc3.txt" & Chr(34) & Chr(32) & Chr(34) & "Label" & Chr(34)
Print #1, "exit"
Close #1
Open App.Path & "\doc3.txt" For Output As #1
Print #1, "asdf"
Close #1

Shell "c:\PrintViaNotepad\PrLab.bat", vbMinimizedNoFocus
End Sub
Comment
Watch Question

Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Can you please describe in detail what you are trying to accomplish?
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
Why creating an bat file to run it ? rather proceed like example below

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 Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

private Sub exec_program()
    ShellExecute App.hInstance, "Open", "D:\Sample.exe", "Parms", "C:\", SW_HIDE
End Sub

Author

Commented:
print a text file in vb6 using notepad

Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Why do you want to use notepad?
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
this line above should do the job directly
ShellExecute App.hInstance, "Open", "C:\Windows\Notepad.exe", " /pt " & Chr(34) & App.Path & "\Doc3.txt", SW_HIDE
Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Do you actually want to print the file?
Do you want see what it looks like in Notepad?

Author

Commented:
yes it needs to print
Louis LIETAER where do I put the printer  (in my example I have "Label"



Author

Commented:
ShellExecute App.hInstance, "Open", "C:\Windows\Notepad.exe", " /pt " & Chr(34) & App.Path & "\Doc3.txt", SW_HIDE

ShellExecute  says argument not optional

Author

Commented:
tried this
Private Sub exec_program()
   ShellExecute App.hInstance, "Open", Chr(34) & "C:\Windows\Notepad.exe" & Chr(34) & " /pt " & Chr(34) & "C:\PrintViaNotepad\Doc3.txt" & Chr(34) & Chr(32) & Chr(34) & "Label" & Chr(34), SW_HIDE
End Sub

did not work
Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
This will print the file.

Private Sub Command1_Click()


Dim F As Integer
Dim strFile As String


F = FreeFile
Open "c:\MyPath\MyFile.txt" For Input As #F
strFile = Input(LOF(F), F)
Close #F
Printer.Print strFile
Printer.EndDoc
End Sub


Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
ha you put dll declaration before ?

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

and missing parameter lpDirectory As String, corrected bellow

Private Sub exec_program() 
   ShellExecute App.hInstance, "Open", Chr(34) & "C:\Windows\Notepad.exe" & Chr(34) & " /pt " & Chr(34) & "C:\PrintViaNotepad\Doc3.txt" & Chr(34) & Chr(32) & Chr(34) & "Label" & Chr(34), "C:\" , SW_HIDE 
End Sub

Author

Commented:
Martin Liss  input past end of file

Author

Commented:
the file has special characters in it , does that matter?
Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Please attach your file.

Author

Commented:
still get ShellExecute  says argument not optional

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 Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2
Private Sub exec_program()
ShellExecute App.hInstance, "Open", Chr(34) & "C:\Windows\Notepad.exe" & Chr(34) & " /pt " & Chr(34) & "C:\PrintViaNotepad\Doc3.txt" & Chr(34) & Chr(32) & Chr(34) & "Label" & Chr(34), "C:\", SW_HIDE
End Sub
Private Sub Command2_Click()
Call exec_program
End Sub
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
sorry "C:\Windows\Notepad.exe" is the option missing

ShellExecute App.hInstance, "Open", "C:\Windows\Notepad.exe",  Chr(34) & " /pt " & Chr(34) & "C:\PrintViaNotepad\Doc3.txt" & Chr(34) & Chr(32) & Chr(34) & "Label" & Chr(34), "C:\", SW_HIDE

Author

Commented:

Author

Commented:
Martin Liss how can I just send the file to you and no one else?
Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Out of fairness to everyone, that that's not allowed.

Try my method with any file, even one that you create just for testing.
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
Sorry I am working on Mac, without vb6 :-)

notepad syntax is something like "C:\Windows\Notepad.exe /PT <filename> <printername>" why this car(34) (quote if I remember well)

have a try  with

ShellExecute App.hInstance, "Open", "C:\Windows\Notepad.exe", "C:\PrintViaNotepad\Doc3.txt Label", "C:\", SW_HIDE

Author

Commented:
LABEL is the printer name to print to
Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
If LABEL is the default printer, my code will use that printer for the output.

Author

Commented:
ShellExecute App.hInstance, "Open", "C:\Windows\Notepad.exe", "C:\PrintViaNotepad\Doc3.txt", "C:\", SW_SHOWNORMAL
shows it normally
but it needs to print to the printer name 'Label'

looks like you can not transfer anything after the file name into notepad

Author

Commented:
Martin Liss I sent you the file
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
PID = Shell("""C:\Windows\Notepad.exe"" ""/PT C:\PrintViaNotepad\Doc3.txt LABEL""", vbNormalFocus)

try this one
Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Unless you can modify your file so that it doesn't contain special characters, I don't think you can print the whole file.

Here's a way to print at least part of it. The CUTOFF Const is the number of lines that will print. You can experiment with that value to get as much as you can.
Private Sub Command1_Click()


Dim F As Integer
Dim strLine As String
Dim lngLine As Integer
Const CUTOFF = 10


F = FreeFile
Open "c:\MyPath\MyFile.txt" For Input As #F


Do While Not EOF(F)
    lngLine = lngLine + 1
    If lngLine > CUTOFF Then
        Exit Do
    End If
    Line Input #F, strLine
    Printer.Print strLine
Loop


Printer.EndDoc
End Sub

Author

Commented:
PID = Shell("""C:\Windows\Notepad.exe"" ""/PT C:\PrintViaNotepad\Doc3.txt LABEL""", vbNormalFocus)
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
So something wrong in your command

if you run the command below under cmd, it should not work:

C:\Windows\Notepad.exe /PT C:\PrintViaNotepad\Doc3.txt LABEL

Author

Commented:
this brings up the doc3.txt in notepad
C:\Windows\Notepad.exe C:\PrintViaNotepad\Doc3.txt


this
C:\Windows\Notepad.exe /PT C:\PrintViaNotepad\Doc3.txt LABEL
says
"the filename, directory name or volume label syntax is incorrect"



Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Try this.
Private Sub Command1_Click()

Dim F As Integer
Dim strLine As String

On Error GoTo ErrorRoutine
F = FreeFile
Open "c:\MyPath\MyFile" For Input As #F

Do While Not EOF(F)

    Line Input #F, strLine
    Printer.Print strLine
Loop

ErrorRoutine:
Close #F
Printer.EndDoc
End Sub

Open in new window

Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
after some research, it seem that parameter of /PT have to be quoted

can you try with cmd :
C:\Windows\Notepad.exe /PT "C:\PrintViaNotepad\Doc3.txt" "LABEL"

Author

Commented:
so how do I make this one work exaclty

pid = Shell("""C:\Windows\Notepad.exe"" ""/pt C:\PrintViaNotepad\Doc3.txt LABEL""", vbNormalFocus)

Martin LissProtect yourself and your loved ones. Stay home for the holidays.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Have you tried the code I posted here?
System Infrastructure Architect
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
works but
anyway to hide the print pop up
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
try "vbhide" instead of "vbNormalFocus" (last param of function shell)