Word

Insert All Documents From a Specified Folder

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro will insert all the Word Documents from a specified folder into the current document. 

Discussion:

You have several files that you want to combine into one file. This macro will do all the work for you, just specify the folder to work with. 

Code:

instructions for use

			

Option Compare Text Option Explicit Private Const BIF_RETURNONLYFSDIRS As Long = &H1 Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2 Private Const BIF_RETURNFSANCESTORS As Long = &H8 Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000 Private Const BIF_BROWSEFORPRINTER As Long = &H2000 Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000 Private Const MAX_PATH As Long = 260 Type BrowseInfo hOwner As Long pidlRoot As Long pszDisplayName As String lpszINSTRUCTIONS As String ulFlags As Long lpfn As Long lParam As Long iImage As Long End Type Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Boolean hNameMappings As Long lpszProgressTitle As String End Type Declare Function SHGetPathFromIDListA Lib "shell32.dll" ( _ ByVal pidl As Long, _ ByVal pszBuffer As String) As Long Declare Function SHBrowseForFolderA Lib "shell32.dll" ( _ lpBrowseInfo As BrowseInfo) As Long Function BrowseFolder(Optional Caption As String = "") As String Dim BrowseInfo As BrowseInfo Dim FolderName As String Dim ID As Long Dim Res As Long With BrowseInfo .hOwner = 0 .pidlRoot = 0 .pszDisplayName = String$(MAX_PATH, vbNullChar) .lpszINSTRUCTIONS = Caption .ulFlags = BIF_RETURNONLYFSDIRS .lpfn = 0 End With FolderName = String$(MAX_PATH, vbNullChar) ID = SHBrowseForFolderA(BrowseInfo) If ID Then Res = SHGetPathFromIDListA(ID, FolderName) If Res Then BrowseFolder = Left$(FolderName, InStr(FolderName, _ vbNullChar) - 1) End If End If End Function Sub InsertDocs() Dim Prompt As String Dim Title As String Dim Path As String Dim FileName As String Dim MyResponse As VbMsgBoxResult WordBasic.DisableAutoMacros True '*** Get folder from user *** Prompt = "Select the folder with the files that you want to insert." Title = "Folder Selection" MsgBox Prompt, vbInformation, Title Path = BrowseFolder("Select A Folder") If Path = "" Then Prompt = "You didn't select a folder. The procedure has been canceled." Title = "Procedure Canceled" MsgBox Prompt, vbCritical, Title GoTo Canceled: End If '*** Confirm the procedure before continuing *** Prompt = "Are you sure that you want to insert all the files in the folder:" & _ vbCrLf & Path & "?" Title = "Confirm Procedure" MyResponse = MsgBox(Prompt, vbQuestion + vbYesNo, Title) If MyResponse = vbNo Then GoTo Canceled: End If Application.DisplayAlerts = False Application.ScreenUpdating = False '*** Loop through all Word documents and search each of them for the specified criteria*** FileName = Dir(Path & "\*.doc", vbNormal) Do Until FileName = "" On Error Resume Next Selection.InsertFile FileName:=Path & "\" & FileName, Range:="", ConfirmConversions:= _ False, Link:=False, Attachment:=False Selection.InsertBreak Type:=wdPageBreak On Error GoTo 0 FileName = Dir() Loop Canceled: WordBasic.DisableAutoMacros False Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub

How to use:

  1. Copy the code above.
  2. Open Word.
  3. Alt + F11 to open the Visual Basic Editor.
  4. On the left, choose Normal (or normal.dot).
  5. Hit Insert-Module from the menu.
  6. Paste the code into the window that appears at right.
  7. Close the VBE (Alt + Q or press the x in the top right corner).
 

Test the code:

  1. Hit Tools-Macro-Macros and double-click InsertDocs.
 

Sample File:

InsertDocs.ZIP 13.86KB 

Approved by mdmackillop


This entry has been viewed 304 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express