-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDFandSave-Excel.bas
83 lines (54 loc) · 1.99 KB
/
PDFandSave-Excel.bas
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
Attribute VB_Name = "PDFandSaveExcel"
' Creates New Functions PDFandSaveExcelSheet(active sheet) and PDFandSaveExcelWB(all sheets in workbook)
Public Sub PDFandSaveExcelSheet()
ActiveWorkbook.Save
SaveActiveDocumentAsPdfExcelSheet
End Sub
Public Sub PDFandSaveExcelWB()
ActiveWorkbook.Save
SaveActiveDocumentAsPdfExcelWB
End Sub
Sub SaveActiveDocumentAsPdfExcelSheet()
Dim strPath As String
On Error GoTo Errhandler
If InStrRev(ActiveWorkbook.FullName, ".") <> 0 Then
strPath = Left(ActiveWorkbook.FullName, InStr(ActiveWorkbook.FullName, ".") - 1) & ".pdf"
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End If
On Error GoTo 0
Exit Sub
Errhandler:
MsgBox "There was an error saving a copy of this document as PDF. " & _
"Ensure that the PDF is not open for viewing and that the destination path is writable. Error code: " & Err
End Sub
Sub SaveActiveDocumentAsPdfExcelWB()
Dim strPath As String
Dim sheetName As String
Dim workSheet As workSheet
On Error GoTo Errhandler
If InStrRev(ActiveWorkbook.FullName, ".") <> 0 Then
strPath = Left(ActiveWorkbook.FullName, InStr(ActiveWorkbook.FullName, ".") - 1)
For Each workSheet In Worksheets
workSheet.Select
sheetName = workSheet.Name
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPath & " - " & sheetName & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
Next workSheet
End If
On Error GoTo 0
Exit Sub
Errhandler:
MsgBox "There was an error saving a copy of this document as PDF. " & _
"Ensure that the PDF is not open for viewing and that the destination path is writable. Error code: " & Err
End Sub