print jpg files from vba

etsoft

Registered User.
Local time
Today, 02:48
Joined
Nov 14, 2003
Messages
15
Hi,

is there a way to print jpg or other graphic files from vba code like you do with office files ?

Thanks in advance

Erwin
 
Files on a disk is mere data storage of the item that data pertains to. A file is really nothing more than a blob of 1's and 0's. The display of a file is determined by what container or interface it goes into to make it visible and workable. This is why files generally contain a file extension. With the extension, we can clearly see what container can display or enable the file. This includes MS-Office Files.

Graphics files are no different and therefore need to be placed into a container to work with them. A MS-Access Report is one such container since it does hold a built in mechanism to display graphics....the Picture property. With the use of this property, you can therefore think of a report as a glorified Image Control.

Understandably you don't want to display a report of the image, you merely want to print it. This can be done using a Report object but instead of displaying the report we just keep it hidden. The idea is to dynamically create a temporary Report Form, place the desired image into the Picture property of the Report, print the report, then when we're done...delete the Report Object.

The code below demonstrates the printing of a single image where as the image path and file name is pulled from a Table Field. If there are a series of images to print (page per image) then you definitely don't want to create and delete a Report Object for each image. You will want to iterate through those images and change the Picture Property while the form is created then after all the images have been placed into the printer spool, delete the Report Object. You will need to tweak the code to suit whatever needs you have.

Place this code wherever you deem necessary:

Code:
   [COLOR="DarkGreen"]'Declare Variables...[/COLOR]
   Dim MyPicFile As String
   Dim rpt As New Report
   Dim RptName As String
   
   [COLOR="DarkGreen"]'Fill the MyPicFile string variable with the name
   'of the image to print. Here we are simply using
   'the DLookUp Function to acquire the Path and Image
   'file name from Table. You can fill this variable
   'any way you like.[/COLOR]
   MyPicFile = Nz(DLookup("[PicPath]", "[MyTable]", "[PicID]=" & Whatever), "")
   
   [COLOR="DarkGreen"]'Exit if it's found that the MyPicFile variable
   'contains nothing (empty string).[/COLOR]
   If MyPicFile = "" Then Exit Sub
   
   [COLOR="DarkGreen"]'Trap Errors...[/COLOR]
   On Error GoTo Error_PrintPic

  [COLOR="DarkGreen"] 'Create the Temporary Report[/COLOR]
   Set rpt = CreateReport()
   
  [COLOR="DarkGreen"] 'Set Report Properties...[/COLOR]
   With rpt
      '.Visible = False
      .PictureType = 1      [COLOR="DarkGreen"]'Linked[/COLOR]
      .PictureSizeMode = 3  [COLOR="DarkGreen"]'Zoom[/COLOR]
      .Picture = MyPicFile  [COLOR="DarkGreen"]'The Picture (image) to Print[/COLOR]
      RptName = .Name
   End With
   
   [COLOR="DarkGreen"]'Print the report...[/COLOR]
   DoCmd.OpenReport RptName, , , , acHidden

Exit_PrintPic:
   On Error Resume Next
  [COLOR="DarkGreen"] 'Close the report[/COLOR]
   DoCmd.Close acReport, RptName, acSaveNo
  [COLOR="DarkGreen"] 'Delete the report[/COLOR]
   DoCmd.DeleteObject acReport, RptName
   
   Set rpt = Nothing
   If Err <> 0 Then Err.Clear
   Exit Sub
   
Error_PrintPic:
   Dim ErrMsg As String
   [COLOR="DarkGreen"]'Message selectable Errors...[/COLOR]
   Select Case Err.Number
     Case 2202
        ErrMsg = "There is no Printer attached to system." & vbCr & _
                 "Please attach Printer and try again."
     Case Else
        ErrMsg = Err.Number & " -- " & Err.Description
   End Select
   MsgBox ErrMsg, vbExclamation, "Picture Print Error"
   Resume Exit_PrintPic

.
 
Wow thanks , is working and thank you for the in depth explanation.

I wonder if this would work with pdf files as well , although that is not a 'graphic' format.
 
On slight problem is i want to use the standard windows printer which i have set earlier in the code.

At the beginning of this procedure i change the default printer to a pdf printer.
With the exexution of your code it is reset to the original printer.

Can that be changed ??

This is the code i use to change the printer :

Dim dr As aht_tagDeviceRec
Dim drexisting As aht_tagDeviceRec
Call ahtGetDefaultPrinter(drexisting)
dr.drDeviceName = "pdfFactory Pro"
Printer.TopMargin = 5
Call ahtSetDefaultPrinter(dr)
 
Last edited:
The mechanism I provided will not print PDF files. There are however several API's available that can do the job (google it).

If your printer is changing then you will need to set the Default printer to whatever printer you want. Here is one way:

Place the following code into a Database Code Module (not a Form Module):

Code:
Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const WM_WININICHANGE As Long = &H1A

Private Declare Function SendNotifyMessage Lib "user32" _
                         Alias "SendNotifyMessageA" _
                         (ByVal hwnd As Long, _
                         ByVal msg As Long, _
                         ByVal wParam As Long, _
                         lParam As Any) As Long
   
Private Declare Function SetDefaultPrinter Lib "winspool.drv" _
                         Alias "SetDefaultPrinterA" _
                         (ByVal pszPrinter As String) As Long

Public Sub SetPrinter(ByVal PrinterName As String)
   [COLOR="DarkGreen"]'Set the default printer to the Printer Name
   'supplied in the PrinterName argument[/COLOR]
   SetDefaultPrinter PrinterName
   
   [COLOR="DarkGreen"]'Broadcast the change to Windows.[/COLOR]
    Call SendNotifyMessage(HWND_BROADCAST, WM_WININICHANGE, _
                           0, ByVal "windows")
End Sub

Then modify the previous code to:

Code:
[COLOR="DarkGreen"]...................
...................
...................
   'Set the Default Printer to the PDF Printer.[/COLOR]
   Call SetPrinter("pdfFactory Pro")
    
   [COLOR="DarkGreen"]'Print the report...[/COLOR]
   DoCmd.OpenReport RptName, , , , acHidden

Exit_PrintPic:
   On Error Resume Next
  [COLOR="DarkGreen"] 'Close the report[/COLOR]
   DoCmd.Close acReport, RptName, acSaveNo
   [COLOR="DarkGreen"]'Delete the report[/COLOR]
   DoCmd.DeleteObject acReport, RptName
   
   Set rpt = Nothing
   If Err <> 0 Then Err.Clear
   
   [COLOR="DarkGreen"]'Set the Default Printer back to the Standard Printer.[/COLOR]
   Call SetPrinter("Whatever The Printer Name Is")
   Exit Sub

[COLOR="DarkGreen"]...................
...................
...................[/COLOR]

.
 
hi, i'm getting a lifftle frustrated with this , it is not working.
Of course not your fault.
I think i need to change the standard windows printer ....
 
Hey CyberLinx in your above responce you referenecd printing multiple images. Can you give me an quick example of what that code might look like?

Thanks

Mstemp
 
Cyberlynx:

Will you VB code work for access 2003 which I believe does not have the picture primitive built in?
 

Users who are viewing this thread

Back
Top Bottom