Print Picture and Text Contents

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Print Picture and Text Contents

Post by D Willett »

Hi. My following code is just printing the path to my picture instead of the picture itself. This is to print a QR Code to a label printer.
I would like the QR.jpg and the contents of Me.txtEST.text to print onto one Avery label.
The QR.jpg location is: "L:\MMPDF\ConsoleFiles\" & Me.txtEST & "\" & "QR.jpg" and txtEST is a control on the current form.
I have the print dialog opening which is a start. Can anyone assist?
Kind Regards.

Code: Select all

Private Sub Command1_Click()
  
' Set CancelError is True
On Error GoTo errhandler
    
    CommonDialog1.PrinterDefault = True
    CommonDialog1.CancelError = True

    ' Set flags
    CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums
    CommonDialog1.ShowPrinter

    Printer.Print "L:\MMPDF\ConsoleFiles\" & Me.txtEST & "\" & "QR.jpg"
    Printer.EndDoc
    Exit Sub
errhandler:
    Select Case Err
    Case 32755 '  Dialog Cancelled
        MsgBox "you cancelled the dialog box"
    Case Else
        MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Picture and Text Contents

Post by HansV »

Copy the following code to the top of a module (below Option Explicit):

Code: Select all

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
  ByVal hwnd As Long, _
  ByVal lpOperation As String, _
  ByVal lpFile As String, _
  ByVal lpParameters As String, _
  ByVal lpDirectory As String, _
  ByVal nShowCmd As Long) As Long

Private Const SW_SHOWNORMAL As Long = 1
You can then use the following line to print the picture:

Code: Select all

    ShellExecute 0&, "Print", "L:\MMPDF\ConsoleFiles\" & Me.txtEST & "\" & "QR.jpg", 0&, 0&, SW_SHOWNORMAL
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

Hi Hans
Thanks for this. I'm getting the QR.jpg open up in Windows photo and fax viewer instead of printing to the printer.
I also wanted the contents of me.txtEst also. I've put an example below:
You do not have the required permissions to view the files attached to this post.
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Picture and Text Contents

Post by HansV »

You'll need different code for that. See Lesson 24 Printing with Visual Basic. The first part explains how to print a form, the second part how to send text and images to the Printer object (requires loading the pictures onto a form first).
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

Great stuff Hans. I have the form already which holds the QR Image (Picture1) and the txEST resides there also.
Kind Regards :-)
Cheers ...

Dave.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

Hi Hans. I have some code in my project already for printing. This prints the QR image but it stretches it to the width of the label.
There is a reference to the stretch properties in the code but I'm not 100% sure how this works, I also need to add the contents of the text control txtEST.
Can you see anything in the code to improve on?
Regards

Code: Select all

Private Function PrintImageFromFile(pFileName As String, Optional pOrientation As Long = 1, Optional pStretchImage As Boolean = False, Optional pEndDoc As Boolean = True) As Long
'
'   pFileName contains the name of the file to print
'   If pStretchImage is True then the image is stretched to fit the page.
'   Orientation and EndDoc are self-explanatory.
'
'   The routine assumes you've set the printer to the one you want and the paper size
'   is set. Note that no allowances are made for unprintable areas on the page.
'   I'm not sure about PostScript printers either. I haven't got one to test it on.
'

    PrintImageFromFile = 0

    If pFileName = vbNullString Then
        Err.Raise 10001, "PrintImageFromFile", "Filename parameter is empty"
        PrintImageFromFile = 1
    End If

    Dim lImageWidth As Long, lImageHeight As Long
    Dim lPrinterWidth As Long, lPrinterHeight As Long
    Dim sX As Single, sY As Single

    Dim pbStd  As StdPicture

    On Error GoTo InvalidFileName
    If Dir(pFileName, vbNormal) = vbNullString Then
        Err.Raise 10002, "PrintImageFromFile", pFileName & " does not exist"
        PrintImageFromFile = 2
    End If
    On Error GoTo 0

    On Error GoTo InvalidPicture
    Set pbStd = LoadPicture(pFileName)
    On Error GoTo 0

    Printer.ScaleMode = vbTwips  '   Set the printer scale mode to Twips

    '
    '   Set the orientation. 1 = Portrait, 2 = Landscape. Default is Portrait
    '
    '   Make sure you do this before calculating anything based on the printer object
    '   as it changes the Height and Width properties.
    '
    On Error GoTo PrinterError  '   If we can't set the orientation then
    Select Case pOrientation  '   I'm pretty sure we can't print the image.
        Case 1
            Printer.Orientation = 2
        Case 2
            Printer.Orientation = 2
        Case Else
            Printer.Orientation = 2
    End Select

    lPrinterWidth = Printer.ScaleWidth
    lPrinterHeight = Printer.ScaleHeight
    lImageWidth = pbStd.Width / 1.75  '   The 1.75 was found through experimentation
    lImageHeight = pbStd.Height / 1.75  '   Change it if you like but it works.

    '
    '   If the picture is larger than the page or we want to stretch it then
    '   scale it to the page
    '

    If pStretchImage Or lImageWidth > lPrinterWidth Then
        lImageWidth = lPrinterWidth
    End If

    If pStretchImage Or lImageHeight > lPrinterHeight Then
        lImageHeight = lPrinterHeight
    End If

    '
    '   Calculate the coordinates for the print
    '

    sX = (lPrinterWidth - lImageWidth) / 2
    sY = (lPrinterHeight - lImageHeight) / 2
    Printer.PaintPicture pbStd, sX, sY, lImageWidth, lImageHeight

    If pEndDoc Then
        Printer.EndDoc
    End If

    On Error GoTo 0

    Exit Function

