Solved

Font Size

Posted on 1998-04-19
11
388 Views
Last Modified: 2013-12-03
I am formatting a printed page.  The upper portion is a graphic, the lower portion is text.  The vertical page space occupied by the text portion varies from one run to the next.  The upper graphic is scaled to fit in the vertical space remaining.  So:  My program does a "dummy run" through the text portion, in order to obtain the vertical space required for that.  Then the program scales the graphic, prints it, then prints the text. This process results in Printer.Font.Size generally less than 8, typically around 6 or 7.  Consequently, I have to use "Small Font".  The skeleton of my program looks like this:

Print a header using default font and size.
YHEAD = Printer.TextHeight("A") ' -- Vertical space taken up by the header
' -- start the dummy text run --
Printer.Print " "
Printer.Font.Size = 7
Printer.Font.Name = "Small Font"
Printer.Font.Size = 7
DELY = Printer.TextHeight("A")
---
--- Compute the text portion of the page, keeping track of number of lines printed, NLINES
---
YTEXT = DELY * NLINES ' -- This is the vertical space taken up by the text.
YGRID = (9.5 * 1440) - YHEAD - YTEXT ' This is the vertical space available for the graphic
                9.5 inches times 1440 twips/inch
' -- the graphic is now computed.  It also contains some text, the size of which, FSIZE, is computed from YGRID
Printer.Print " "
Printer.Font.Size = FSIZE
Printer.Font.Name = "Small Font"
Printer.Font.Size = FSIZE
--
-- the graphic is printed
--
Printer.Print " "
Printer.Font.Size = 7
Printer.Font.Name = "Small Font"
Printer.Font.Size = 7
--
-- the text is now printed
--
Printer.EndDoc
End Sub
***********************************
Problem:  The first time through, all the above works perfectly.  After that, the program absolutely ignores the first setting of font size, in the skeleton above.  The "header" default is retained.  If I kill VB, restart it and run the program, it again works perfectly, but just the FIRST time.  I've asked this question here before, but not in all this detail.  (Also under signon of "normpos").  

Can somebody please tell me what is going on here?  Please pretty please? -- Norm
0
Comment
Question by:posnorm
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 4

Expert Comment

by:zsi
ID: 1425270
What OS are you running this program under?  NT4.0, in particular, has a problem with printing, but it is fixable.
0
 
LVL 1

Expert Comment

by:Lint
ID: 1425271

Someone once told me that when changing font sizes you have to add a doevents after the font change. Never had occation to test that out.

0
 

Author Comment

by:posnorm
ID: 1425272
Lint:  Thank you for your reply.  I am running VB5.0 with Windows 95.  I will try "doevents" and get back to you.  Some other possibilities:  I'm not sure if the Print "" should be a space, or a blank between the quote marks.  Also, maybe a semicolon after the "" is necessary.  I don't know.  It's witchcraft, and it ought to be straightforward. -- Norm
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:posnorm
ID: 1425273
zsi, Lint, and anybody else:  I put a DoEvents after every Font.Size change.  The problem described above is cured!  Curiously, an opposite problem popped up:  The text formatting makes a lot of fancy use of Text.Width.  The FIRST time the program is run, the Font Sizes all work correctly, but one of the Text Widths acts like the Font Size is the system default!  Running the program again (or again), and everything works right.  I make a hokey work around:  I put everything inside a Do Loop which loops twice.  The first time goes without actually printing anything, and the second time enables all the prints.  I am living with this fix, but would dearly love to know what the heck is going on. --Norm
0
 
LVL 1

Expert Comment

by:Lint
ID: 1425274
Happy that helped!

Print
 or
print ""

should be the same.
0
 

Author Comment

by:posnorm
ID: 1425275
Everybody calm down.  After a great deal of fussing around, I can state my fundamental problem like so:

Printer.Font.Name -- and -- Printer.Font.Size -- somehow do not "stay put".  On bended knee I beseech the Visual Basic Gods out there:  What do I have to do, once those two statements come and go in the code, to PRESERVE what they supposedly accomplished?  What makes them "go away"?  What ought to be done, or not done, following those lines of code, to FORCE them to do what they are supposed to do?

I run EXACTLY the same code on two different machines (both Windows 95) and get different results.  Sometimes it acts right only the second time around.  Sometimes only the first time.

I have tried "Dim FA As New StdFont" and "Dim FB As New StdFont", then in the code, I have "Set Printer.Font = FA" -- then some printing -- the "Set Printer.Font = FB --the some printing.  Machine A:  The first printing takes the system font and size default, and the second printing takes FA!  Machine B:  The first printing takes the system default, but the second printing takes the size of FA with the font of FB.

I really do not know what to do.  I have also posted this question under logon "normpos"

