Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The bug tdf#33304 meanwhile was marked NOTABUG WONTFIX.
However some users still seem to have problems with adjusting the margins complying with their intention to place a header inside the area that was defined as the margin in advance of the insertion.

To ease those of them who can accept support by user code (supposed to get assigned to a sensitive area in the Format toolbar), I want to post a raw draft of a Sub doing the needed complicated calculations and setting the new margin based on the result.

Sub insertHeaderIfMissingAndAdjustMargin(Optional pDoc As Object, _
      Optional pPageStylePtr As Object, Optional pMinPrintMargin As Long, _
      Optional pHeaderBodyDistance As Long)
Const minHbd As Long = 200, minPm As Long = 400
If IsMissing(pDoc) Then pDoc = ThisComponent
If NOT pDoc.supportsService _
             ("com.sun.star.text.GenericTextDocument") Then Exit Sub
REM There may be lots of possible errors not catched by the code,
REM in specific the current selection as the default PageStylePointer
REM may not (directly) have a .PageStyleName property.
REM The CurrentSelection should be an ordinary position within the bodytext
REM on a page having set the page style which shall be processed. 
On Error Goto errorCase 
If IsMissing(pPageStylePtr) Then pPageStylePtr = pDoc.CurrentSelection(0)
If IsMissing(pMinPrintMargin) Then pMinPrintMargin = minPm
If IsMIssing(pHeaderBodyDistance) Then pHeaderBodyDistance = minHbd
If pMinPrintMargin<minPM Then pMinPrintMargin = minPm
If pHeaderBodyDistance<minHbd Then pHeaderBodyDistance = minHbd
pageStyles = pDoc.StyleFamilies.PageStyles
pgSName = pPageStylePtr.PageStyleName
pgS = pageStyles.getByName(pgSName)
If pgS.HeaderIsOn Then Exit Sub
pgS.HeaderIsOn = True
pgS.HeaderBodyDistance = pHeaderBodyDistance
With pgs
 marginCorrection = _
  .HeaderHeight + .HeaderTopBorderDistance + _
  .HeaderBottomBorderDistance + .HeaderBodyDistance
End With
newMargin = pgS.TopMargin - marginCorrection
If newMargin<pMinPrintMargin Then newMargin = pMinPrintMargin
pgS.TopMargin = newMargin
errorCase:
End Sub

The complicated calculations I was talking of you find here:

 marginCorrection = _
  .HeaderHeight + .HeaderTopBorderDistance + _
  .HeaderBottomBorderDistance + .HeaderBodyDistance

The sole actual problem is to know the true height of the header itself if .HeaderIsDynamicHeight is True. That's the property shown as AutoFit height in the English UI. You should probably better switch this property off by an additional line of code and define the .HeaderHeight explicitly meeting your needs.

The bug tdf#33304 meanwhile was marked NOTABUG WONTFIXRESOLVED NOTABUG.
However some users still seem to have problems with adjusting the margins complying with their intention to place a header inside the area that was defined as the margin in advance of the insertion.

To ease those of them who can accept support by user code (supposed to get assigned to a sensitive area in the Format toolbar), I want to post a raw draft of a Sub doing the needed complicated calculations and setting the new margin based on the result.

Sub insertHeaderIfMissingAndAdjustMargin(Optional pDoc As Object, _
      Optional pPageStylePtr As Object, Optional pMinPrintMargin As Long, _
      Optional pHeaderBodyDistance As Long)
Const minHbd As Long = 200, minPm As Long = 400
If IsMissing(pDoc) Then pDoc = ThisComponent
If NOT pDoc.supportsService _
             ("com.sun.star.text.GenericTextDocument") Then Exit Sub
REM There may be lots of possible errors not catched by the code,
REM in specific the current selection as the default PageStylePointer
REM may not (directly) have a .PageStyleName property.
REM The CurrentSelection should be an ordinary position within the bodytext
REM on a page having set the page style which shall be processed. 
On Error Goto errorCase 
If IsMissing(pPageStylePtr) Then pPageStylePtr = pDoc.CurrentSelection(0)
If IsMissing(pMinPrintMargin) Then pMinPrintMargin = minPm
If IsMIssing(pHeaderBodyDistance) Then pHeaderBodyDistance = minHbd
If pMinPrintMargin<minPM Then pMinPrintMargin = minPm
If pHeaderBodyDistance<minHbd Then pHeaderBodyDistance = minHbd
pageStyles = pDoc.StyleFamilies.PageStyles
pgSName = pPageStylePtr.PageStyleName
pgS = pageStyles.getByName(pgSName)
If pgS.HeaderIsOn Then Exit Sub
pgS.HeaderIsOn = True
pgS.HeaderBodyDistance = pHeaderBodyDistance
With pgs
 marginCorrection = _
  .HeaderHeight + .HeaderTopBorderDistance + _
  .HeaderBottomBorderDistance + .HeaderBodyDistance
End With
newMargin = pgS.TopMargin - marginCorrection
If newMargin<pMinPrintMargin Then newMargin = pMinPrintMargin
pgS.TopMargin = newMargin
errorCase:
End Sub

The complicated calculations I was talking of you find here:

 marginCorrection = _
  .HeaderHeight + .HeaderTopBorderDistance + _
  .HeaderBottomBorderDistance + .HeaderBodyDistance

The sole actual problem is to know the true height of the header itself if .HeaderIsDynamicHeight is True. That's the property shown as AutoFit height in the English UI. You should probably better switch this property off by an additional line of code and define the .HeaderHeight explicitly meeting your needs.