Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-03-2025, 03:18 AM
ADAL ADAL is offline How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2021
Novice
How to Detect Specific Page Breaks in Word VBA
 
Join Date: Dec 2023
Posts: 26
ADAL is on a distinguished road
Default How to Detect Specific Page Breaks in Word VBA

Hello everyone,

I'm working on a Word VBA project and need to create a macro to identify if a specific page contains any type of page break (e.g., PageBreak, SectionBreakNextPage, SectionBreakOddPage, or SectionBreakEvenPage), excluding SectionBreakContinuous.

The issue is that using Text = Chr(12) does not differentiate between the various types of page breaks or section breaks.

Could someone please help me with an approach or code snippet to specifically detect the different types of page breaks and section breaks on a particular page?



Thank you in advance for your assistance!
Reply With Quote
  #2  
Old 01-03-2025, 05:20 AM
macropod's Avatar
macropod macropod is online now How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For example:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim s As Long, Rng As Range
With ActiveDocument
  For s = .Sections.Count To 1 Step -1
    With .Sections(s)
      Set Rng = .Range: Rng.End = Rng.End - 1
      Rng.Find.Execute FindText:=Chr(12), Replacewith:="", Forward:=True, Wrap:=wdFindStop, Replace:=wdReplaceAll
      If s = 1 Then Exit Sub
      If .PageSetup.SectionStart <> wdSectionContinuous Then .Range.Characters.Last.Delete
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-03-2025, 05:31 AM
ADAL ADAL is offline How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2021
Novice
How to Detect Specific Page Breaks in Word VBA
 
Join Date: Dec 2023
Posts: 26
ADAL is on a distinguished road
Default

Thanks, but I don't understand how that could help me in my case, maybe I didn't explain myself well, I want in the pages that there is a pagebreak of any type, that x is done and if there is no pagebreak of any type that y is done (even if there is a continuous sectionbreak on the page)
I tried this
Code:
Set rngPage= ActiveDocument.Bookmarks("\page").Range
If Len(rngPage.Text) - Len(Replace(rngPage.Text, Chr(12), "")) > 0 Then

but it also detects the continuous sectionbreaks
Reply With Quote
  #4  
Old 01-03-2025, 05:37 AM
macropod's Avatar
macropod macropod is online now How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

What do you want to do with the breaks when you find them? The code I posted shows how you might delete all except Continuous Section Breaks (I mis-read your 'detect' as 'delete'). There's all manner of other things you could do, but you need to give more detail. What do you want to do each kind of break or the ranges preceding them?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 01-03-2025, 11:46 AM
vivka vivka is offline How to Detect Specific Page Breaks in Word VBA Windows 7 64bit How to Detect Specific Page Breaks in Word VBA Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Hi, ADAL! Because of my scarce knowledge of VBA, I would use the following workaround to find cont. section breaks in a selected range:
Code:
Sub Find_ContSectionBrks()

Dim Rng As range
    Application.ScreenUpdating = False
    Set Rng = Selection.range
    With Rng.Find
        .ClearFormatting
       .Replacement.ClearFormatting
       .text = "^12"
       .Forward = True
       .Wrap = wdFindStop
       .MatchWildcards = True
       While .Execute
          If Rng.Characters.Last.Information(wdActiveEndPageNumber) = _
            Rng.Characters.Last.Next.Information(wdActiveEndPageNumber) Then
             Rng.Select
             Rng.InsertBefore "ContBr" 
             Selection.range.HighlightColorIndex = wdBrightGreen
             Rng.Collapse wdCollapseEnd
          End If
       Wend
    End With
    Application.ScreenUpdating = True
    Set Rng = Nothing
End Sub
You can add your 'do code' after 'Then' and delete three next lines until 'Rng.Collapse wdCollapseEnd'.
Reply With Quote
  #6  
Old 01-05-2025, 02:07 PM
gmaxey gmaxey is offline How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,599
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Perhaps something like this:


Code:
Option Explicit
Sub YourLargerMacro()
  If fcnXorY Then
    MsgBox "Do x"
  Else
    MsgBox "Do y"
  End If
