Re: Search for paragraph, format, & continue this through rest of docu



The following should do it:

Dim prange As Range, pstring As String
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="KeepTogetherTag", MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
Set prange = Selection.Paragraphs(1).Range
pstring = Mid(prange.Text, 16, Len(prange) - 32)
prange.Text = pstring & vbCr
prange.ParagraphFormat.KeepWithNext = True
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"LDMueller" <LDMueller@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B06DA3C4-0DF7-4147-AF00-2CEE71136C9F@xxxxxxxxxxxxxxxx
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



.



Relevant Pages

  • Re: Search for paragraph, format, & continue this through rest of
    ... If the tags are not exactly as you indicated, ... The reason my document has the "KeepTogetherTag" as the beginning of the ... paragraph and the "KeepTogetherTag1" as the end of the paragraph is ... the tags and format this paragraph to "Keep with Next". ...
    (microsoft.public.word.vba.general)
  • Re: Search for paragraph, format, & continue this through rest of
    ... The reason my document has the "KeepTogetherTag" as the beginning of the ... paragraph and the "KeepTogetherTag1" as the end of the paragraph is ... the tags and format this paragraph to "Keep with Next". ... .MatchWholeWord = False ...
    (microsoft.public.word.vba.general)
  • Re: Search for paragraph, format, & continue this through rest of
    ... The reason my document has the "KeepTogetherTag" as the beginning of the ... paragraph and the "KeepTogetherTag1" as the end of the paragraph is because ... the tags and format this paragraph to "Keep with Next". ... .MatchWholeWord = False ...
    (microsoft.public.word.vba.general)
  • Re: Importing Word text into Excel
    ... .Format = False ... .MatchWholeWord = False ... The paragraphs (actually lines because there is a paragraph ... "macropod" wrote: ...
    (microsoft.public.excel.programming)
  • Search for paragraph, format, & continue this through rest of docu
    ... I'm trying to search and highlight a paragph which has ... the word "KeepTogetherTag" as the beginning of the paragraph and the word ... Once I select and format the first paragraph, ... ' Search document for the word KeepTogetherTag ...
    (microsoft.public.word.vba.general)