![]() |
|
#1
|
|||
|
|||
![]()
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! |
#2
|
||||
|
||||
![]()
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] |
#3
|
|||
|
|||
![]()
These fields are in the body of the documents.
|
#4
|
||||
|
||||
![]()
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 For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
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 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 |
#6
|
||||
|
||||
![]()
The line is
Code:
If Trim(Split(UCase(.Code.Text), "MERGEFIELD")(1)) = "LABEL" Then
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
||||
|
||||
![]()
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] |
#8
|
|||
|
|||
![]()
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 ![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
ducky831 | Excel Programming | 3 | 09-17-2015 01:36 PM |
![]() |
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 |