lbl_Exit:
  Exit Sub
End Sub
Function fcnXorY() As Boolean
'A basic Word Macro coded by Gregory K. Maxey
Dim oRngStart As Range, oRng As Range
Dim bDo_x As Boolean
  bDo_x = False
  Set oRngStart = Selection.Range
  Set oRng = ActiveDocument.Bookmarks("\Page").Range
  With oRng.Find
    .Text = "^m"
    If .Execute Then
      bDo_x = True
    Else
      With oRng.Find
        .Text = "^b"
        Do While .Execute
          oRng.Collapse wdCollapseEnd
          oRng.Select
          If Selection.Sections(1).PageSetup.SectionStart <> wdSectionContinuous Then
            bDo_x = True
            Exit Do
          End If
        Loop
      End With
    End If
  End With
  oRngStart.Select
lbl_Exit:
  Exit Function
End Function
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 01-07-2025, 11:15 AM
ADAL ADAL is offline How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2021
Novice
How to Detect Specific Page Breaks in Word VBA
 
Join Date: Dec 2023
Posts: 26
ADAL is on a distinguished road
Default

Thank you very much, i'm going to try it, for now i realized that actually something very simple is enough for me, i just check if the last character on the page is a ^12 then it is almost certainly because it is a pagebreak, (although it may be that a continuessectionbreak came out by coincidence at the end of the page, for now this is enough for me)
Reply With Quote
  #8  
Old 01-07-2025, 01:17 PM
macropod's Avatar
macropod macropod is online now How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

What you could do is loop through the Sections instead of through the pages. Any Chr(12) occurring within a Section can only be a page break.

Conversely, if you loop through the pages, any Section break within a page must be a continuous Section break.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 01-07-2025, 02:44 PM
ADAL ADAL is offline How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2021
Novice
How to Detect Specific Page Breaks in Word VBA
 
Join Date: Dec 2023
Posts: 26
ADAL is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
What you could do is loop through the Sections instead of through the pages. Any Chr(12) occurring within a Section can only be a page break.

Conversely, if you loop through the pages, any Section break within a page must be a continuous Section break.
I am not sure what are you meaning. i need to loop pages, because i am making a macro to make all the pages to end at the same height (as know, it is a trouble in ms word), except for the pages they end in a page break.
Reply With Quote
  #10  
Old 01-07-2025, 03:50 PM
macropod's Avatar
macropod macropod is online now How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by ADAL View Post
I am not sure what are you meaning. i need to loop pages, because i am making a macro to make all the pages to end at the same height (as know, it is a trouble in ms word), except for the pages they end in a page break.
Unless you're changing the page setup via Section breaks, all pages in a Word document have the same height. And even if you're using Section breaks, each Section can be formatted so the pages have the same height.

So it's not apparent to me what you think you'll achieve via a macro that inserts page breaks other than complicating any future edits to the document. Indeed, what you risk is having page breaks splitting paragraphs and, when you switch to a different printer, having the page breaks being pushed to the tops of the next pages, resulting in empty pages in your document.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 01-08-2025, 02:12 PM
ADAL ADAL is offline How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2021
Novice
How to Detect Specific Page Breaks in Word VBA
 
Join Date: Dec 2023
Posts: 26
ADAL is on a distinguished road
Default

Thank you very much, but I didn't mean that the pages are not the same size, I meant that the text within the pages doesn't always end at the same height (this is a known problem)
LIKE IN THE ATACHMENT
Attached Images
File Type: png 1111.png (137.4 KB, 23 views)
Reply With Quote
  #12  
Old 01-08-2025, 02:23 PM
macropod's Avatar
macropod macropod is online now How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Messing around with page breaks isn't going to help with that. At best you'll end up splitting paragraphs where there's an automatic page break - which could thoroughly mess up your document formatting - and still won't balance the pages, plus you'll make the document a nightmare for anyone to make subsequent edits. On top of that, merely opening the document on a computer using a different printer driver is liable to result in the page breaks being in the wrong places, including at the top of the next page - causing your document to have blank pages.

