-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.txt
57 lines (47 loc) · 1.94 KB
/
macro.txt
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
Sub ExtractPagesToPDF()
'Declare variables
Dim startPage As Integer
Dim endPage As Integer
Dim numPages As Integer
Dim selectedDoc As Document
Dim fileName As String
Dim savePath As String
'Prompt user to select Word document
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select the Word document to extract pages from"
.Filters.Clear
.Filters.Add "Word Documents", "*.docx;*.docm;*.doc;*.dotx;*.dotm;*.dot"
.FilterIndex = 1
If .Show = -1 Then 'if user selects a file
fileName = .SelectedItems(1)
'Get the path of the selected file
savePath = Left(fileName, InStrRev(fileName, "\"))
Else 'if user cancels
MsgBox "No document selected.", vbCritical
Exit Sub
End If
End With
'Open the selected document
Set selectedDoc = Documents.Open(fileName)
'Start loop
Do
'Ask user to input number of pages to extract
numPages = InputBox("Enter the number of pages to extract to PDF (or enter 0 to stop):")
'Check if user wants to stop loop
If numPages = 0 Then
MsgBox "All is ok", vbInformation
Exit Do
End If
'Ask user to input start page number
startPage = InputBox("Enter the starting page number:")
'Calculate end page number
endPage = startPage + numPages - 1
'Extract pages to PDF
With selectedDoc
.ExportAsFixedFormat OutputFileName:=savePath & "ExtractedPages_" & startPage & "-" & endPage & ".pdf", ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=startPage, To:=endPage, OpenAfterExport:=False
End With
Loop
'Close the document
selectedDoc.Close savechanges:=False
End Sub