Write string to text file and print?

E

Ed

A macro creates a string as its result. I'd like to create a text file,
write the string to it, print the file, and dispose of the text file. I
tried the FreeFile, Open, Write, and Print functions, but apparently I don't
understand how to use them correctly. I got a file created, but there was
nothing on it, Print didn't print it, and it saved itself with no file
extension.

Can someone point me in a good direction, please?

Ed
 
T

Tom Ogilvy

Actually you don't need to create a text file. You can write to the
printer, same as writing to the file: the below prints several lines, but
you should be able to adapt it to your situation.

Sub Macro5()
Dim ctrl as Long
Dim tmpstr as String
Open "LPT1:" For Output As #1
Print #1, "[Start of Printing Test]"
For ctrl = 1 To 10
tmpstr = "Printing:" + Str(ctrl)
Print #1, tmpstr
Next
tmpstr = "[End of printing test]" + Chr(12)
Print #1, tmpstr
Close #1
End Sub


To a Network printer


First, I went to the immediate window in the VBE to query the activeprinter
string


? activePrinter
\\ARDAPS01\1D343E on Ne02:

then I used the first part in the below code:

Sub Macro5()
Dim ctrl As Long
Dim tmpstr As String
Open "\\ARDAPS01\1D343E" For Output As #1
Print #1, "[Start of Printing Test]"
For ctrl = 1 To 10
tmpstr = "Printing Line " + Str(ctrl)
Print #1, tmpstr
Next
tmpstr = "[End of printing test]" + Chr(12)
Print #1, tmpstr
Close #1
End Sub

Worked for me.

Regards,
Tom Ogilvy
 
E

Ed

The LPT1 test worked great, Tom. Unfortunately, when I tried my string, the
printer choked.

The string is built through looping using
strChanges = strChanges & str1 & " " & str3 & vbCr
then
strChanges = "Your changes are:" & vbCr & strChanges
To print, I used
Open "LPT1:" For Output As #1
Print #1, strChanges
Close #1

I know at this point I have nine lines (I check with MsgBox). The printer,
though, seems to want to overwrite everything onto the first line. Is this
method not able to handle a multi-line string?

Ed

Tom Ogilvy said:
Actually you don't need to create a text file. You can write to the
printer, same as writing to the file: the below prints several lines, but
you should be able to adapt it to your situation.

Sub Macro5()
Dim ctrl as Long
Dim tmpstr as String
Open "LPT1:" For Output As #1
Print #1, "[Start of Printing Test]"
For ctrl = 1 To 10
tmpstr = "Printing:" + Str(ctrl)
Print #1, tmpstr
Next
tmpstr = "[End of printing test]" + Chr(12)
Print #1, tmpstr
Close #1
End Sub


To a Network printer


First, I went to the immediate window in the VBE to query the activeprinter
string


? activePrinter
\\ARDAPS01\1D343E on Ne02:

then I used the first part in the below code:

Sub Macro5()
Dim ctrl As Long
Dim tmpstr As String
Open "\\ARDAPS01\1D343E" For Output As #1
Print #1, "[Start of Printing Test]"
For ctrl = 1 To 10
tmpstr = "Printing Line " + Str(ctrl)
Print #1, tmpstr
Next
tmpstr = "[End of printing test]" + Chr(12)
Print #1, tmpstr
Close #1
End Sub

Worked for me.

Regards,
Tom Ogilvy


Ed said:
A macro creates a string as its result. I'd like to create a text file,
write the string to it, print the file, and dispose of the text file. I
tried the FreeFile, Open, Write, and Print functions, but apparently I don't
understand how to use them correctly. I got a file created, but there was
nothing on it, Print didn't print it, and it saved itself with no file
extension.

Can someone point me in a good direction, please?

Ed
 
T

Tom Ogilvy

for a printer you need to use vbCRLF, not just vbCr

--
Regards,
Tom Ogilvy




Ed said:
The LPT1 test worked great, Tom. Unfortunately, when I tried my string, the
printer choked.

