Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-18-2017, 11:00 PM
macropod's Avatar
macropod macropod is offline Finding specific text in IF-fields across multiple Word documents Windows 7 64bit Finding specific text in IF-fields across multiple Word documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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
  #2  
Old 10-19-2017, 12:32 AM
Office_Worker Office_Worker is offline Finding specific text in IF-fields across multiple Word documents Windows 8 Finding specific text in IF-fields across multiple Word documents Office 2013
Novice
Finding specific text in IF-fields across multiple Word documents
 
Join Date: Oct 2017
Posts: 4
Office_Worker is on a distinguished road
Default

Thanks!
What does the following line do in the ways of selecting the code fields or texts?

Code:
If Trim(Split(UCase(.Code.Text), "MERGEFIELD")(1)) = "Label" Then
I replaced 'Label' with the relevant information and left 'MERGEFIELD' in place.
The code runs and opens/closes all the relevant docs but the result comes up empty which I know can't be true. It might be that I need to replace 'MERGEFIELD' with something but doing so makes the code stop running in this line. If I then mouse over '.Code.Text' it shows me an unrelated fieldname as a value.

Last edited by macropod; 10-19-2017 at 12:57 AM. Reason: Deleted unnecessary quote of entire post replied to
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding specific text in IF-fields across multiple Word documents Help creating VBA code to search multiple word documents for specific group of words dmreno Word VBA 3 07-30-2019 02:31 PM
referenceing/finding word from one documents into another documents and indicating their location rok123 Word VBA 1 02-07-2016 04:50 PM
Finding specific text in IF-fields across multiple Word documents Excel Macro finding a specific word ducky831 Excel Programming 3 09-17-2015 01:36 PM
Finding specific text in IF-fields across multiple Word documents Macro to insert multiple pictures to word to a specific size and text wrap mescaL Word VBA 3 11-03-2014 10:51 PM
Finding specific text within body of email Tammfran Outlook 0 03-14-2014 02:32 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft