#1
|
|||
|
|||
Splitting a word document using VBA
I've found this code online that allows me to split each page into PDF and save it. However, I need to change it so that it splits every 2 pages. So each PDF document will have 2 pages. So the first document will have pages 1 and 2. The second document will have 3 and 4, and so on.
Here's the code I have so far. Code:
Sub SaveAsSeparatePDFs() 'UpdatebyExtendoffice20181120 Dim I As Long Dim xDlg As FileDialog Dim xFolder As Variant Dim xStart, xEnd As Integer On Error GoTo lbl Set xDlg = Application.FileDialog(msoFileDialogFolderPicker) If xDlg.Show <> -1 Then Exit Sub xFolder = xDlg.SelectedItems(1) xStart = CInt(InputBox("Start Page", "KuTools for Word")) xEnd = CInt(InputBox("End Page:", "KuTools for Word")) If xStart <= xEnd Then For I = xStart To xEnd ActiveDocument.ExportAsFixedFormat OutputFileName:= _ xFolder & "\Page_" & I & ".pdf", ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _ wdExportFromTo, From:=I, To:=I, Item:=wdExportDocumentContent, _ IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _ wdExportCreateHeadingBookmarks, DocStructureTags:=True, _ BitmapMissingFonts:=False, UseISO19005_1:=False Next End If Exit Sub lbl: MsgBox "Enter right page number", vbInformation, "KuTools for Word" End Sub Many thanks for all your help in advance <3 Last edited by macropod; 07-19-2022 at 07:38 AM. Reason: Added code tags |
#2
|
||||
|
||||
If you're trying to split mailmerge output, see the Split Merged Output to Separate Documents topic in the Mailmerge Tips & Tricks 'Sticky' thread at the top of the mailmerge forum (https://www.msofficeforums.com/mail-...ps-tricks.html) or, better still, the Send Mailmerge Output to Individual Files topic in the same thread.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
Cross-posted at: Redirecting
For cross-posting etiquette, please read: Excelguru Help Site - A message to forum cross posters
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Just add Step 2 and some minor changes
I tried the following and it worked for me. Cool routine, by the way.
Code:
Sub ExportPDF() 'UpdatebyExtendoffice20181120 Dim I As Long Dim xDlg As FileDialog Dim xFolder As Variant Dim xStart As Integer, xEnd As Integer ActiveDocument.ComputeStatistics (wdStatisticPages) 'MsgBox "Page count = " & ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) On Error GoTo lbl Set xDlg = Application.FileDialog(msoFileDialogFolderPicker) If xDlg.Show <> -1 Then Exit Sub xFolder = xDlg.SelectedItems(1) xStart = CInt(InputBox("Start Page", "PDF Export")) xEnd = CInt(InputBox("End Page:", "PDF Export")) If ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) < xEnd Then MsgBox "End page is more than total pages." Exit Sub End If If xStart <= xEnd Then If Int((xEnd + 1 - xStart) / 2) <> (xEnd + 1 - xStart) / 2 Then MsgBox "Start and end pages does not include an even number of pages." Exit Sub End If For I = xStart To xEnd Step 2 ActiveDocument.ExportAsFixedFormat OutputFileName:= _ xFolder & "\Page_" & I & "_and_" & I + 1 & ".pdf", ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _ wdExportFromTo, From:=I, To:=I + 1, Item:=wdExportDocumentContent, _ IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _ wdExportCreateHeadingBookmarks, DocStructureTags:=True, _ BitmapMissingFonts:=False, UseISO19005_1:=False Next End If Exit Sub lbl: MsgBox "Enter right page number", vbInformation, "PDF Export" End Sub A few more user input checks might be necessary. |
#5
|
|||
|
|||
There is also an answer from Hans Vogelar in your other post. Have you followed the directions given by the linked page macropod posted. If not, please do.
|
Tags |
splitting |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Splitting merged Word document into individual .pdf documents (naming from a field in the file) | ScarlettNZ | Mail Merge | 1 | 05-30-2019 12:53 AM |
Filename after splitting document | Julian | Word VBA | 5 | 12-08-2015 04:51 AM |
Splitting Word Document based on line content | TwiceOver | Word VBA | 23 | 10-12-2015 02:01 PM |
Document splitting | MsLavigne | Word | 2 | 05-09-2012 05:52 AM |
WORD 2003 Need help splitting a HUGE Document | dlawson | Word | 4 | 04-14-2009 12:22 PM |