Some of your 'issue' is the result of you having Word's widow/orphan control applied to the paragraphs in question. Turning widow/orphan control off on the last paragraph of the first page in your screenshot will add an extra line of text to the first page but will leave the last line of that paragraph orphaned at the top of the next page.

Word is a word-processor, not a page layout program. If you want that level of control, use the appropriate software - but then you'll lose Word's word-processing flexibility.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 01-08-2025, 03:00 PM
ADAL ADAL is offline How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2021
Novice
How to Detect Specific Page Breaks in Word VBA
 
Join Date: Dec 2023
Posts: 26
ADAL is on a distinguished road
Default

I know, I probably didn't explain myself well enough, I'm not interested in touching anything at all with section breaks, or page breaks, what the macro does is just spread the space that remains at the bottom between the paragraphs on the page (in a way that is barely noticeable), I work in book design, and this is not a new issue, this problem also exists in InDesign, and there are all kinds of plugins that try to solve the problem in different ways.
And my question is because on pages that have a page break there, then there is no need to arrange them at all, because there it is justified that a lot of empty space remains, and then my macro is supposed to skip these pages.
Reply With Quote
  #14  
Old 01-08-2025, 06:05 PM
macropod's Avatar
macropod macropod is online now How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

So how do you propose to make those adjustments - by:
• manipulating the paragraph before/after spacing; or
• manipulating the paragraph line heights,
or both?

Do be aware that whatever you do to the first and last paragraphs on a page could affect the previous/next page if those paragraphs span a page break. Changing the line heights will certainly do that, whilst changing the before spacing will affect the previous page and changing the after spacing will affect the next page.

You may also be interested in minimizing paragraph last-line wrapping as part of your approach. For that, see: https://www.msofficeforums.com/word-...ngle-line.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 01-09-2025, 11:17 AM
ADAL ADAL is offline How to Detect Specific Page Breaks in Word VBA Windows 10 How to Detect Specific Page Breaks in Word VBA Office 2021
Novice
How to Detect Specific Page Breaks in Word VBA
 
Join Date: Dec 2023
Posts: 26
ADAL is on a distinguished road
Default

macropod Thank you very much for your code, i've been thinking about that, and i think it can help me a lot.
Regarding your question, mainly what i do is manipulate the spaces between paragraphs, and part of the macro is that in the first paragraph i don't change the spaces before the paragraph, only the ones before, and in the last paragraph the other way around.
This code is mainly important in documents with 2 columns, where the 2 columns must end at the same height.
I’d like to ask you a question, but I’m not sure if this is the appropriate forum, or if i should open a new post, or if i should ask you in private (if is there any way to do this).
I know you’re a highly skilled VBA developer (and also gmaxey) — I can tell from the excellent code you share in the forum. That’s why I thought of reaching out to you (if you feel comfortable answering, of course).
I wanted to ask if you make a living from this skill. Recently, I’ve been learning VBA for Word seriously, and I’d like to hear your ideas about how to monetize it. In the country where I live, it’s a very rare skill, but I’m still unsure how to turn it into a source of income.
If you could share any advice or insights from your experience, I’d deeply appreciate it.
Thank you so much for your time and willingness to help!
Reply With Quote
Reply

Tags
page breaks



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Detect Specific Page Breaks in Word VBA 2 frustrating bugs in Word 2019 draft view - page number in status bar and invisible page breaks sidr Word 19 01-15-2025 03:23 PM
MS Word - can I automate page breaks after every 150 words??? ninose Word 4 09-05-2020 12:13 PM
How to Detect Specific Page Breaks in Word VBA Avoid continous breaks turn into page breaks sponge Word 3 09-14-2017 12:11 PM
How to Detect Specific Page Breaks in Word VBA Hidden page breaks and section breaks jrasicmark Word 3 06-02-2014 11:28 PM
Deleted Section Breaks Changes Page Breaks Brantnshellie Word 0 02-01-2009 09:22 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft