-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBA_PDF Printer
86 lines (80 loc) · 2.94 KB
/
VBA_PDF Printer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Sub PDF()
'
' PDF Macro
' This Macro enables you to create PDFs from your worksheets.
' Simply select the range you want to exract as PDF.
'
Dim SelectedRange As Range
Dim Answer As VbMsgBoxResult
Dim orientation
On Error GoTo Handle
Set SelectedRange = Application.InputBox("Please select the Range you want to print!", "Print Selection", , , , , , 8)
Do While WorksheetFunction.And(SelectedRange.Rows.Count = 1, SelectedRange.Columns.Count = 1)
MsgBox "Please select a range" & vbNewLine & "Selection of one cell has been detected!", vbCritical, "Error"
Set SelectedRange = Application.InputBox("Please select the Range you want to print!", "Print Selection", , , , , , 8)
Loop
' PDF orientation must be selected (Portrait or Landascape)
Answer = MsgBox("Should it be Portrait?", vbYesNo, "A4 Orientation")
If Answer = vbYes Then
orientation = xlPortrait
ElseIf Answer = vbNo Then
orientation = xlLandscape
End If
ActiveSheet.PageSetup.PrintArea = SelectedRange.Address
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = SelectedRange.Address
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.CenterHorizontally = False
.CenterVertically = False
.orientation = orientation
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
Selection.PrintOut Copies:=1, Collate:=True
Handle:
End Sub