The string is built through looping using
strChanges = strChanges & str1 & " " & str3 & vbCr
then
strChanges = "Your changes are:" & vbCr & strChanges
To print, I used
Open "LPT1:" For Output As #1
Print #1, strChanges
Close #1

I know at this point I have nine lines (I check with MsgBox). The printer,
though, seems to want to overwrite everything onto the first line. Is this
method not able to handle a multi-line string?

Ed

Tom Ogilvy said:
Actually you don't need to create a text file. You can write to the
printer, same as writing to the file: the below prints several lines, but
you should be able to adapt it to your situation.

Sub Macro5()
Dim ctrl as Long
Dim tmpstr as String
Open "LPT1:" For Output As #1
Print #1, "[Start of Printing Test]"
For ctrl = 1 To 10
tmpstr = "Printing:" + Str(ctrl)
Print #1, tmpstr
Next
tmpstr = "[End of printing test]" + Chr(12)
Print #1, tmpstr
Close #1
End Sub


To a Network printer


First, I went to the immediate window in the VBE to query the activeprinter
string


? activePrinter
\\ARDAPS01\1D343E on Ne02:

then I used the first part in the below code:

Sub Macro5()
Dim ctrl As Long
Dim tmpstr As String
Open "\\ARDAPS01\1D343E" For Output As #1
Print #1, "[Start of Printing Test]"
For ctrl = 1 To 10
tmpstr = "Printing Line " + Str(ctrl)
Print #1, tmpstr
Next
tmpstr = "[End of printing test]" + Chr(12)
Print #1, tmpstr
Close #1
End Sub

Worked for me.

Regards,
Tom Ogilvy


Ed said:
A macro creates a string as its result. I'd like to create a text file,
write the string to it, print the file, and dispose of the text file. I
tried the FreeFile, Open, Write, and Print functions, but apparently I don't
understand how to use them correctly. I got a file created, but there was
nothing on it, Print didn't print it, and it saved itself with no file
extension.

Can someone point me in a good direction, please?

Ed
 
E

Ed

Ah!! Thank you!
Ed

Tom Ogilvy said:
for a printer you need to use vbCRLF, not just vbCr

--
Regards,
Tom Ogilvy




Ed said:
The LPT1 test worked great, Tom. Unfortunately, when I tried my string, the
printer choked.

The string is built through looping using
strChanges = strChanges & str1 & " " & str3 & vbCr
then
strChanges = "Your changes are:" & vbCr & strChanges
To print, I used
Open "LPT1:" For Output As #1
Print #1, strChanges
Close #1

I know at this point I have nine lines (I check with MsgBox). The printer,
though, seems to want to overwrite everything onto the first line. Is this
method not able to handle a multi-line string?

Ed

Tom Ogilvy said:
Actually you don't need to create a text file. You can write to the
printer, same as writing to the file: the below prints several lines, but
you should be able to adapt it to your situation.

Sub Macro5()
Dim ctrl as Long
Dim tmpstr as String
Open "LPT1:" For Output As #1
Print #1, "[Start of Printing Test]"
For ctrl = 1 To 10
tmpstr = "Printing:" + Str(ctrl)
Print #1, tmpstr
Next
tmpstr = "[End of printing test]" + Chr(12)
Print #1, tmpstr
Close #1
End Sub


To a Network printer


First, I went to the immediate window in the VBE to query the activeprinter
string


? activePrinter
\\ARDAPS01\1D343E on Ne02:

then I used the first part in the below code:

Sub Macro5()
Dim ctrl As Long
Dim tmpstr As String
Open "\\ARDAPS01\1D343E" For Output As #1
Print #1, "[Start of Printing Test]"
For ctrl = 1 To 10
tmpstr = "Printing Line " + Str(ctrl)
Print #1, tmpstr
Next
tmpstr = "[End of printing test]" + Chr(12)
Print #1, tmpstr
Close #1
End Sub

Worked for me.

Regards,
Tom Ogilvy


A macro creates a string as its result. I'd like to create a text file,
write the string to it, print the file, and dispose of the text
file.
I there
was
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top