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 / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
LDMueller - 19 Feb 2006 22:22 GMT
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
Doug Robbins - Word MVP - 20 Feb 2006 05:15 GMT
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

Signature

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

> Hello,
>
[quoted text clipped - 75 lines]
>    Selection.MoveDown Unit:=wdLine, Count:=1
> End Sub
LDMueller - 20 Feb 2006 05:42 GMT
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

> The following should do it:
>
[quoted text clipped - 90 lines]
> >    Selection.MoveDown Unit:=wdLine, Count:=1
> > End Sub
Doug Robbins - Word MVP - 20 Feb 2006 12:41 GMT
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?

Signature

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

> Doug,
>
[quoted text clipped - 120 lines]
>> >    Selection.MoveDown Unit:=wdLine, Count:=1
>> > End Sub
LDMueller - 20 Feb 2006 13:01 GMT
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)

> 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
[quoted text clipped - 128 lines]
> >> >    Selection.MoveDown Unit:=wdLine, Count:=1
> >> > End Sub
Doug Robbins - Word MVP - 20 Feb 2006 16:26 GMT
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?

Signature

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

>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
[quoted text clipped - 143 lines]
>> >> >    Selection.MoveDown Unit:=wdLine, Count:=1
>> >> > End Sub
Helmut Weber - 20 Feb 2006 16:50 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Doug Robbins - Word MVP - 20 Feb 2006 18:22 GMT
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

Signature

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

> Hi,
>
[quoted text clipped - 21 lines]
>
> There are so many ways.
LDMueller - 22 Feb 2006 14:06 GMT
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

> Actually, I am now thinking that the text might be of the form
>
[quoted text clipped - 49 lines]
> >
> > There are so many ways.
Doug Robbins - Word MVP - 22 Feb 2006 21:13 GMT
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

Signature

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

> Doug,
>
[quoted text clipped - 78 lines]
>> >
>> > There are so many ways.
LDMueller - 28 Feb 2006 11:17 GMT
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

> That makes a world of difference.  I think that the following should do what
> you want:
[quoted text clipped - 102 lines]
> >> >
> >> > There are so many ways.
Doug Robbins - Word MVP - 28 Feb 2006 20:59 GMT
Can you send me a document in which the problems occur and I will try and
sort it out.

Signature

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

> Doug,
>
[quoted text clipped - 124 lines]
>> >> >
>> >> > There are so many ways.
LDMueller - 10 Mar 2006 14:06 GMT
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

> Can you send me a document in which the problems occur and I will try and
> sort it out.
[quoted text clipped - 127 lines]
> >> >> >
> >> >> > There are so many ways.
Doug Robbins - Word MVP - 10 Mar 2006 19:43 GMT
Remove the UpperCase letters from my email address.

Signature

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

> Hi Doug,
>
[quoted text clipped - 141 lines]
>> >> >> >
>> >> >> > There are so many ways.
LDMueller - 31 Mar 2006 16:03 GMT
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.

> Remove the UpperCase letters from my email address.
>
[quoted text clipped - 143 lines]
> >> >> >> >
> >> >> >> > There are so many ways.
LDMueller - 22 Feb 2006 14:40 GMT
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

> Hi,
>
[quoted text clipped - 21 lines]
>
> There are so many ways.
Helmut Weber - 20 Feb 2006 16:31 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

LDMueller - 22 Feb 2006 14:40 GMT
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

> Hi,
>
[quoted text clipped - 13 lines]
> Next
> End Sub

Rate this thread:






 
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.