Different paragraph settings for custom defined style whenever it appears at the very top of a page

A

andreas

Dear Experts:

I got a certain, custom defined paragraph style with certain paragraph
settings, such as spacing before and after.

If a paragraph formatted with this custom style is located at the VERY
TOP of a page of a multi-page document then I would like to be able to
run a macro that automatically sets a different spacing for these
paragraphs. But the settings for the custom defined style must be left
untouched. The new paragraph settings have to be applied manually just
for these first paragraphs.

This may sound weird, but I hope this is feasible with Word VBA.

Help is appreciated. Thank you very much in advance.

Regards,

Andreas
 
B

Bear

Andreas:

I don't have any help, just some comments. From what I read, your
application is a problem in Word, as Word has little notion of pages. It can
be difficult to reliably detect when a given para is at the top of a page.

I'd also like to remind you that how Word handles the space above a
paragraph after a page break can be controlled to some extent by the
Customization settings in Tools > Options.

Finally, you might have some success by searching through all your
paragraphs, and when the style matches the target style, select that para and
examine:

Selection.Information (wdFirstCharacterLineNumber) = 1

Maybe that's a start. Maybe that's such a horrible approach that it will
goad the real code experts into providing an elegant solution we can all
enjoy.

Bear
 
H

Helmut Weber

Hi Andreas,

one could:

select one page of the doc after the other
select the first paragraph on that page
check whether it spans to a preceiding page
by getting Information(wdActiveEndPageNumber)
of the first character of that paragraph
compare it to Information(wdActiveEndPageNumber)
of the last character of that paragraph.

If not different, than that paragraph
is entirely on the page in question.

If it is entirely on that page and is the first
paragraph as well and the name of the paragraph style
is the right one, then is the kind of paragraph
you are looking for.

I'm well aware, that paragraphs may span
dozens of pages. This is not covered here.
But, for your question,
IMHO, I think, this is theory.
Could all be handled, nevertheless.

Have a look at this hardly tested code sample,
which you'd have to streamline quite a bit, I guess,
however, it may give an idea of what to do.

Sub Test33()
Dim lPgs As Long
Dim x As Long
lPgs = ActiveDocument.ComputeStatistics(wdStatisticPages)
With Selection
For x = 1 To lPgs
.GoTo what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=x
.Bookmarks("\page").Select
With .Paragraphs(1).Range
.Select
If
Selection.Characters.First.Information(wdActiveEndPageNumber) = _

Selection.Characters.Last.Information(wdActiveEndPageNumber) Then
MsgBox "Same Page"
Else
MsgBox "not same Page"
End If
End With
Next
End With
End Sub

You could also loop over all paragraphs
of your style, and apply the above check.

What method is faster depends on the structure
of your doc.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
L

Lene Fredborg

Below you will find an additional solution. My first solution was close to
what Helmut suggests (same principles) but then I thought that the macro
should also reset the spacing of paragraphs that were no longer in top of a
page (due to editing of the document). The macro below use Find. I may have
overlooked something but the macro worked in my tests. See also the comments
in the code.

The macro finds the style "MyStyle". If the paragraph found when looping is
in the top of the page, the spacing of that paragraph will be changed. If not
in top of a page, the spacing will be reset to the spacing defined in
"MyStyle" (this will "repair" wrong spacing due to editing of the document).

If you want to specify the space in e.g. centimeters instead of points, use
for example:
CentimetersToPoints(1.2) instead of just the number.


Sub ChangeSpace_FirstParaOnPage()

'Find all paragraphs with specified style
'If in top of page, set special space
'else reset space to style definition

Dim strStyleName As String
Dim oRange As Range

'Replace the name with the desired style name
strStyleName = "Heading 1"

Set oRange = ActiveDocument.Range

With oRange.Find
.Style = strStyleName
.Forward = True
.Wrap = wdFindStop

