Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make this Word macro loop?

Status
Not open for further replies.

tombaraider

Technical User
Feb 20, 2004
20
US
I have a client who created a macro in Word that finds the word 'Report', then forces a new page at every occurrence it finds. He needs to make the macro loop til the end of any document he runs it in. I researched this and found that the macro only needs a couple of short lines of new code but I was a little confused. I would appreciate if someone could me give the solution. Here is the code:

Option Explicit

Sub sasmacro1()
'
' sasmacro1 Macro
'
'
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.5)
.BottomMargin = InchesToPoints(0.5)
.LeftMargin = InchesToPoints(0.5)
.RightMargin = InchesToPoints(0.5)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With
If Selection.PageSetup.Orientation = wdOrientPortrait Then
Selection.PageSetup.Orientation = wdOrientLandscape
Else
Selection.PageSetup.Orientation = wdOrientPortrait
End If
Selection.WholeStory
Selection.Font.Name = "Courier New"
Selection.Font.Size = 8
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine

End Sub
 
Something like the following will do it for all occurrences of Report, without having to write your own loop:

Code:
[blue]    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    
    With Selection.Find
        .Text = "Report"
        .Replacement.Text = "^mReport"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll[/blue]
 
The macro you provided doesn't do what you think it does. All it does is set up the margins, makes certain the orientation is in landscape and make the font 8pt in Courier New.

In fact, to do what you want (i.e., add a page break every time the word "report" occurs) doesn't require a macro. Using Replace, put report in the Find Box and ^m^& in the Replace with box. This will put a page break before each occurrence of report.
 
Hmm.... It looks like the macro will change the orientation of the page to the opposite it was originally. If it is in Portrait it will change it to being in Landscape. If it's not in Portrait, it will change it to Portrait.
 
strongm, client says: the page break needs to come at the end of the last line of the previous page, instead of before the word “REPORT”.
 
>finds the word 'Report', then forces a new page at every occurrence
>the page break needs to come at the end of the last line of the previous page, instead of before the word “REPORT

So a change an apparent change requirements. Although I'm not actually sure I understand the distinction here.

If I have

This
is
a
report

and run the example code I'd get

This
is
a
<pagebreak>
report

Now - is the pagebreak after the last line of the previous page (which only becomes a previous page because we've inserted a pagebreak), or before the word report?
 
From what has been described, you don't need a macro for this - you could do it with a wildcard Find/Replace, where:
Find = (^13)(*Report[!^13]{1,})
Replace = \1^012\2
Note: The above Find expression assumes 'Report' is not the last word in its paragraph. If it is, you could delete '[!^13]{1,}' from the Find expression.

Of course, you could implement the above as a macro, too. I concur that, as posted, sasmacro1 is not "a macro in Word that finds the word 'Report', then forces a new page at every occurrence it finds".

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top