INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to make this Word macro loop?

How to make this Word macro loop?

(OP)
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

RE: How to make this Word macro loop?

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

CODE

    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 

RE: How to make this Word macro loop?

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.

RE: How to make this Word macro loop?

>makes certain the orientation is in landscape

Want a bet?

RE: How to make this Word macro loop?

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.

RE: How to make this Word macro loop?

(OP)
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”.

RE: How to make this Word macro loop?

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

RE: How to make this Word macro loop?

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]

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close