Solved

How can I replace pages 2-4 in all Word documents in a folder with the pages in the current open file using a macro?

Posted on 2014-07-22
18
208 Views
Last Modified: 2014-08-08
I have around 100+ .doc and .docx files in a folder that contain the company terms & conditions on pages 2-4.
Very occasionally these terms change.
The pages contain text boxes and images.

If I create a document with new terms & conditions, can I replace all pages 2-4 in a designated folder with pages 2-4 in the open document?

I would like to achieve this with a macro which will facilitate the easy changing of files, en masse, in the future.

Thanks for your help
0
Comment
Question by:spar-kle
  • 9
  • 9
18 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40211581
Are the pages separated by Page breaks or Next Page Section Breaks?
0
 

Author Comment

by:spar-kle
ID: 40211629
Print layout it shows multiple pages, but outline and draft view only shows one page break
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40211677
Any chance of seeing a sample?
0
 

Author Comment

by:spar-kle
ID: 40212040
Here's a sample document with all actual content removed.
Pages 2-4 may be updated
Data-Sheet-T-C.docx
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40212667
I had hoped that the pages would be positively delimited, This code looks checks the page number of the paragraphs. This should work unless a page overflows in the middle of a paragraph.
Sub UpdateDocs()
    Dim strMasterDoc As String
    Dim strUpdateDoc As String
    Dim MasterDoc As Document
    Dim strFolder1 As String
    Dim strFolder2 As String
    Dim rngMaster As Range
    Dim rngUpdate As Range
    Dim UpdateDoc As Document
    
    strMasterDoc = "I:\Allwork\ee\28481125\Data-Sheet-T-C.docx"
    strFolder1 = "I:\Allwork\ee\28481125\Input\"
    strFolder2 = "I:\Allwork\ee\28481125\Output\"
    
    Set MasterDoc = Documents.Open(strMasterDoc)
    Set rngMaster = GetPages(MasterDoc, 2, 4)
    rngMaster.Copy
    strUpdateDoc = Dir$(strFolder1 & "*.docx")
    Do Until Len(strUpdateDoc) = 0
        Set UpdateDoc = Documents.Open(strFolder1 & strUpdateDoc)
        Set rngUpdate = GetPages(UpdateDoc, 2, 4)
        rngUpdate.Paste
        UpdateDoc.SaveAs strFolder2 & strUpdateDoc
        UpdateDoc.Close wdDoNotSaveChanges
        strUpdateDoc = Dir$()
    Loop
End Sub

Function GetPages(Doc As Document, iStart As Integer, iEnd As Integer) As Range
Dim para As Paragraph
Dim rng As Range

For Each para In Doc.Paragraphs
    Select Case para.Range.Information(wdActiveEndPageNumber)
        Case iStart
            Set GetPages = para.Range.Duplicate
        Case iEnd + 1
            GetPages.End = para.Range.Start
            Exit Function
    End Select
Next para
End Function

Open in new window

The file name for the master document and for the ipnut and output folders are all hard coded.
0
 

Author Comment

by:spar-kle
ID: 40213683
Thanks Graham
That works perfectly for amending page 2 only.

Pages 3 and 4 are not amended unfortunately.

As an alternative method, would it be possible to delete all pages on input file, except page 1, then insert page breaks and copy the three new pages?
0
 

Author Comment

by:spar-kle
ID: 40214367
...or alternatively, how about if the first page of each input file is copied to the master file and then saved in the output file with the original name?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40215202
I've been away today, but hope to look at this again tomorrow (in 12+ hours time)
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40216388
I have modified your sample document by adding another page, so that there now five pages. I have added text at the start of each page with the Page number, and then created two versions, one with the word 'Old' after the page number and the other with 'New'. After running the macro code, the word 'New' can be seen on pages 2 to 4 only of the old version.
Hopefully this will work for you. If it does, but it still doesn't work for you with your own files, then it might be to do with what is on the following page (5) , or how page 4 is ended. If that is the case, can you past a further sample with another page, please?
Data-Sheet-T-C-Old.docx
Data-Sheet-T-C-New.docx
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:spar-kle
ID: 40219861
Thanks Graham
Definitely getting there.

When I use the Data-Sheet-T-C-New.docx as my template it was adding a blank page after the first, so I removed the page break on page 1 which resolved this issue.

