Printing Text Files from VBA

D

Don Wiss

I'm working on a large application that now calls APL through an ActiveX
server. APL manages a bunch of databases, does calculations, formats
reports, and prints them to the default printer. All interface with the
user is already in Excel/VBA. I'm working to eliminate the APL. Access can
take over the APL component files. VBA, of course, can handle the
calculations, and in some cases more easily, using the built in BetaDist
and GammaLn functions.

With help here I'm now reading and writing small text files, which APL did
before. Next along this line is to print them. In APL I have a rather
sophisticated formatting routine that the APL people wrote for me. But to
start I'd be happy in just sending the text file to the printer. Maybe
someday I can replicate the formatting that APL does, especially the fit to
page capability.

Don <donwiss at panix.com>.
 
T

Tom Ogilvy

the easiest would be to open the file in excel and print it from there. Or
you could use the low level fileio and parse it into excel, then print it
from excel

Another would be to open it in word and print it from there

You can wite it directly to the printer a line at a time or perhaps more
than a line at a time, same as you read it in.
 
D

Don Wiss

the easiest would be to open the file in excel and print it from there. Or
you could use the low level fileio and parse it into excel, then print it
from excel

Another would be to open it in word and print it from there

You can wite it directly to the printer a line at a time or perhaps more
than a line at a time, same as you read it in.

Actually I miswrote my request. The text I want to print is already in
Excel as strings. For now I used Excel to print. It is a bit slow the way I
did it. I'm sure writing it directly to the printer would be faster, but at
least the Excel way lets me play with formatting. I'll probably start
enhancing the below. At least change the format based on the ending value
of j.

Sub PrintText(S As String)
' arg: CR/LF delimited text string
' reads: global ViewerType

Dim SheetName As String
Dim i As Integer, j As Integer, T As String

Application.StatusBar = "Printing..."
SheetName = ActiveSheet.Name

' create hidden sheet
Application.ScreenUpdating = False
Sheets.Add
Range("A:A").ColumnWidth = 100
Columns("A:A").WrapText = True
Cells.Font.Name = "Courier New"

' place text on sheet
For i = 1 To Len(S)
If Mid(S, i, 1) <> Chr(13) Then
T = T & Mid(S, i, 1)
Else
j = j + 1
Cells(j, 1).Value = T
T = ""
i = i + 1
End If
Next i

' print
With ActiveSheet
With .PageSetup
If ViewerType = 1 Then
.CenterHeader = "WC Parameter History"
ElseIf ViewerType = 2 Then
.CenterHeader = "Sheet Instructions for: " & SheetName
End If
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
.PrintOut

' delete temp sheet
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
End With

' put people back where they were
Sheets(SheetName).Select
Application.StatusBar = False

End Sub

Don <donwiss at panix.com>.
 

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