Do While .Execute(findtext:="", Format:=True)
With oRange
'If first paragraph on page, change spacing
'First: If a manual line break (Chr(12)) is found before the
paragraph,
'it is included in the selection - then exlude it
If .Characters(1) = Chr(12) Then 'manual line break
.Start = .Start + 1
End If
If .Characters(1).Information(wdFirstCharacterLineNumber) =
1 Then
'Change space - replace by the desired space
.ParagraphFormat.SpaceBefore = 30
.ParagraphFormat.SpaceAfter = 20
Else
'Reset space to style definition
.ParagraphFormat.SpaceBefore =
ActiveDocument.Styles(strStyleName).ParagraphFormat.SpaceBefore
.ParagraphFormat.SpaceAfter =
ActiveDocument.Styles(strStyleName).ParagraphFormat.SpaceAfter
End If
'Make oRange ready for next lool
oRange.Collapse wdCollapseEnd
'To prevent endless loop in case of last paragraph in document
If .End = ActiveDocument.Range.End - 1 Then Exit Do
End With
Loop

End With

Set oRange = Nothing
MsgBox "Finished."

End Sub

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

andreas

Below you will find an additional solution. My first solution was close to
what Helmut suggests (same principles) but then I thought that the macro
should also reset the spacing of paragraphs that were no longer in top of a
page (due to editing of the document). The macro below use Find. I may have
overlooked something but the macro worked in my tests. See also the comments
in the code.

The macro finds the style "MyStyle". If the paragraph found when looping is
in the top of the page, the spacing of that paragraph will be changed. If not
in top of a page, the spacing will be reset to the spacing defined in
"MyStyle" (this will "repair" wrong spacing due to editing of the document).

If you want to specify the space in e.g. centimeters instead of points, use
for example:
CentimetersToPoints(1.2) instead of just the number.

Sub ChangeSpace_FirstParaOnPage()

'Find all paragraphs with specified style
'If in top of page, set special space
'else reset space to style definition

Dim strStyleName As String
Dim oRange As Range

'Replace the name with the desired style name
strStyleName = "Heading 1"

Set oRange = ActiveDocument.Range

With oRange.Find
.Style = strStyleName
.Forward = True
.Wrap = wdFindStop

Do While .Execute(findtext:="", Format:=True)
With oRange
'If first paragraph on page, change spacing
'First: If a manual line break (Chr(12)) is found before the
paragraph,
'it is included in the selection - then exlude it
If .Characters(1) = Chr(12) Then 'manual line break
.Start = .Start + 1
End If
If .Characters(1).Information(wdFirstCharacterLineNumber) =
1 Then
'Change space - replace by the desired space
.ParagraphFormat.SpaceBefore = 30
.ParagraphFormat.SpaceAfter = 20
Else
'Reset space to style definition
.ParagraphFormat.SpaceBefore =
ActiveDocument.Styles(strStyleName).ParagraphFormat.SpaceBefore
.ParagraphFormat.SpaceAfter =
ActiveDocument.Styles(strStyleName).ParagraphFormat.SpaceAfter
End If
'Make oRange ready for next lool
oRange.Collapse wdCollapseEnd
'To prevent endless loop in case of last paragraph in document
If .End = ActiveDocument.Range.End - 1 Then Exit Do
End With
Loop

End With

Set oRange = Nothing
MsgBox "Finished."

End Sub

--
Regards
Lene Fredborg
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word









- Zitierten Text anzeigen -

Bear, Helmut and Lene,

great support from all of you. I will give all the suggestions a try
in the coming days and then will let you know.
It is really incredibly how helpful you and all the other experts
are. I am really enjoying this support.

Regards, Andreas
 
A

andreas

Below you will find an additional solution. My first solution was close to
what Helmut suggests (same principles) but then I thought that the macro
should also reset the spacing of paragraphs that were no longer in top of a
page (due to editing of the document). The macro below use Find. I may have
overlooked something but the macro worked in my tests. See also the comments
in the code.

