View Single Post
 
Old 10-18-2017, 11:00 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,342
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

Try:
Code:
Option Explicit
Dim FSO As Object, oFolder As Object, StrFolds As String, StrDocs As String, StrTgt As String
 
Sub Main()
Application.ScreenUpdating = False
Application.DisplayAlerts = wdAlertsNone
Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long
TopLevelFolder = GetFolder
If TopLevelFolder = "" Then Exit Sub
StrTgt = ThisDocument.FullName
StrFolds = vbCr & TopLevelFolder
If FSO Is Nothing Then
  Set FSO = CreateObject("Scripting.FileSystemObject")
End If
'Get the sub-folder structure
Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders
For Each aFolder In TheFolders
  RecurseWriteFolderName (aFolder)
Next
'Process the documents in each folder
For i = 1 To UBound(Split(StrFolds, vbCr))
  Call UpdateDocuments(CStr(Split(StrFolds, vbCr)(i)))
Next
ThisDocument.Range.Text = "Matches found in:" & StrDocs
Application.DisplayAlerts = wdAlertsAll
Application.ScreenUpdating = True
End Sub

Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFolds = StrFolds & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
  RecurseWriteFolderName (SubFolder)
Next
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Sub UpdateDocuments(oFolder As String)
Dim strFldr As String, strFile As String, wdDoc As Document, i As Long
strFldr = oFolder
If strFldr = "" Then Exit Sub
strFile = Dir(strFldr & "\*.doc", vbNormal)
While strFile <> ""
  If strFldr & "\" & strFile <> StrTgt Then
    Set wdDoc = Documents.Open(FileName:=strFldr & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=False, Visible:=False)
    With wdDoc
      For i = 1 To .Fields.Count
        With .Fields(i)
          If .Type = wdFieldIf Then
            With .Code.Fields(1)
              If .Type = wdFieldMergeField Then
                If Trim(Split(UCase(.Code.Text), "MERGEFIELD")(1)) = "LABEL" Then
                  StrDocs = StrDocs & vbCr & strFldr & "\" & strFile
                  Exit For
                End If
              End If
            End With
          End If
        End With
      Next
      .Close False
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
End Sub
The code includes a folder browser, so all you need do is select the top-level folder you want to process and both that and all it's sub-folders will be tested. Any matches will be output to the document you add the code to.

For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote