How to tell if a word is within a MERGEFIELD?

H

hafabee

I have some VBA code that loops through a range word by word looking
for some specific words.

I want to ignore a word if is part of the name of a MERGEFIELD, but I
cannot figure out how to do it.

I *have* figured out how to exclude urls and emails.

Here is the skeleton of my current code:

For Each rngWord In rngLookat.Words
If rngWord.Hyperlinks.Count = 0 then
If StrComp(LCase(Trim(rngWord.Text)), "status") = 0 then
' Do what I do
End If
End If
Next rngWord

I would like to ignore the word if it part of a MERGEFIELD name, such
as MEMBER_STATUS.

Any ideas would be appreciated.
 
C

Cindy M.

Hi Hafabee,
I have some VBA code that loops through a range word by word looking
for some specific words.

I want to ignore a word if is part of the name of a MERGEFIELD, but I
cannot figure out how to do it.
Sadly, Microsoft hasn't given us a function that will return whether a
Range or Selection is within a field. So you need to work in a somewhat
round-about way. Basically, you have to loop through every Field in the
document, check whether it's a Mergefield. If it is, check whether the
range (or selection.range) in question is InRange of that field.

Here's a bit of sample code. In order to stream-line things a bit, I
first put all the MergeField ranges in an array. I then need only loop
through the array to check InRange:

Option Explicit

Dim aRanges() As Word.Range

Sub WalkWordsInDoc()
Dim doc As Word.Document
Dim rng As Word.Range
Dim fld As Word.Field
Dim fldCounter As Long

Set doc = ActiveDocument
fldCounter = 0
For Each fld In doc.Fields
If fld.Type = wdFieldMergeField Then
ReDim Preserve aRanges(fldCounter)
Set aRanges(fldCounter) = fld.Result
fldCounter = fldCounter + 1
End If
Next

For Each rng In doc.Words
If IsRangeInMergeField(rng) Then
MsgBox "Merge field!"
End If
Next
End Sub

Function IsRangeInMergeField(rng As Word.Range) As Boolean
Dim rngField As Word.Range
Dim counter As Long

For counter = LBound(aRanges()) To UBound(aRanges())
Set rngField = aRanges(counter)
If rng.InRange(rngField) Then
IsRangeInMergeField = True
Exit For
End If
Next
End Function
I *have* figured out how to exclude urls and emails.

Here is the skeleton of my current code:

For Each rngWord In rngLookat.Words
If rngWord.Hyperlinks.Count = 0 then
If StrComp(LCase(Trim(rngWord.Text)), "status") = 0 then
' Do what I do
End If
End If
Next rngWord

I would like to ignore the word if it part of a MERGEFIELD name, such
as MEMBER_STATUS.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top