Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-18-2017, 01:16 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 Finding specific text in IF-fields across multiple Word documents

Hello,



I work for a company where we use several different labels (company names, logo's etc). In order to make sure our correspondence gets the label appropriate texts we've been using IF-fields to match the text to the label.

For instance [IF [MERGEFIELD Label] = "label1" "text1" "text2" ]

One of the labels is getting a new name and new label-specific information. We would now like to run a VBA script to find all the documents that have IF-constructs that call upon the old label name.

When we use the Explorer's 'content:' search option it only shows Word documents that have the label name in the body (plain text) of the document and not in the IF-constructs.

Is it possible to write a VBA macro that searches for text used in IF-constructs and then lists them in either a new Word document or an Excel document?

Much thanks in advance!
Reply With Quote
  #2  
Old 10-18-2017, 12:55 PM
macropod's Avatar
macropod macropod is online now 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,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

In which part of the documents are these fields - the body or a page header? If a page header, which one?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-18-2017, 10:17 PM
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

Quote:
Originally Posted by macropod View Post
In which part of the documents are these fields - the body or a page header? If a page header, which one?
These fields are in the body of the documents.
Reply With Quote
  #4  
Old 10-18-2017, 11:00 PM
macropod's Avatar
macropod macropod is online now 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,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
  #5  
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
  #6  
Old 10-19-2017, 12:58 AM
gmayor's Avatar
gmayor gmayor is offline Finding specific text in IF-fields across multiple Word documents Windows 10 Finding specific text in IF-fields across multiple Word documents Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The line is
Code:
If Trim(Split(UCase(.Code.Text), "MERGEFIELD")(1)) = "LABEL" Then
i.e. in upper case!
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 10-19-2017, 12:59 AM
macropod's Avatar
macropod macropod is online now 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,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

The code Trim(Split(UCase(.Code.Text), "MERGEFIELD")(1))
simply gets the mergefield name as an upper-case string. The code's If test simply checks whether that string is "LABEL" - also an upper-case string.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 10-19-2017, 03:50 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

I see!

With that little bit of extra info I've managed to run the script and find all the documents with the particular Mergefield. Thanks a lot for your help, everyone
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 02:28 AM.


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