Anybody???  -- Please.  -- Norm
0
 

Author Comment

by:posnorm
ID: 1425276
Here is a little test program to illustrate the problem:
**********************************
Private Sub Command1_Click()
Dim HA As String
Dim HB As String
Dim FA As New StdFont
Dim FB As New StdFont
Dim YA As Integer
Dim YB As Integer
HA = "HEADER"
HB = "Some Text"
Printer.Orientation = 2
Printer.Print " "
FA.Name = "Arial"
FA.Size = 12 * 3
Printer.Print " "
FB.Size = 3 * 3
FB.Name = "Small Font"
FB.Size = 7 * 3
Printer.Line (700, 700)-(14600, 10900), , B
'
Printer.Print " "
Set Printer.Font = FA
YA = Printer.TextHeight(HA)
Printer.CurrentX = 700
Printer.CurrentY = 700
Printer.Print HA
'
Printer.Print " "
Set Printer.Font = FB
YB = Printer.TextHeight(HB)
Printer.CurrentX = 700
Printer.CurrentY = 700 + YA
Printer.Print HB
'
Printer.CurrentX = 700
Printer.CurrentY = 700 + YA + YB
Printer.Print HB
'
Printer.EndDoc
End Sub
***************************
Notes:  I'm trying the above on two different machines, two different locations.  The Printer.Line is to draw a box, to convince me that the "twips per inch" is the same on both machines.  It is.  The multiplication by 3 in the three .Size statements is curious:  On one machine, without multiplying by three, all three .Size statements yield expected results.  On the other machine (different printer), the font size comes out about 1/3 what is expected.  I'd like to know why that is.  Replacing the "StdFont" stuff with conventional Printer.Font.Size and Printer.Font.Name statements does not have that difference between machines.

But my primary problem, is that the printed result does not "obey" these instructions.  Mysteriously, it SOMETIMES does.  I can run the above program a number of times in succession.  When the program "obeys" is not predictable.  On one machine it will run correctly all day long, then fail the first time the next day.  On the other machine, it may run right the first time it is run, then not after that.  "Run Incorrectly" means that the system default font and size is sent to the printer.  A true bafflement.  I'm getting annoyed too. -- Norm
0
 
LVL 7

Accepted Solution

by:
kamall earned 200 total points
ID: 1425277
Here is the secret:
Fix Printer Object Font/Paging Quirk
An ‘undocumented feature’ of VB4’s Printer Object requires that each new page be initialized before the font can be changed.
You would think that the following code would change the current font of the default printer in VB4, but you’d be wrong:
With Printer.Font
..Name = "Arial"
..Size = 11
End With

sText = "What a waste it is to lose one’s mind."
Printer.Print sText
An ‘undocumented feature’ of VB4’s Printer Object requires that each new page be initialized before the font can be changed. This code works as expected:
' Start new page
Printer.Print
With Printer
' Set margins as desired
..ScaleMode = vbTwips
..CurrentX = 720
..CurrentY = 720
' Now you can set the font
With .Font
..Name = "Arial"
..Size = 11
End With

End With

sText = "If we don’t succeed, we run the risk of failure."
Printer.Print sText
Printer.EndDoc

Regards
0
 

Author Comment

by:posnorm
ID: 1425278
kamall:  Thank you from the bottom of my heart.  I have asked this question in various forms both here and elsewhere a number of times.  Yours is the first EXPLANATION.  It's really frustrating to run into these sorts of poorly documented "behavioral" problems both in VB and other languages.  --  Norm
0
 
LVL 7

Expert Comment

by:kamall
ID: 1425279
Norm,
Thanks, and I am glad that I could help.
Best regards.
0
 

Author Comment

by:posnorm
ID: 1425280
kamall:  I'm still having trouble.  The sequence of printer.such-and-such is important.  Please sketch out the proper sequence for the following:

Command_Click()
Printer.SetMargins-----
prtA ' sub which prints some text
prtB ' sub which prints some graphics and text, same sheet of paper.
Printer.EndDoc
End Sub
**********
PrtA(stuff for use in PrtB)
Does a dummy run through the text to determine required paper "real estate"
Sets font size (may be "SmallFont")
Prints
End Sub
**********
PrtB(stuff obtained from PrtA)
Uses information from PrtA to determine remaining paper "real estate"
Sets a different font size than that used in PrtA
Loops through some data, and for each item:
   Computes CurrentX and CurrentY
   Draws a box with Printer.line (x, y)-(x, y),,B
   Prints some computed text in the box.
End of loop
*****************
------Now, if the box is printed BEFORE or AFTER the text is printed, the resulting font size is altogether different.  Why?  Maybe I should make two loops, print all the boxes, THEN set Font.Size and loop again to print all the text.  Please comment -- Norm
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 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