![]() |
|
|
|
#1
|
|||
|
|||
|
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! |
|
#2
|
||||
|
||||
|
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] |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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] |
|
#5
|
|||
|
|||
|
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
|
|
#6
|
|||
|
|||
|
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
|
|
#7
|
|||
|
|||
|
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)
|
|
#8
|
||||
|
||||
|
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] |
|
#9
|
|||
|
|||
|
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.
|
|
#10
|
||||
|
||||
|
Quote:
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] |
|
#11
|
|||
|
|||
|
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 |
|
#12
|
||||
|
||||
|
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] |
|
#13
|
|||
|
|||
|
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. |
|
#14
|
||||
|
||||
|
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] |
|
#15
|
|||
|
|||
|
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! |
|
| Tags |
| page breaks |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Avoid continous breaks turn into page breaks
|
sponge | Word | 3 | 09-14-2017 12:11 PM |
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 |