Scope of Selection.Find.Format, Macro Search&Replace

T

Tom Brubaker

The Word Search&Replace dialog has a nice feature for searching for
text WITH a given format, WITHOUT a given format, or blindly, and for
replacing with or without new formatting.

Macros access provide the .Font and .Format attributes, but it doesn't
seem to me that they are able to achieve the same results. For
instance, suppose I want to search for all text that is NOT italicized
and replace it with the same text in bold. Two plausible solutions
fail:


(1)
..Font.Italics = False
..Replacement.Font.Bold = True
..Format = True

-> selects all text and boldifies it. Failed to select the non-italics
only.


(2)
..Font.Italics = False
..Replacement.Font.Bold = True
..Format = False

-> again selects all text, but does not apply any bolding. Failed to
select the non-italics only, and failed to apply bolding.



Surely someone has encountered this before. It seems like negative
format searches ("Not bold", "Not Italics", etc.) simply don't work via
macro Any insights?
 
K

Klaus Linke

Hi Tom,

(1) should definitely work.

Things that may have gone wrong...

-- > .Font.Italics = False
.... "Italic*s*" was surely a typo?

-- Better specify .Wrap:
.Wrap = wdFindContinue
.... and perhaps the other parameters, too
.MatchWholeWord = False
(else they can be taken from a previous search, and cause the Find to fail)

-- Some fonts look bold, but aren't (say "Arial Black").

Regards,
Klaus
 
G

Greg

Tom,

Try:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Italic = False
.Replacement.Font.Bold = True
.Execute Replace:=wdReplaceAll
End With
End Sub
 
T

Tom Brubaker

Klaus - you're right, (1) should work; Greg's example is living proof.

I believe I see what the gotcha was for my macro. In addition to True
and False, the .Font.* attributes can apparently also take on the value
wdUndefined. Calling .ClearFormatting sets these values back to
wdUndefined and NOT to False. Thus, if you have set a value to False
but later called .ClearFormatting, it will NOT default back to False,
but rather to wdUndefined, and needs to be set again.

Thank you both

Regards

Tom
 

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