よろずやネット

最近はLibreofficeにハマっています。

LibreOffice Calc、印刷時のヘッダー・フッターを設定する

下記サンプルでは、ヘッダー左側に「いろは商店」という架空商店の名前と電話番号、ヘッダー右側にページ番号とページ総数を挿入する。

Option Explicit

Global Const fontName As String = "Tahoma"
Global Const fontSize As Single = 9
Global Const fontName2 As String = "Times New Roman"
Global Const fontSize2 As Single = 9

Sub setHeaderFooter

	Dim oDoc As Object: oDoc = ThisComponent
	Dim oSheetStyle As Object
	oSheetStyle = oDoc.StyleFamilies.getByName("PageStyles").getByName("Default")	

	' Header Height ヘッダー高さ, ヘッダー間隔を含む 1/100mm
	oSheetStyle.HeaderHeight = 900

	' Header Spacing ヘッダー間隔、ヘッダーとコンテンツの間隔 1/100mm
	oSheetStyle.HeaderBodyDistance = 50

	' Disable Autofit Height, 高さ自動調整オフ
	oSheetStyle.HeaderDynamic= False

	Dim oFooter As Object
	Dim oHeader As Object

	' ----- clear Footer
	oFooter = oSheetStyle.RightPageFooterContent
	oFooter.RightText.String = ""
	oFooter.CenterText.String = ""
	oFooter.LeftText.String = ""

	' ----- clear Header
	oHeader = oSheetStyle.RightPageHeaderContent
	oHeader.RightText.String = ""
	oHeader.CenterText.String = ""
	oHeader.LeftText.String = ""

	' ----- get TextField
	Dim oPageNumber As Object
	oPageNumber = oDoc.createInstance("com.sun.star.text.TextField.PageNumber")

	Dim oPageCount As Object
	oPageCount = oDoc.createInstance ("com.sun.star.text.TextField.PageCount")

	' ----- get TextCursor
	Dim oTextCursor As Object

	' ----- Header Right
	oTextCursor = oHeader.RightText.createTextCursor

	oTextCursor.gotoEnd (False)
	oTextCursor.setPropertyValue("CharFontName", "Tahoma")
	oTextCursor.setPropertyValue("CharHeight", 8)
	oTextCursor.gotoEnd (False)
	oTextCursor.String = "Page "

	oTextCursor.gotoEnd (False)
	oHeader.RightText.insertTextContent (oTextCursor, oPageNumber, True)
	oTextCursor.gotoEnd (False)
	oTextCursor.String = "/"
	oTextCursor.gotoEnd (False)
	oHeader.RightText.insertTextContent(oTextCursor, oPageCount, True)

	' ----- Header Left
	oTextCursor = oHeader.LeftText.createTextCursor

	oTextCursor.gotoEnd (False)
	oTextCursor.setPropertyValue("CharFontName", "Tahoma")
	oTextCursor.setPropertyValue("CharHeight", 8)
	oTextCursor.gotoEnd (False)
	oTextCursor.String = "いろは商店" & Chr(10) & "電話番号: "

	oTextCursor.gotoEnd (False)
	oTextCursor.setPropertyValue("CharFontName", "Tahoma")
	oTextCursor.setPropertyValue("CharHeight", 10)
	oTextCursor.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.BOLD)

	oTextCursor.gotoEnd (False)
	oTextCursor.String = "555-0100"

	' ----- Style 適用
	oSheetStyle.RightPageFooterContent = oFooter
	oSheetStyle.RightPageHeaderContent = oHeader

End Sub