Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-12-2021, 11:06 AM
Venteux Venteux is offline VBA/Macro for page numbering across multiple documents Windows 10 VBA/Macro for page numbering across multiple documents Office 2019
Novice
VBA/Macro for page numbering across multiple documents
 
Join Date: May 2021
Posts: 22
Venteux is on a distinguished road
Default VBA/Macro for page numbering across multiple documents

Hi all, I need to apply continuous page numbering across multiple documents. I have over 200 documents (chapters) with different authors that work on individual chapters. I can merge the documents at the end and have continuous page numbering but when I separate the documents again, the continuous page numbering is lost.

The {INCLUDETEXT} field isn't practical for me for several reasons: 1) if one or several of the chapters change, then I have to go back and manually update them again, 2) I do this task every year, so the title of the documents will change every year.



I found a macro (Automatic Page Numbers across Multiple Documents (Microsoft Word)) that seems like it would work, but it gives me an error and when I debug, this is the line that is problematic: "Application.Documents(thisFile).Close Savechanges:=wdSaveChanges"

Does anyone know what would cause this issue? I'm a VBA novice, so this is beyond me. I've specified my own path name and the file names for the array.

This is the whole code:

Code:
Sub PageNumberReset()
    Dim pgNo As Long
    Dim n As Long
    Dim pathName As String
    Dim fileNames
    Dim thisFile As String
    Dim aRange As Range

    ' Specify the path to the document files
    pathName = "C:\MyDocs\Example"
    ' Create an array holding the document file names, in sequence
    fileNames = Array("Chap1.docx", "Chap2.docx", "Chap3.docx")

    pgNo = 0
    For n = 0 To UBound(fileNames)
        thisFile = pathName & fileNames(n)
        Application.Documents.Open (thisFile)
        ActiveDocument.Sections(1).Headers(1).PageNumbers.StartingNumber = pgNo + 1
        Set aRange = ActiveDocument.Range
        aRange.Collapse Direction:=wdCollapseEnd
        aRange.Select
        pgNo = Selection.Information(wdActiveEndAdjustedPageNumber)
        Application.Documents(thisFile).Close Savechanges:=wdSaveChanges
    Next n
End Sub

Can anyone help with this?

Last edited by macropod; 05-12-2021 at 02:52 PM. Reason: Added code tags
Reply With Quote
  #2  
Old 05-12-2021, 03:05 PM
macropod's Avatar
macropod macropod is offline VBA/Macro for page numbering across multiple documents Windows 10 VBA/Macro for page numbering across multiple documents 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

I seems to me that maintaining a 'main' document with INCLUDETEXT fields pointing to each of the 'secondary' documents would be no more onerous than using a macro employing an array of document names that is just as susceptible as INCLUDETEXT fields to your chapters being renamed.

As for your macro, the path is missing the final separator. The code could also be improved in other ways:
Code:
Sub PageNumberReset()
Application.ScreenUpdating = False
Dim pgNo As Long, n As Long
Dim StrPath As String
Dim ArrNames, wdDoc As Document

' Specify the path to the document files
StrPath = "C:\MyDocs\Example\"
' Create an array holding the document file names, in sequence
ArrNames = Array("Chap1.docx", "Chap2.docx", "Chap3.docx")

For n = 0 To UBound(ArrNames)
  Set wdDoc = Documents.Open(FileName:=StrPath & ArrNames(n), AddToRecentFiles:=False)
  With wdDoc
    With .Sections.First
      For Each HdFt In .Headers
        With HdFt
          If .Exists = True Then
            .PageNumbers.RestartNumberingAtSection = True
            .PageNumbers.StartingNumber = pgNo + 1
          End If
        End With
      Next
      For Each HdFt In .Footers
        With HdFt
          If .Exists = True Then
            .PageNumbers.RestartNumberingAtSection = True
            .PageNumbers.StartingNumber = pgNo + 1
          End If
        End With
      Next
    End With
    pgNo = pgNo + .ComputeStatistics(wdStatisticPages)
    .Close Savechanges:=wdSaveChanges
  End With