PrinterError:
    Err.Raise 10003, "PrintImageFromFile", "Printer error " & _
                                           "(" & Err.Number & " - " & Err.Description & ")"
    PrintImageFromFile = 3

InvalidPicture:
    Err.Raise 10004, "PrintImageFromFile", pFileName & _
                                           " is not a supported picture format"
    PrintImageFromFile = 10004

InvalidFileName:
    Err.Raise 10005, "PrintImageFromFile", pFileName & _
                                           " is an invalid file name"
    PrintImageFromFile = 10005
End Function

Code: Select all

Private Sub Command1_Click()
  
    cd.CancelError = True
    On Error GoTo PrintImageFromFileError

    Dim x      As Long
    Dim sFileToPrint As String
    With cd
        cd.ShowPrinter
        

    End With

    sFileToPrint = "L:\MMPDF\ConsoleFiles\" & Me.txtEST & "\" & "QR.jpg"
    

    Dim lngOption As Long

    x = PrintImageFromFile(sFileToPrint, True)

    Unload Me
    Exit Sub

PrintImageFromFileError:
    Select Case Err.Number
        Case 32755
            'Do Nothing
        Case Else
            MsgBox "Error " & Err.Number & " - " & _
                   Err.Description, vbOKOnly, "Error raised"
            End
    End Select
End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Picture and Text Contents

Post by HansV »

When you use the Printer object, you're using the page as a canvas on which you "paint" images and text. Take PrintInvoice_Click from the link in my previous reply as example. I wouldn't use PrintImageFromFile - it is intended to make an image fill the page; I assume that's not what you want.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

not really. I've got the image printing correctly now by changing the following:

Code: Select all

    lPrinterWidth = Printer.ScaleWidth
    lPrinterHeight = Printer.ScaleHeight
    lImageWidth = pbStd.Width / 1.75  '   The 1.75 was found through experimentation
    lImageHeight = pbStd.Height / 3.25 '   Change it if you like but it works.
I changed the ImageHeight division from 1.75 to 3.25 which fits nicely.
I'll just need to add the txtEST contents if thats possible.
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Picture and Text Contents

Post by HansV »

Call PrintImageFromFile as follows:

Code: Select all

    x = PrintImageFromFile(sFileToPrint, 1, False, False)
Add lines like the following:

Code: Select all

    Printer.CurrentY = 500 
    Printer.CurrentX = 200 
    Printer.Print Me.txtEST
and use the following to send the page to the printer:

Code: Select all

    Printer.EndDoc
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

I was stumped for a minute then where to add the code .... it works and adds the txtEST contents. txtEST is just a job reference identifier so the QR code can be recognised.
It does seem to work better using:
x = PrintImageFromFile(sFileToPrint, True)
as the false false method doesn't print until the form is closed.
However, it works and I am grateful for the assistance.

Kind Regards
Cheers ...

Dave.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

Hi Hans
Don't know if we can do this. The code changes the default printer, is there a way to stop this?
Thanks
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Picture and Text Contents

Post by HansV »

The code itself doesn't change the printer, as far as I can tell, but perhaps the user changes it in the line

cd.ShowPrinter

Can you retrieve/set the current printer in VB6? If so, you could store the default printer in a variable at the beginning of the code, and restore it at the end.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

Cheers Hans, I'll take a look at it when I find 5.

Regards
Cheers ...

Dave.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

I found an article on this:
http://forums.codeguru.com/showthread. ... urn false;
I'm not sure how to apply it though. I understand it can be inserted into a new module but not sure how to call and applt it.

Cheers
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Picture and Text Contents

Post by HansV »

I don't think you should use that code. It relies on setting an entry in the Win.ini file - I doubt that modern versions of Windows use that.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

Cheers Hans, always use good advice.
Regards
Cheers ...

Dave.

BenCasey
4StarLounger
Posts: 495
Joined: 13 Sep 2013, 07:56

Re: Print Picture and Text Contents

Post by BenCasey »

HansV wrote: Can you retrieve/set the current printer in VB6? If so, you could store the default printer in a variable at the beginning of the code, and restore it at the end.
You might be able to use these functions (VBA)

Code: Select all

Function fShowAllPtrs()
  Dim prtLoop As Printer
  For Each prtLoop In Application.Printers
    With prtLoop
      MsgBox "Device name: " & .DeviceName & vbCr _
           & "Driver name: " & .DriverName & vbCr _
           & "Port: " & .Port
    End With
  Next prtLoop
end function

Code: Select all

Function fWhichIsDefaultPtr()
  Dim prtDefault As Printer
  Set Application.Printer = Application.Printers(0)
  Set prtDefault = Application.Printer
  With prtDefault
    MsgBox "Device name: " & .DeviceName & vbCr _
         & "Driver name: " & .DriverName & vbCr _
         & "Port: " & .Port
  End With
end function
Regards, Ben

"Science is the belief in the ignorance of the experts."
- Richard Feynman

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Print Picture and Text Contents

Post by Rudi »

Hi Sarah,

Welcome to Eileen's Lounge.
Glad to hear that this thread has been of help to you.

BTW: Cute avatar... Reminded me of the character "Lamb Chops", or is that Shawn the sheep's cousin. :grin:
1.jpg
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Print Picture and Text Contents

Post by D Willett »

Hi Sarah
I'm interested in how you are incorporating QR codes in your project, what purpose etc.
Regards
Cheers ...

Dave.

User avatar
Leif
Administrator
Posts: 7218
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Print Picture and Text Contents

Post by Leif »

Unfortunately, due to the high possibility that her motives are incompatible with the house rules, Sarah may be unable to answer in person as her access to Eileen's Lounge has been severely restricted.

PS: Rudi - I think you mean Shaun the Sheep
Leif