Use VBA Find to identify paragraph whose style does not matchspecified style

D

dedawson

The task is to Find and Replace some text values in all paragraphs
except those formatted with the a specified style, such as DataLine.

I understand that one can use Selection.Find.Style =
ActiveDocument.Styles("Data Line") to find any paragraph formatted
with the DataLine style. The question then is, does anyone know of a
means of employing Selection.Find.Style (or any other method for that
matter) of Finding all paragraphs that do NOT have the DataLine style?
 
J

Jay Freedman

You got it backwards. You can't search for a paragraph whose style isn't a particular one.

What you can do is search for the text you want to replace; each time you find an instance, check the style at that location and do the replacement only if the style isn't "Data Line".

Try this with the standard "quick brown fox" text:

Sub demo()
Dim Rg As Range
Set Rg = ActiveDocument.Range
With Rg.Find
.Text = "fox"
While .Execute
If Rg.Paragraphs(1).Style <> "Data Line" Then
Rg.Text = "elephant"
Rg.Collapse wdCollapseEnd
End If
Wend
End With
End Sub
 
D

dedawson

Thanks Jay.

Turns out that although your approach works in general, it fails with
the specific data I was feeding it. The text I was searching for was
white space paragraph mark (^w^p). Since your approach implements a
pseudo-Replace (via insertion of a character string) I had to change
the Replace value from ^p to CHR(13); no big deal. This worked well
until the white space occurred in a numbered paragraph. In such
instances, the process failed as the insertion of the CHR(13) results
in the existing paragraph picking up the style of the following
paragraph, which was not always the same as the existing paragraph.

I ended up solving the problem by resorting to a trick I've employed
in solving similar problems.
1. Flag the paragraphs I want to exclude
2. Do my global processing
3. Remove the flags.

The code is shown below to help any others who might need to do this
sort of thing

Selection.HomeKey unit:=wdStory ' Because Replace:=wdReplaceAll does
not behave well
' if started in middle of file,
despite .Wrap = wdFindContinue

' Check for presence of Data Line style. If present, flag all
occurrences to exclude from Replace
If StyleExists("Data Line") Then ' Flag all Data Line paragraphs
to prevent removal of trailing white space
Selection.Find.Style = ActiveDocument.Styles("Data Line")
With Selection.Find
.Text = "^p"
.Replacement.Text = "Exclude-from-global-Replace^p"
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Now remove all trailing white space from all paragraphs
With Selection.Find
.Text = "^w^p"
.Replacement.Text = "^p"
.Format = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Now remove all Data Line flags
Selection.Find.Style = ActiveDocument.Styles("Data Line")
With Selection.Find
.Text = "Exclude-from-global-Replace^p"
.Replacement.Text = "^p"
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If



Function StyleExists(styleName As String) As Boolean
Dim currentStyle As Style
Dim stylePresent As Boolean
stylePresent = False

' Check for existence of style in active document.
For Each currentStyle In ActiveDocument.Styles
If currentStyle.NameLocal = styleName Then
stylePresent = True
Exit For
End If
Next currentStyle
' Return
StyleExists = stylePresent
End Function




Cheers,

david
 
B

Bryce Jett

DeDawson,

Is there a way to modify this to find formatted text with no style and have a new style created based on it? I have very little knowledge with macros.
 
D

dedawson

In 25 words or less: Not really.

So, let's think about what you say you want to do for a minute.
Keep in mind that every item in Word is associated with some Paragraph
and Character style, owing to Word defaults.

So when you say look for formatted text with no style, what you're
saying is look for text with Direct Formatting that currently has no
defined Character or Paragraph style associated with it.

But there's a problem. While one can detect whether or not a selected
bit of text has any direct formatting applied to it, one cannot search
for the application of direct formatting.

Just the same, if one has some text with direct formatting selected,
yes, one could then create a new style based on the characteristics of
that direct formatting (after programmatically determining all the
formatting attributes of the selection). But, no, you really don't
want to do that, because you would be potentially be defining dozens
of character styles, which each require unique names, which, by the
way, your code would have to generate.

The best bet is to forget about coding this, and simply define styles
you know you would like to use ahead of time, with meaningful names
that you choose, and then apply them to items as may be necessary.
 

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