Next n

Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by Charles Kenyon; 05-13-2021 at 08:53 AM.
Reply With Quote
  #3  
Old 05-12-2021, 04:30 PM
Venteux Venteux is offline VBA/Macro for page numbering across multiple documents Windows 10 VBA/Macro for page numbering across multiple documents Office 2019
Novice
VBA/Macro for page numbering across multiple documents
 
Join Date: May 2021
Posts: 22
Venteux is on a distinguished road
Default

I completely agree that entering each file name into the array is just as onerous. I was hoping to try the macro first to see if it works before trying to edit with some sort of file dialog instead of an array.

Thanks very much for the code! I tried it, and it seemed to have done something to the files because the "date modified" has updated. However, when I check the page numbers of the files, the first page of each document is still 1. I'm just using a { PAGE } code in the footers. Am I doing something wrong?
Reply With Quote
  #4  
Old 05-12-2021, 04:35 PM
macropod's Avatar
macropod macropod is offline VBA/Macro for page numbering across multiple documents Windows 10 VBA/Macro for page numbering across multiple documents 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

The code you posted - and which I adapted - refers to page numbering in headers, not footers.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-12-2021, 04:41 PM
Venteux Venteux is offline VBA/Macro for page numbering across multiple documents Windows 10 VBA/Macro for page numbering across multiple documents Office 2019
Novice
VBA/Macro for page numbering across multiple documents
 
Join Date: May 2021
Posts: 22
Venteux is on a distinguished road
Default

Ugh, how did I miss that?

I will try again. Thank you!!
Reply With Quote
  #6  
Old 05-12-2021, 04:56 PM
Venteux Venteux is offline VBA/Macro for page numbering across multiple documents Windows 10 VBA/Macro for page numbering across multiple documents Office 2019
Novice
VBA/Macro for page numbering across multiple documents
 
Join Date: May 2021
Posts: 22
Venteux is on a distinguished road
Default

I still can't get it to work. I created test documents with page numbers in the headers, and as before, the macro runs, and something happens to the files, but when I open the files, they all still start at 1.
Reply With Quote
  #7  
Old 05-13-2021, 07:23 PM
macropod's Avatar
macropod macropod is offline VBA/Macro for page numbering across multiple documents Windows 10 VBA/Macro for page numbering across multiple documents 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

Depending on your documents' page layouts etc., it's possible the right headers/footers aren't being reset. I've extended the code to process all headers & footers in the target documents.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 05-14-2021, 07:31 AM
Venteux Venteux is offline VBA/Macro for page numbering across multiple documents Windows 10 VBA/Macro for page numbering across multiple documents Office 2019
Novice
VBA/Macro for page numbering across multiple documents
 
Join Date: May 2021
Posts: 22
Venteux is on a distinguished road
Default

AMAZING!! Thank you so much! It works beautifully. I truly appreciate it!
Reply With Quote
  #9  
Old 05-14-2021, 10:27 AM
Venteux Venteux is offline VBA/Macro for page numbering across multiple documents Windows 10 VBA/Macro for page numbering across multiple documents Office 2019
Novice
VBA/Macro for page numbering across multiple documents
 
Join Date: May 2021
Posts: 22
Venteux is on a distinguished road
Default

For anyone else viewing this post, I ended up combining macropod's other macro from the post at https://www.msofficeforums.com/word-...ocx-files.html with this macro so that I don't have to manually type in the path and file names. It works like a charm.

Thanks again!!
Reply With Quote
Reply

Tags
macro, pagenumber, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page Numbering Macro mbr50 Word VBA 4 01-20-2020 04:47 PM
VBA/Macro for page numbering across multiple documents Multiple page numbering Spyke44 Word 4 10-18-2015 08:18 AM
VBA/Macro for page numbering across multiple documents Run a macro on multiple documents prakhil Word VBA 1 06-27-2014 06:20 AM
Multiple page document to individual multiple page documents Legger Mail Merge 3 06-15-2014 06:36 AM
VBA/Macro for page numbering across multiple documents page numbering across multiple documents reitdesign Word 3 12-12-2008 11:55 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:07 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