Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Brubaker - 04 Jan 2006 18:46 GMT
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?
Klaus Linke - 04 Jan 2006 19:10 GMT
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

> 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
[quoted text clipped - 25 lines]
> format searches ("Not bold", "Not Italics", etc.) simply don't work via
> macro   Any insights?
Greg - 04 Jan 2006 19:11 GMT
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
Tom Brubaker - 04 Jan 2006 19:40 GMT
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
Klaus Linke - 04 Jan 2006 21:08 GMT
Glad you got it sorted!

:-)  Klaus

> Klaus - you're right, (1) should work; Greg's example is living proof.
>
[quoted text clipped - 10 lines]
>
> Tom
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.