Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-17-2022, 04:40 AM
leeqiang leeqiang is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2019
Advanced Beginner
How to traverse folders and subfolders to merge a word document into one document?
 
Join Date: Aug 2020
Posts: 53
leeqiang is on a distinguished road
Default How to traverse folders and subfolders to merge a word document into one document?

How to traverse folders and subfolders to merge a word document into one document?


The folders and subfolders are word documents, and the contents of all word documents are merged into one word document.
That is, the original content in each word is integrated into a word document in order
Reply With Quote
  #2  
Old 11-17-2022, 02:21 PM
gmaxey gmaxey is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub MergeDocs()

Dim rng As Range

Dim MainDoc As Document

Dim strFile As String, strFolder As String

Dim Count As Long

    With Application.FileDialog(msoFileDialogFolderPicker)

        .Title = "Pick folder"

        .AllowMultiSelect = False

        If .Show Then

            strFolder = .SelectedItems(1) & Application.PathSeparator

        Else

            Exit Sub

        End If

    End With

    Set MainDoc = Documents.Add

    strFile = Dir$(strFolder & "*.doc")        ' can change to .docx

    Count = 0

    Do Until strFile = ""

        Count = Count + 1

        Set rng = MainDoc.Range

        With rng

            .Collapse wdCollapseEnd

            If Count > 1 Then

                .InsertBreak wdSectionBreakNextPage

                .End = MainDoc.Range.End

                .Collapse wdCollapseEnd

            End If

            .InsertFile strFolder & strFile

        End With

        strFile = Dir$()

    Loop

    MsgBox ("Files are merged")

lbl_Exit:

    Exit Sub

End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 11-18-2022, 03:50 PM
macropod's Avatar
macropod macropod is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

See:
https://www.msofficeforums.com/151968-post6.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 11-18-2022, 10:54 PM
leeqiang leeqiang is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2019
Advanced Beginner
How to traverse folders and subfolders to merge a word document into one document?
 
Join Date: Aug 2020
Posts: 53
leeqiang is on a distinguished road
Default

How come there is no response after the code runs?
Reply With Quote
  #5  
Old 11-19-2022, 02:58 AM
leeqiang leeqiang is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2019
Advanced Beginner
How to traverse folders and subfolders to merge a word document into one document?
 
Join Date: Aug 2020
Posts: 53
leeqiang is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
This code cannot traverse the folder and its subfolders. It can only merge the word documents under the same folder together. How to modify can traverse folders and subfolders. thank you
Reply With Quote
  #6  
Old 11-19-2022, 03:10 AM
leeqiang leeqiang is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2019
Advanced Beginner
How to traverse folders and subfolders to merge a word document into one document?
 
Join Date: Aug 2020
Posts: 53
leeqiang is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Code:
Sub MergeDocs()

Dim rng As Range

Dim MainDoc As Document

Dim strFile As String, strFolder As String

Dim Count As Long

    With Application.FileDialog(msoFileDialogFolderPicker)

        .Title = "Pick folder"

        .AllowMultiSelect = False

        If .Show Then

            strFolder = .SelectedItems(1) & Application.PathSeparator

        Else

            Exit Sub

        End If

    End With

    Set MainDoc = Documents.Add

    strFile = Dir$(strFolder & "*.doc")        ' can change to .docx

    Count = 0

    Do Until strFile = ""

        Count = Count + 1

        Set rng = MainDoc.Range

        With rng

            .Collapse wdCollapseEnd

            If Count > 1 Then

                .InsertBreak wdSectionBreakNextPage

                .End = MainDoc.Range.End

                .Collapse wdCollapseEnd

            End If

            .InsertFile strFolder & strFile

        End With

        strFile = Dir$()

    Loop

    MsgBox ("Files are merged")

lbl_Exit:

    Exit Sub

End Sub
This code cannot traverse the folder and its subfolders. It can only merge the word documents under the same folder together, and cannot merge the word documents under the same folder together. Some are merged and some are not. How to modify can traverse folders and subfolders. thank you
Reply With Quote
  #7  
Old 11-20-2022, 01:59 PM
macropod's Avatar
macropod macropod is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Quote:
Originally Posted by leeqiang View Post
Quote:
Originally Posted by macropod View Post
This code cannot traverse the folder and its subfolders. It can only merge the word documents under the same folder together. How to modify can traverse folders and subfolders. thank you
Clearly, you haven't tried it...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 11-21-2022, 02:53 AM
leeqiang leeqiang is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2019
Advanced Beginner
How to traverse folders and subfolders to merge a word document into one document?
 
Join Date: Aug 2020
Posts: 53
leeqiang is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Clearly, you haven't tried it...
I tried it
Reply With Quote
  #9  
Old 11-21-2022, 04:54 AM
macropod's Avatar
macropod macropod is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Well, if you tried it, you would know that it works as described...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 04-19-2023, 12:18 AM
leeqiang leeqiang is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2019
Advanced Beginner
How to traverse folders and subfolders to merge a word document into one document?
 
Join Date: Aug 2020
Posts: 53
leeqiang is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Well, if you tried it, you would know that it works as described...

Can I add page numbers for each page after merging files?
Reply With Quote
  #11  
Old 04-19-2023, 12:21 AM
leeqiang leeqiang is offline How to traverse folders and subfolders to merge a word document into one document? Windows 10 How to traverse folders and subfolders to merge a word document into one document? Office 2019
Advanced Beginner
How to traverse folders and subfolders to merge a word document into one document?
 
Join Date: Aug 2020
Posts: 53
leeqiang is on a distinguished road
Default

Can I add page numbers for each page after merging files? The format of page numbers is the current page number and the total number of pages. For example, in the form of 8/800. 8 is the current page number, and 800 is the total number of pages after the merged document.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching through folders/ subfolders and rename files if certain condition is met mihnea96 Excel 1 05-15-2017 07:09 AM
How to traverse folders and subfolders to merge a word document into one document? Merge fields from Excel into word document Tonykiwi Word 5 10-11-2015 12:54 PM
Merge answers into one word document RonNCmale Word 1 01-01-2014 12:28 PM
How to traverse folders and subfolders to merge a word document into one document? Order of Folders in Document Library eliz.bell Word 2 04-03-2012 11:49 AM
Can't create subfolders in shared Contact folders shellyrc7 Outlook 0 03-30-2012 08:18 PM

Other Forums: Access Forums

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