Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-19-2022, 07:21 AM
Bunndahabhain Bunndahabhain is offline Splitting a word document using VBA Windows 10 Splitting a word document using VBA Office 2019
Novice
Splitting a word document using VBA
 
Join Date: Jul 2022
Posts: 1
Bunndahabhain is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 07-19-2022, 07:41 AM
macropod's Avatar
macropod macropod is offline Splitting a word document using VBA Windows 10 Splitting a word document using VBA Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 07-19-2022, 08:19 AM
macropod's Avatar
macropod macropod is offline Splitting a word document using VBA Windows 10 Splitting a word document using VBA Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #4  
Old 07-22-2022, 10:35 AM
VBAadvocate VBAadvocate is offline Splitting a word document using VBA Windows 10 Splitting a word document using VBA Office 2019
Novice
 
Join Date: Feb 2022
Posts: 12
VBAadvocate is on a distinguished road
Default 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
I added a little check to make sure we have an even number of pages and I + 1 in the pdf export command.

A few more user input checks might be necessary.
Reply With Quote
  #5  
Old 07-22-2022, 11:20 AM
Charles Kenyon Charles Kenyon is online now Splitting a word document using VBA Windows 11 Splitting a word document using VBA Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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.
Reply With Quote
Reply

Tags
splitting

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting a word document using VBA 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
Splitting a word document using VBA 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
Splitting a word document using VBA WORD 2003 Need help splitting a HUGE Document dlawson Word 4 04-14-2009 12:22 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:02 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft