PDA

View Full Version : Solved: Line by line check



lawtai
08-17-2005, 11:37 AM
I was wondering how I can go about doing a line by line check for certain characters. I would need to do this instead of the search since I would want to check for multiple characters/styles.

Or, is it possible to limit your find to a specified section?

Thanks!

Killian
08-17-2005, 11:55 AM
You can specify a range to work with explicitly with ActiveDocument.Range(start,finish)where start and finish are character positions and/or by referencing parts of range defined by the collection objects:
Words (Index)
Characters (Index)
Sentences (Index)
Paragraphs (Index)
Sections (Index)'Example:
Dim myWord As Range
'make each instance of "test" bold in section 2
For Each myWord In ActiveDocument.Sections(2)
If myWord.Text = "test" Then
myWord.Font.Bold = True
End If
Next

lawtai
08-17-2005, 01:04 PM
Will it also let me search within that section and only that section?

Killian
08-18-2005, 01:26 AM
Yes, in the example I gave, each word in the second section of the document is tested.

lawtai
08-18-2005, 05:02 AM
ah ok, how would I go about setting search parameters if I only want to determine if a style is present on that tpage?

Killian
08-18-2005, 05:28 AM
First off, I need to correct an error in the code I gave - you would need to go through the word collection of the defined rangeDim myWord As Range

For Each myWord In ActiveDocument.Sections(2).Range.Words
If myWord.Style = "TestStyle" Then
myWord.Font.Color = vbRed
End If
NextAlthough I don't see the problem with using Find - it will be significantly quicker and can be used on a defined range in the same wayWith ActiveDocument.Sections(2).Range
.Find.ClearFormatting
.Find.Style = ActiveDocument.Styles("TestStyle Char")
.Find.Replacement.ClearFormatting
.Find.Replacement.Style = ActiveDocument.Styles("NewStyle Char")
With .Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Find.Execute Replace:=wdReplaceAll
End With Note that if a style is applied to a word rather than paragraph then you will need to search for and apply a character style (the style name will be modified as in the example: "TestStyle" will be "TestStyle Char")

lawtai
08-18-2005, 06:56 AM
Thanks!