PDA

View Full Version : How to insert breaks before every word "Report"?



akokin
04-02-2008, 01:38 AM
Hello.
There is one document. It have some text and the words "Report" within this text. I need to insert before every word "Report" break page. I wrote code but it insert only one break. That it is:

Sub insSections()
Dim sOt As Variant
Dim aStory As StoryRanges
Set aStory = ActiveDocument.StoryRanges
With Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.Execute FindText:="Report"
End With
If Selection.Find.Found Then
For Each sOt In aStory
Selection.MoveLeft unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdSectionBreakNextPage
Next
End If
End Sub
How to insert breaks before every word "Report"?
Thank you.

Nelviticus
04-02-2008, 01:54 AM
This should work:
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.Text = "Report"
.Replacement.Text = "^mReport"
.Execute Replace:=wdReplaceAll
End With
Regards

akokin
04-02-2008, 02:51 AM
This should work:
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.Text = "Report"
.Replacement.Text = "^mReport"
.Execute Replace:=wdReplaceAll
End With Regards
Thank you very much!

EricFletcher
04-02-2008, 07:14 AM
Note that you don't really need to use code to do this. Nelviticus' code is the VBA version of using the Find and Replace dialog: the "Special" pulldown (click More to see it) inserts codes for many special items, including a manual page break (^m) and a section break (^b). If you put "Report^p" in the Find what box, and "^mReport^p" inthe Replace with box, you will get the same result (don't include the " symbols).

Actually, the code snippet provided by Nelviticus will end up putting a page break before every instance of the word "Report" so you really should include the paragraph marker (^p) as I've done above.

fumei
04-02-2008, 12:26 PM
Excuse me, but there has been NO mention that "Report" is a single paragraph - i.e. it is always "Report" followed by a paragraph mark.

Yes, I think it is probably safe to assume that, but we do not know for sure.

Is it ALWAYS going to be "Report" - and ONLY "Report" - that is significant? It is ALWAYS going to be "Report" followed by a paragraph mark?

Report
Report 1.1
Yadda yadda 1111

Report
Report 2.2
Yadda yadda 22222

In any case, it is true that VBA is not required for this, but presumably there is a reason to do it with code. A single button click, or keyboard shortcut, to do the job seems viable to me.

EricFletcher
04-02-2008, 01:10 PM
Excuse me, but there has been NO mention that "Report" is a single paragraph - i.e. it is always "Report" followed by a paragraph mark.
Fumei is absolutely correct of course; I did make that assumption (and should know better! :thumb)

Akokin's original code and the suggestion from nelviticus would both add the page break in every instance of the found word; my probably-too-quick response pointed that out, but neglected to elaborate on how it would still not address all instances.

This is a good example of how risky it can be to just run a macro or issue a command without doing a thorough analysis first. If you use the Find dialog box, you can get a sense of the impact by choosing "Highlight all items" and clicking Find All before committing to the Find and Replace. This highlights and displays a count of all items that meet the Find what criteria.

If there are constructs such as those identified by Fumei, you can usually still define them. For example, "^pReport" would find all paragraphs beginning with "Report" (except if it is the 1st line of the document). However, you'd also find a paragraph starting with "Reporter John Smith..." so be careful. As well, wildcard options in the F&R dialog (and in VBA code) can also be used for more complex definitions.

akokin
04-02-2008, 11:37 PM
Dear Gerry and Eric. Thank you very much for your explanations and replies. Really I need to insert break before alone words "Report" with a paragraph mark.
Sincerely, Anton.

fumei
04-04-2008, 06:38 AM
And there you go. If it is identified explicitly - it is "Report" alone, with a paragraph mark - then there is no wondering. I would never have posted any questions if it was clearly and explicitly stated.

If it is NOT identified explicitly, then...we do wonder.

Which is why we try to get people to state things clearly, and explicitly.