L
LDMueller
Hello,
I have Word 2003. I'm trying to search and highlight a paragph which has
the word "KeepTogetherTag" as the beginning of the paragraph and the word
"KeepTogetherTag1" as the end of the paragrah. Once this paragraph is
highlighted, I to to Format, Paragraph, Line and Page Breaks and under
Pagination I want to ensure only "Keep with Next" is checked.
Below, I've provided my code to do this.
My problem is this. Once I select and format the first paragraph, I need it
to locate the next paragraph like this, format it and continue to the end of
the document.
I can write a little VBA and have tried Do While and Loop, but can't seem to
write it correctly so nothing works.
Any assistance you provide would be greatly appreciated!
Sub FormatP()
' Search document for the word KeepTogetherTag
Selection.Find.ClearFormatting
With Selection.Find
.Text = "KeepTogetherTag"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
' Once you find KeepTogetherTag, go to beginning of the line
' and press F8 to extend next search
Selection.HomeKey Unit:=wdLine
Selection.Extend
Selection.Find.ClearFormatting
' Search document for the word KeepTogetherTag1
' Now a paragraph is highlighted so I can format it to keep together
With Selection.Find
.Text = "KeepTogetherTag1"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
' Format selected paragraph by going to Format, Paragraph, Line and Page
Breaks
' and under Pagination make sure only "Keep with Next" is checked
With Selection.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.WidowControl = False
.KeepWithNext = True
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
End With
' Move down one line so you can search for the next paragraph
' beginning with KeepTogetherTag
Selection.MoveDown Unit:=wdLine, Count:=1
End Sub
I have Word 2003. I'm trying to search and highlight a paragph which has
the word "KeepTogetherTag" as the beginning of the paragraph and the word
"KeepTogetherTag1" as the end of the paragrah. Once this paragraph is
highlighted, I to to Format, Paragraph, Line and Page Breaks and under
Pagination I want to ensure only "Keep with Next" is checked.
Below, I've provided my code to do this.
My problem is this. Once I select and format the first paragraph, I need it
to locate the next paragraph like this, format it and continue to the end of
the document.
I can write a little VBA and have tried Do While and Loop, but can't seem to
write it correctly so nothing works.
Any assistance you provide would be greatly appreciated!
Sub FormatP()
' Search document for the word KeepTogetherTag
Selection.Find.ClearFormatting
With Selection.Find
.Text = "KeepTogetherTag"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
' Once you find KeepTogetherTag, go to beginning of the line
' and press F8 to extend next search
Selection.HomeKey Unit:=wdLine
Selection.Extend
Selection.Find.ClearFormatting
' Search document for the word KeepTogetherTag1
' Now a paragraph is highlighted so I can format it to keep together
With Selection.Find
.Text = "KeepTogetherTag1"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
' Format selected paragraph by going to Format, Paragraph, Line and Page
Breaks
' and under Pagination make sure only "Keep with Next" is checked
With Selection.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.WidowControl = False
.KeepWithNext = True
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
End With
' Move down one line so you can search for the next paragraph
' beginning with KeepTogetherTag
Selection.MoveDown Unit:=wdLine, Count:=1
End Sub