The macro finds the style "MyStyle". If the paragraph found when looping is
in the top of the page, the spacing of that paragraph will be changed. If not
in top of a page, the spacing will be reset to the spacing defined in
"MyStyle" (this will "repair" wrong spacing due to editing of the document).

If you want to specify the space in e.g. centimeters instead of points, use
for example:
CentimetersToPoints(1.2) instead of just the number.

Sub ChangeSpace_FirstParaOnPage()

'Find all paragraphs with specified style
'If in top of page, set special space
'else reset space to style definition

Dim strStyleName As String
Dim oRange As Range

'Replace the name with the desired style name
strStyleName = "Heading 1"

Set oRange = ActiveDocument.Range

With oRange.Find
.Style = strStyleName
.Forward = True
.Wrap = wdFindStop

Do While .Execute(findtext:="", Format:=True)
With oRange
'If first paragraph on page, change spacing
'First: If a manual line break (Chr(12)) is found before the
paragraph,
'it is included in the selection - then exlude it
If .Characters(1) = Chr(12) Then 'manual line break
.Start = .Start + 1
End If
If .Characters(1).Information(wdFirstCharacterLineNumber) =
1 Then
'Change space - replace by the desired space
.ParagraphFormat.SpaceBefore = 30
.ParagraphFormat.SpaceAfter = 20
Else
'Reset space to style definition
.ParagraphFormat.SpaceBefore =
ActiveDocument.Styles(strStyleName).ParagraphFormat.SpaceBefore
.ParagraphFormat.SpaceAfter =
ActiveDocument.Styles(strStyleName).ParagraphFormat.SpaceAfter
End If
'Make oRange ready for next lool
oRange.Collapse wdCollapseEnd
'To prevent endless loop in case of last paragraph in document
If .End = ActiveDocument.Range.End - 1 Then Exit Do
End With
Loop

End With

Set oRange = Nothing
MsgBox "Finished."

End Sub

--
Regards
Lene Fredborg
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word









- Zitierten Text anzeigen -

Dear Lene,

I tried it out several times. Your code works perfect. Thank you very
much. It is a very nice peace of work. Real great job!
 
A

andreas

Hi Andreas,

one could:

select one page of the doc after the other
select the first paragraph on that page
check whether it spans to a preceiding page
by getting Information(wdActiveEndPageNumber)
of the first character of that paragraph
compare it to Information(wdActiveEndPageNumber)
of the last character of that paragraph.

If not different, than that paragraph
is entirely on the page in question.

If it is entirely on that page and is the first
paragraph as well and the name of the paragraph style
is the right one, then is the kind of paragraph
you are looking for.

I'm well aware, that paragraphs may span
dozens of pages. This is not covered here.
But, for your question,
IMHO, I think, this is theory.
Could all be handled, nevertheless.

Have a look at this hardly tested code sample,
which you'd have to streamline quite a bit, I guess,
however, it may give an idea of what to do.

Sub Test33()
Dim lPgs As Long
Dim x As Long
lPgs = ActiveDocument.ComputeStatistics(wdStatisticPages)
With Selection
For x = 1 To lPgs
.GoTo what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=x
.Bookmarks("\page").Select
With .Paragraphs(1).Range
.Select
If
Selection.Characters.First.Information(wdActiveEndPageNumber) = _

Selection.Characters.Last.Information(wdActiveEndPageNumber) Then
MsgBox "Same Page"
Else
MsgBox "not same Page"
End If
End With
Next
End With
End Sub

You could also loop over all paragraphs
of your style, and apply the above check.

What method is faster depends on the structure
of your doc.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Dear Helmut,

Lene provided me with a fully functional code. It is running fine.
Very good job of hers. Nevertheless thank you for giving me more
insight into Word Macro Coding with your suggestions which have always
been superb in this forum.

Regards, Andreas
 
L

Lene Fredborg

Thank you for the feedback. I am glad I could help you.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top