Excel VBA UserForm打印屏幕另存为pdf

时间:2018-11-18 19:53:50

标签: excel vba excel-vba printing userform

我有一个用户窗体,其中有个按钮可以获取用户窗体的打印屏幕,粘贴到新创建的工作表中并将其导出为pdf。我目前使用的代码存在以下问题:

1)我想将打印屏幕调整为一页,现在需要打印屏幕并分成两页。

2)更高分辨率的打印屏幕。是否可以提高打印屏幕的分辨率以获得更高的质量?

我用于UserForm按钮的代码:

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))
    newWS.PasteSpecial Format:="Bitmap", Link:=False, DisplayAsIcon:=False
    pdfName = ThisWorkbook.Path & "\" & Sheets("MAIN").Range("D14").Value & "Project_Summary" & "_" & " " & Format(Now, "yyyy-mmm-dd") & ".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

1 个答案:

答案 0 :(得分:0)

Application.PrintCommunication = False
With newWS.PageSetup
    .Orientation = xlLandscape
    .Zoom = False
    .FitToPagesWide = 1
End With
Application.PrintCommunication = True
ewWS.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=pdfName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=False, IgnorePrintAreas:=False, _
    OpenAfterPublish:=False