How to see the data printed to a file

Posted on 2002-03-21
Last Modified: 2010-05-02
I created a "printer" that directs its output to a file. I look at the file in wordpad but I want to see a WYSIWYG view.

I need to write a series of reports and I do not want to kill a tree. I am looking into a Print Preview tool that would help, but I'd still like to be able to output print files and use them later.

I would also like to actually print some of these files, but how do I get the printer to understand that this is a printfile.

I have vague memories of a .prn file type.... Is there a viewer application (PrintViewer) of some kind? Or an option in Word or WordPad or....

I am hoping the answer is "Duh- just use BlahBlah" but if there is more to it I'll bump up the miserly 25 points.

Also, is there an easy way to default the outfile to clear and reuse the same print file each time?
Question by:wizard61
LVL 17

Accepted Solution

inthedark earned 25 total points
ID: 6886823
There is a simple answer emulate the printer object as a class module.  So here is some code which you can extened to meet your needs.

In the MyprClass you create all of the methods normally used in the printer. So in your report:

Dim MyPr as MyPRClass
Set MyPr = New MyPrClass

Instead of directly updating the printer you save the data to a file with a command for each operation.

You place a variable length command in the file but the trick is to prefix each command with a fixed length section to make read back simple.

You have to make some changes to you code but only small changes:

Printer.Line (10,20)- (30,40), RGB(10,20,30), BF




Public Sub Print(DataItem)

Redim Params(2)

SaveCommand Params,VarDataItem

End Sub

Public Sub Line(X1,Y1,X2,Y2,Color,BFParam)

Redim Params(6)
Params(0) = "LINE"
Params(1) = X1
Params(2) = Y1
Params(3) = X2
Params(4) = Y2
Params(5) = Color
Params(6) = BFParam

SaveCommand Params, ""

End Sub

Sub SaveCommand(Params, DataItem)

If MySpoolLfn<1 then
End if

HasFontChanged ' Call a sub that detects if font details have changed and saves a command first

dim CC as long
Dim Cmd as String

For CC=0 To UBound(Params)
    CMD = CMD + Cstr(Params(CC) + "|"
Next CC

' Rec is 10+Sep+4+Sep+Command+Data

Rec = format(len(CMD) + len(DataItem) + 16, "0000000000") + "|" + Format(Len(CMD),"0000") + "|" + CMD + DataItem

Put MySpoolLfn,Lof(MySpoolLfn)+1,Rec

End Sub

In the example above the numeric arguemnts are converted to strings.  But in graphics methods the picture to be printed will be a string of bytes.

To read back the file just get the first 16 bytes then you know how much to read:

Sub SpoolFile(PreView as Boolean, ZoomPercent)

zf = ZoomPercent / 100

If PreView Then
  Set PR  = frmDummy.picDummy
  Set Pr = Printer
End If


    If ps>Lof(MySpoolLFN) Then Exit Sub
    Get MySpoolLfn, PS, Rec
    BL = Val(Left(Rec,10)) ' Whole Length
    Rec = Space$(BL)
    Get MySpoolLfn, PS, Rec
    Process(Rec, Pr)


End Sub

Sub Process(Rec, Pr)

' First Extrct Params

ReDim Params(0) as string

TL = Val(Mid(Rec,1,10)) ' Whole length of record
CL = Val(Mid(Rec,12,4)) ' Length of Command

Params = Split(Mid(Rec,17, CL), "|")

DL = TL - CL - 16

If DL = 0 Then
    VarData = ""
    VarData = Mid(Rec, CL + 17)
End If

Select Case Ucase(Params(0))
    Case = "PRINT"
        Pr.CurrentX = Val(Params(1)) * zf
        Pr.CurrentY = Val(Params(2)) * zf
        PR.Print VarData;
    CASE = "LINE"
        X1 = Val(Params(1)) * zf
        Y1 = Val(Params(2)) * zf
        X2 = Val(Params(3)) * zf
        Y2 = Val(Params(4)) * zf
        Color = Val(Params(5))
        BFParam = Params(6)
        Select Case BFParam
           CASE = "BF"
              Pr.Line (X1,Y1) - (X2, Y2), Color, BF
        End Select
    Case = "FONT"
        Pr.Font.Name = Params(1)
        Pr.Font.Size = Val(Params(1))  * zf
        Pr.Font.Bold = Val(Params(1))
End Select

End Sub

I will leave you to do the rest, it is not rocket science.

Author Comment

ID: 6894845
After looking at this for a while, I left wishing I had the time to spend making something nice.

Is there a 3rd party ocx that anyone can suggest for this?
LVL 49

Expert Comment

ID: 7634224
Hi wizard61,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept inthedark's comment(s) as an answer.

wizard61, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 7666101
Moving to the Paq

Experts Exchange Moderator

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 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