Solved

Printing with different fonts through API

Posted on 2004-08-30
4
878 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

739 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