0

I have a code for copying userform and pasting it to Users desktop as a pdf file. Pdf file is OK, however quality of image inside it is quite bad. Is there any way to increase the quality of image inside pdf file?

Private Sub btnPrintPDF_Click()
'change to your button name
    Dim pdfName As String
    Dim newWS As Worksheet

    Application.DisplayAlerts = False

    keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY, 0
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0
    keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0

    DoEvents 'Otherwise, all of screen would be pasted as if PrtScn rather than Alt+PrtScn was used for the copy.

    Set newWS = ThisWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
    Application.PrintCommunication = False
With newWS.PageSetup
    .Orientation = xlPortrait
    .Zoom = False
 .FitToPagesTall = 1
 .FitToPagesWide = 1
End With
Application.PrintCommunication = True
    newWS.PasteSpecial Format:="Bitmap", Link:=False, DisplayAsIcon:=False
    pdfName = Environ$("USERPROFILE") & "\Desktop\" & ThisWorkbook.Sheets("Other Data").Range("P14").Value & "," & " " & "Summary" & "_" & Format(Now, "dd.mm.yyyy") & ".pdf"
newWS.ExportAsFixedFormat Type:=xlTypePDF, _
    FileName:=pdfName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=False, IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
    newWS.Delete
    Unload Me

    Application.DisplayAlerts = True
    ThisWorkbook.Sheets("MAIN").Activate

End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
10101
  • 2,232
  • 3
  • 26
  • 66
  • 1
    have you tried using different file types, or image types? bitmap versus jpeg versus png, for example, each are important for different types of images. [Here's a link that might help with saving as a jpeg](https://stackoverflow.com/questions/16143877/using-vba-code-how-to-export-excel-worksheets-as-image-in-excel-2003). – Cyril Apr 01 '19 at 17:25
  • I would like to try, but I don't get it how to change it to jpeg or any other. I don't have any extension in the code (jpeg, png). It just `Format:="Bitmap"` - How to change that to jpeg? – 10101 Apr 01 '19 at 17:32
  • 1
    [Worksheet.PasteSpecial method (Excel)](https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.pastespecial). You should be able to call the format by number, such that format:=4 is for bitmap, or format:=0 is png, etc. – Cyril Apr 01 '19 at 17:37
  • Thank you very much! For some reason only `format:=0` works. It is much better, but would be nice to try jpeg `format:=1` as well. – 10101 Apr 01 '19 at 17:44
  • 1
    Why? JPEG will always result in a lower quality image because it uses a lossy compression scheme. It certainly won't result in a better quality image – cybernetic.nomad Apr 01 '19 at 18:16
  • @cybernetic.nomad png seems to be excessive for an image of a userform, but should have the most definition. I understand that JPEG uses irreversible/lossy compression, and my immediate comment was thinking it was compressed bitmap (i forgot it could also be uncompressed), which is also irreversible (similar, but different... might have made a difference). Came back with a better, more appropriate option, so that should circumvent any issue with irreversible compression. – Cyril Apr 02 '19 at 15:17

1 Answers1

2

Answer from comments section:

Worksheet.PasteSpecial method (Excel). You should be able to call the format by number, such that format:=4 is for bitmap, or format:=0 is png, etc

Corrected line appears:

newWS.PasteSpecial Format:=0, Link:=False, DisplayAsIcon:=False
Cyril
  • 6,448
  • 1
  • 18
  • 31