Share via

Word "Loop".

Anonymous
2023-12-11T23:31:11+00:00

Hello from Steve

I have up to 30 pages

Can the below please be able to "Loop".

Sub Australia_PageBreaks()

Dim i As Integer 

Dim searchText As String 

Dim found As Boolean 

found = False

For i = 1 To 3

    searchText = "<" & i & " " 

    Selection.MoveDown Unit:=wdLine, Count:=3 

    Selection.Find.ClearFormatting 

    With Selection.Find 

        .Text = searchText 

        .Replacement.Text = "" 

        .Forward = True 

        .Wrap = wdFindContinue 

        .Format = False 

        .MatchCase = False 

        .MatchWholeWord = False 

        .MatchAllWordForms = False 

        .MatchSoundsLike = False 

        .MatchWildcards = True 

    End With 

    found = Selection.Find.Execute 

    If found Then 

        Exit For 

    End If 

Next i 

If found Then

    Selection.HomeKey Unit:=wdLine 

    Selection.InsertBreak Type:=wdPageBreak 

    Selection.TypeParagraph 

End If 

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2023-12-12T03:24:54+00:00

Hello Steve8d,

I'm Shalom and I'd happily help you with your question. In this forum, we are Microsoft consumers just like yourself.

From your code, it seems like you’re trying to loop through a document and insert page breaks at certain points based on some criteria. However, your current loop only runs three times (For i = 1 To 3). If you want to loop through up to 30 pages, you should adjust this to For i = 1 To 30.

Here’s how you might modify your code:

Sub Australia_PageBreaks() Dim i As Integer Dim searchText As String Dim found As Boolean

For i = 1 To 30 searchText = "<" & i & " " found = False

    Selection.MoveDown Unit:=wdLine, Count:=3 
    Selection.Find.ClearFormatting 
    With Selection.Find 
        .Text = searchText 
        .Replacement.Text = "" 
        .Forward = True 
        .Wrap = wdFindContinue 
        .Format = False 
        .MatchCase = False 
        .MatchWholeWord = False 
        .MatchAllWordForms = False 
        .MatchSoundsLike = False 
        .MatchWildcards = True 
    End With 
    found = Selection.Find.Execute 

    If found Then 
        Selection.HomeKey Unit:=wdLine 
        Selection.InsertBreak Type:=wdPageBreak 
        Selection.TypeParagraph 
    End If 
Next i 

End Sub

This code will loop through each page in your document (up to 30 pages), and if it finds the specified text, it will insert a page break and a new paragraph at that location.

Please replace 30 with the actual number of pages you want to loop through.

Best Regards, Shalom

Give back to the community. Help the next person with this problem by indicating whether this answer solved your problem. Click Yes or No at the bottom.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-12-12T05:09:36+00:00

    Hello Shalom_409

    Sorry I should have explained what For i = 1 To 3 means.

    All my paragraphs are numbered.

    I have paragraph's that are numbered from 1 to 24.

    Some paragraphs might start at 2 to 24 or 3 to 24.

    For i = 1 To 3 means start at paragraph 1st, 2nd or 3 to prdut in the page breaks.

    At his time your script is great but it needs to find 1st, 2nd or 3rd paragraph to start the page breaks.

    Would it also please be possible to have a input box so that I can put in the required pages needed.

    I thank you.

    All the Best

    Steve

    Was this answer helpful?

    0 comments No comments