Find Replace w negated option

A

automandc

I have a procedure that eliminates all extra spaces (i.e., more than 2 in a
row) from the document. However, there is one particular style which has a
mono type, and spaces are used to line things up. I want my routine to
search the document and ignore only the paragraphs formatted in that style.
Here is the routine, and a representation of what I want to do (but which
doesn't work):

Sub DeleteExtraSpaces()
'
' Replaces any instance of 3 or more spaces with 2 spaces only.
'
If Not TrackChanges Then
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Underline = wdUnderlineNone
.Style <> "BriefXScript" ' This is what I want to do, but get an
error.
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute FindText:=" {3,}", ReplaceWith:=" ", Replace:=wdReplaceAll
.ClearFormatting
End With
End If
End Sub

Thanks in advance!
 
J

Jezebel

..Style is a property of the Find object, so it takes a value not a function.
Unfortunately there are no negation functions (other than the wildcard not)
for find, so you need to work paragraph by paragraph --

Dim pPar As Word.Paragraph

For Each pPar In ActiveDocument.Paragraphs
If pPar.Style <> "BriefXScript" Then
With pPar.Range.Find
.MatchWildcards = True
.Execute FindText:=" {3,}", ReplaceWith:=" ",
Replace:=wdReplaceAll
End With
End If
Next
 
T

Tony Jollans

Or you could change the spaces in BriefXScript to, say, hard spaces, and
then back again afterwards ..

Sub DeleteExtraSpaces()
'
' Replaces any instance of 3 or more spaces with 2 spaces only.
'
If Not TrackChanges Then
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True

.Style = "BriefXScript"
.Execute FindText:=" ", ReplaceWith:=Chr(160),
Replace:=wdReplaceAll
.ClearFormatting
.Font.Underline = wdUnderlineNone
.Execute FindText:=" {3,}", ReplaceWith:=" ",
Replace:=wdReplaceAll
.ClearFormatting
.Style = "BriefXScript"
.Execute FindText:=Chr(160), ReplaceWith:=" ",
Replace:=wdReplaceAll
End With
End If
End Sub
 
A

automandc

That would probably work too. I use non-breaking spaces a lot (I assume
that's what you mean by "hard space"?

I tried the paragraph-by-paragraph method, and it was definitely a lot slower.
 

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