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

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
 
D

Doug Robbins - Word MVP

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
 
L

LDMueller

Doug,

Thank you so much for the quick response. Unfortunately, this didn't work
and perhaps I didn't explain it well enough and more than likely the code I
provided mislead you (sorry).

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 paragraphs themselves are different sizes and in different locations
throughout the document.

My main objective is to go through the document, highlight what is between
the tags and format this paragraph to "Keep with Next". After it does one,
it searches again, locates another paragraph, formats it and continues doing
this to the end of the document.

I hope this makes sense.

Thanks, Leigh
 
D

Doug Robbins - Word MVP

I believe that I understood what you wanted to do and I think that the macro
that I gave you should do it, unless the tags are not exactly as you
indicated.

If the tags are not exactly as you indicated, what are they?

If the tags are correct, what happened when you ran the macro?

--
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
 
L

LDMueller

I assumed I was to only use your code and not mine. When I ran it, it came
back with a "Run-time error '5': Invalid procedure call or argument" on
pstring = Mid(prange.Text, 16, Len(prange) - 32)
 
D

Doug Robbins - Word MVP

It should not do that.

When I run the macro with a document containing the following text

KeepTogetherTagThe quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog.KeepTogetherTag1

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

KeepTogetherTagThe quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog.KeepTogetherTag1

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

it produces:

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

with the first and third paragraphs formatted as "Keep with next".

Is that the way that your tags are arranged?


--
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
 
H

Helmut Weber

Hi,

how about this one:

Sub Macro15()
Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Paragraphs
With oPrg.Range
If Left(.Text, 15) = "KeepTogetherTag" _
And Right(.Text, 17) = "KeepTogetherTag1" & chr(13) Then
.Select ' for testing only
' delete if ok
.ParagraphFormat.KeepWithNext = True
End If
End With
Next
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

Helmut Weber

Hi,

I see now that the tag should be removed.
In case it is just there like it is,
not enclosed in pointed brackets or something similar:

Sub Macro15()
Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Paragraphs
With oPrg.Range
If Left(.Text, 15) = "KeepTogetherTag" _
And Right(.Text, 17) = "KeepTogetherTag1" & chr(13) Then
With .Find
.Text = "KeepTogetherTag"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
.Characters.Last.Previous = "" ' the "1" left over
.ParagraphFormat.KeepWithNext = True
End If
End With
Next
End Sub


There are so many ways.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Doug Robbins - Word MVP

Actually, I am now thinking that the text might be of the form

KeepTogetherTag¶
The quick brown fox jumps over the lazy dog. The quick brown
fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over
the lazy dog.¶
KeepTogetherTag1¶

That would cause the error that the OP is getting.

If that is what the OP has, then the following should work

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
prange.End = ActiveDocument.Range.End
prange.End = prange.Paragraphs(3).Range.End
prange.Paragraphs(3).Range.Delete
prange.Paragraphs(2).Range.ParagraphFormat.KeepWithNext = True
prange.Paragraphs(1).Range.Delete
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
 
L

LDMueller

Doug,

When I've been referencing that the "KeepTogetherTag" tags are surrounding a
paragraph, I neglected to mention inside this paragraph is a table. I didn't
think it was relevant, but perhaps that was my mistake.

Below is kinda what it looks like...

KeepTogetherTag
Atty Hours Rate Fees
Joan 4.00 $100.00 $400.00 Phone $32.00
John 2.00 $150.00 $300.00 Copy $150.00
Jason 1.00 $300.00 $300.00 Postage 1.00
Mike 2.00 $150.00 $300.00
Total Fees: $1,300.00 Total Costs: $183.00

Total for Bill $1,483.00
KeepTogetherTag1

Basically I need to the table and the line "Total for Bill" to stay
together, not break apart onto two pages.

Thanks, Leigh
 
L

LDMueller

Helmut,

First, thank you for you assistance. I really appreciate it!

After running this macro, when I click inside the paragraph, and go to
Format, Paragraph, Line and Page Breaks, the only option checked is the
Window/Orphan control.

When I've been referencing that the "KeepTogetherTag" tags are surrounding a
paragraph, I neglected to mention inside this paragraph is a table. I didn't
think it was relevant, but perhaps that was my mistake.

Below is kinda what it looks like...

KeepTogetherTag
Atty Hours Rate Fees
Joan 4.00 $100.00 $400.00 Phone $32.00
John 2.00 $150.00 $300.00 Copy $150.00
Jason 1.00 $300.00 $300.00 Postage 1.00
Mike 2.00 $150.00 $300.00
Total Fees: $1,300.00 Total Costs: $183.00

Total for Bill $1,483.00
KeepTogetherTag1

Basically I need to the table and the line "Total for Bill" to stay
together, not break apart onto two pages.

Thanks, Leigh
 
L

LDMueller

Helmut,

After running this macro, when I click inside the paragraph, and go to
Format, Paragraph, Line and Page Breaks, the only option checked is the
Window/Orphan control.

Thanks, Leigh
 
D

Doug Robbins - Word MVP

That makes a world of difference. I think that the following should do what
you want:

Dim prange As Range, pstring As String, prangedup As Range, i As Long
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
Set prangedup = prange.Duplicate
prange.Duplicate.Start = prange.End
prangedup.End = ActiveDocument.Range.End
prangedup.End = prangedup.Start + InStr(prangedup,
"KeepTogetherTag1")
Set prange = prangedup.Duplicate
For i = 1 To prange.Paragraphs.Count - 2
prange.Paragraphs(i).Range.ParagraphFormat.KeepWithNext = True
Next i
prange.Paragraphs(prange.Paragraphs.Count).Range.Delete
prange.Paragraphs(1).Range.Delete
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
 
L

LDMueller

Doug,

This is almost perfect, with one exception. Occasionally it's deleting text
after the KeepTogetherTag1.

In one example, it deleted the contents of the first box of a table that
follows the KeepTogetherTag1.

Unfortunately, since your code is more complex than anything I could write,
I can't even follow what it doing to try to correct it myself.

Any suggestions would be welcomed.
Thanks, Leigh
 
D

Doug Robbins - Word MVP

Can you send me a document in which the problems occur and I will try and
sort it out.

--
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
 
L

LDMueller

Hi Doug,

I have a file for you, but I don't know how to send it to you. I don't see
an option on the screen within the discussion group.

Leitgh
 
D

Doug Robbins - Word MVP

Remove the UpperCase letters from my email address.

--
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
 
L

LDMueller

Hi Doug,

Sorry it's taken so long to respond to you. I was able to use the last code
you sent me after altering a few items on the template. I want to thank you
so much for assisting me with this.
 

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