sigil
sigil

Reputation: 9556

print JPEG to PDF, set paper size

Using VBA, for a given JPEG (or GIF or BMP), I want to print it to a PDF, and set the page size before printing. I've looked at a couple of different ways to print, but no one of them seems to be able to do what I want:

Page size can be set using the methods associated with an Office document object, e.g. Worksheet.PageSetup.PaperSize = xlPaper11x17, but this only sets page size for that object, not for the JPEG that I want to print.

Upvotes: 1

Views: 5694

Answers (1)

You could just import it into your Excel workbook and print it from there. Here's a rudimentary example:

Dim ws As Worksheet
Dim pic As Picture

Set ws = ActiveSheet
ws.PageSetup.PaperSize = xlPaperA4
'Can also specify margins, etc.

ws.Range("A1").Activate
Set pic = ws.Pictures.Insert("C:\mypic.jpg")   
'Set picture size.
With pic.ShapeRange
    .LockAspectRatio = msoFalse
    .Height = Application.CentimetersToPoints(20)
    .Width = Application.CentimetersToPoints(15)
    'Or you could match the size to the paper margins from above.
End With

ws.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="C:\mypic.pdf", OpenAfterPublish:=True

Tested in Excel 2010.

Upvotes: 1

Related Questions