However, although the three new pages are being added, the original pages (2-4) are still in the output file, though they are displaced to pages 5-6
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40223617
I notice that the pages are separated with manual page breaks, so in this version, we use the Find object to look for these page breaks.
Sub UpdateDocs()
    Dim strMasterDoc As String
    Dim strUpdateDoc As String
    Dim MasterDoc As Document
    Dim strFolder1 As String
    Dim strFolder2 As String
    Dim rngMaster As Range
    Dim rngUpdate As Range
    Dim UpdateDoc As Document
    strMasterDoc = "I:\Allwork\ee\28481125\Data-Sheet-T-C_New.docx"
    strFolder1 = "I:\Allwork\ee\28481125\Input\"
    strFolder2 = "I:\Allwork\ee\28481125\Output\"
    
    Set MasterDoc = Documents.Open(strMasterDoc)
    Set rngMaster = GetPages(MasterDoc, 2, 4)
    rngMaster.Copy
    strUpdateDoc = Dir$(strFolder1 & "*.docx")
    Do Until Len(strUpdateDoc) = 0
        Set UpdateDoc = Documents.Open(strFolder1 & strUpdateDoc)
        Set rngUpdate = GetPages(UpdateDoc, 2, 4)
        rngUpdate.Paste
        UpdateDoc.SaveAs strFolder2 & strUpdateDoc
        UpdateDoc.Close wdDoNotSaveChanges
        strUpdateDoc = Dir$()
    Loop
End Sub

Function GetPages(Doc As Document, iStart As Integer, iEnd As Integer) As Range
Dim iPage As Integer
Dim rng1 As Range
Dim rng2 As Range
Dim rng As Range
    Debug.Print Doc.Name

    Set rng1 = Doc.Range.Duplicate
    iPage = 1
    With rng1.Find
        .Text = "^m"
        Do While .Execute
            Set rng2 = rng1.Duplicate
            iPage = iPage + 1
            Select Case iPage
                Case iStart
                    Set rng = rng2
                Case Is >= iEnd + 1
                    rng.End = rng2.End
                    Set GetPages = rng.Duplicate
                    Exit Function
            End Select
        Loop
    End With
End Function

Open in new window

0
 

Author Comment

by:spar-kle
ID: 40223648
Thanks Graham

This code gives me a Run-time error 91: object variable or With block not set
with reference to  rngMaster.Copy

So I presume the  GetPages Function is unable to locate pages.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40223888
Not enough, anyway.
This version issues a diagnostic message if that is the case.
Function GetPages(Doc As Document, iStart As Integer, iEnd As Integer) As Range
    Dim iPage As Integer
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng As Range
 
    Set rng1 = Doc.Range.Duplicate
    iPage = 1
    With rng1.Find
        .Text = "^m"
        Do While .Execute
            Set rng2 = rng1.Duplicate
            iPage = iPage + 1
            Select Case iPage
                Case iStart
                    Set rng = rng2
                Case Is >= iEnd + 1
                    rng.End = rng2.End
                    Set GetPages = rng.Duplicate
                    Exit Function
            End Select
        Loop
    End With
    MsgBox "Only " & iPage - 1 & " page breaks found"
    End
End Function

Open in new window

0
 

Author Comment

by:spar-kle
ID: 40224092
When the first Input file is opened, the message comes up "only 3 page breaks found"
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40224340
Yes. There were only three the document that you sent. There were only four pages, so I guessed that in removing page 5 onwards, you had also removed a page break. In my version of the test document I had inserted a page break to create a fifth page. Without it, extra text lies under the textboxes on page 4.

I still don't know what causes  the overflow to page 5 in your non-redacted documents. However, this macro version tries to find  four page breaks, but if it fails, it uses the techniques from the first version. That is to step through the paragraphs until it finds page 5.
Function GetPages(Doc As Document, iStart As Integer, iEnd As Integer) As Range
    Dim iPage As Integer
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng As Range
 
    Set rng1 = Doc.Range.Duplicate
    iPage = 1
    With rng1.Find
        .Text = "^m"
        Do While .Execute
            Set rng2 = rng1.Duplicate
            iPage = iPage + 1
            Select Case iPage
                Case iStart
                    Set rng = rng2
            
                Case Is > iEnd + 1
                    'rng.End = rng2.End
                    'Set GetPages = rng.Duplicate
                    Exit Function
                Case Else
                    rng.End = rng2.End
                    Set GetPages = rng.Duplicate
                    
            End Select
        Loop
    End With
    Dim para As Paragraph
    Set rng2 = rng.Duplicate
    rng2.Collapse wdCollapseEnd
    rng2.End = Doc.Range.End
    For Each para In rng2.Paragraphs
        If para.Range.Information(wdActiveEndPageNumber) = iEnd + 1 Then
                GetPages.End = para.Range.Start
                Exit For
        End If
    Next para

End Function

Open in new window

0
 

Author Comment

by:spar-kle
ID: 40224454
Thanks for your continued support Graham, really appreciated
I'm using your file "Data-Sheet-T-C-New.docx" with 5 pages.
It is now so very close to what is required.

All of the new pages are inserted correctly into the input files, and pages 1-4 are exactly correct.
However, page 3 of each of the input files is displaced to page 5
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40225104
I'm sorry, but I don't have any more guesses.

If you are  worried about the open view of the documents the you could consider posting to me personally. My email address is obfuscated  (to avoid email harvesters)  in my profile.
0
 

Author Closing Comment

by:spar-kle
ID: 40248402
Thanks for your help, excellent.
really appreciated.
Des
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now