Solved

Printing with different fonts through API

Posted on 2004-08-30
4
874 Views
Last Modified: 2013-12-03
I need to use the API rather than the printer object because the printer I will be printing to may not exist on the local machine (though "Add Printers"), but will be accessable through the network.

I need to print a line on one font, then print a line in another font.

Someting like this:

This is Arial, 18 pt, Bold

This is New Times Roman, 12 pt, regular

What I have so far:

    Dim lhPrinter As Long
    Dim lReturn As Long
    Dim lpcWritten As Long
    Dim lDoc As Long
    Dim i
    Dim myPrinter As String
    Dim MyDocInfo As DOCINFO
    Dim sText As String
   
    myPrinter = "\\mynetwork\printer1"
    lReturn = OpenPrinter(myPrinter, lhPrinter, 0)
    If lReturn = 0 Then
        MsgBox "The Printer doesn't exist."
        Exit Sub
    End If
   
    MyDocInfo.pDocName = "Clif's Print Test"
    MyDocInfo.pOutputFile = vbNullString
    MyDocInfo.pDatatype = vbNullString
    lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)

    Call StartPagePrinter(lhPrinter)

    sText = "This is Arial, 18 pt, Bold" & vbCrLf
    'Need to change font to Arial, 18 pt, Bold
    lReturn = WritePrinter(lhPrinter, ByVal sText, Len(sText), lpcWritten)
    sText = "This is New Times Roman, 12 pt, regular" & vbCrLf
    'Need to change font to New Times Roman, 12 pt, regular
    lReturn = WritePrinter(lhPrinter, ByVal sText, Len(sText), lpcWritten)

    lReturn = EndPagePrinter(lhPrinter)
    lReturn = EndDocPrinter(lhPrinter)
    lReturn = ClosePrinter(lhPrinter)
0
Comment
Question by:Clif
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
mladenovicz earned 500 total points
ID: 11932620
Try to use CreateFontIndirect API. The CreateFontIndirect function creates a logical font that has the characteristics specified in the specified structure. The font can subsequently be selected as the current font for any device context.

http://cuinl.tripod.com/Tips/print9.htm
0
 
LVL 10

Author Comment

by:Clif
ID: 11933803
mladenovicz,

The code you provided the link to does work, however using the code for a basis in changing fonts doesn't seem to work (or I just don't know what I'm doing)

I have tried the following (modifying the code from the link you provided):
   
    lf.lfHeight = 18 * GetDeviceCaps(hPrintDc, LOGPIXELSY) \ 72
    lf.lfWidth = 0
    lf.lfEscapement = 0
    lf.lfOrientation = 0
    lf.lfWeight = 900
    lf.lfItalic = False
    lf.lfUnderline = False
    lf.lfStrikeOut = False
    lf.lfCharSet = DEFAULT_CHARSET
    lf.lfOutPrecision = OUT_DEFAULT_PRECIS
    lf.lfClipPrecision = CLIP_DEFAULT_PRECIS
    lf.lfQuality = DEFAULT_QUALITY
    lf.lfPitchAndFamily = DEFAULT_PITCH
    lf.lfFaceName = StrConv("Arial" & Chr$(0), vbFromUnicode)
    hFontArial = CreateFontIndirect(lf) 'Create the normal font
   
    lf.lfHeight = 12 * GetDeviceCaps(hPrintDc, LOGPIXELSY) \ 72
    lf.lfWidth = 0
    lf.lfEscapement = 0
    lf.lfOrientation = 0
    lf.lfWeight = 500
    lf.lfItalic = False
    lf.lfUnderline = False
    lf.lfStrikeOut = False
    lf.lfCharSet = DEFAULT_CHARSET
    lf.lfOutPrecision = OUT_DEFAULT_PRECIS
    lf.lfClipPrecision = CLIP_DEFAULT_PRECIS
    lf.lfQuality = DEFAULT_QUALITY
    lf.lfPitchAndFamily = DEFAULT_PITCH
    lf.lfFaceName = StrConv("Times New Roman" & Chr$(0), vbFromUnicode)
    hFontTimes = CreateFontIndirect(lf) 'Create the times new roman font


    'Select normal font structure and save previous font info
    hOldfont = SelectObject(hPrintDc, hFontArial)
    sText = "This is Arial, 18 pt, Bold"
    'Send text to printer, starting at location 100, 100
    result = TextOut(hPrintDc, 100, 100, sText, Len(sText))
    'Reset font back to original
    result = SelectObject(hPrintDc, hOldfont)

    'Select times font structure and save previous font info
    hOldfont = SelectObject(hPrintDc, hFontTimes)
    sText = "This is New Times Roman, 12 pt, regular"
    'Send text to printer, starting at location 100, 120
    result = TextOut(hPrintDc, 100, 120, sText, Len(sText))
    'Reset font back to original
    result = SelectObject(hPrintDc, hOldfont)

Nothing gets printed at all.  Just a blank sheet of paper emerges from the printer.
0
 
LVL 10

Author Comment

by:Clif
ID: 11935125
I'm an idiot.

The code sample I gave you that I said wasn't working wasn't exactly what I had in my VB 6 app.  In my app I had accidentally remmed out the sText = "...".  So it was printing, just empty strings.

It does work perfectly.

Thanks.
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11940228
You're welcomed ;)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to debug this code 7 64
Excel VBA, find a string in a column, update a cell 7 86
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 54
Excel Automation VBA 19 88
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

809 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