用紙切り替え処理-リスト3
ページ設定を実行するのは、2つのSubプロシージャです。1つは、用紙の縦横方向と左右余白(マージン)の設定を行うPrintSetで、もう1つは左右方向を1ページ以内に収める設定を行うFitPageです。
まず、PrintSetプロシージャを作成します。
- このプロシージャは、3つの引数を持たせます。ページ設定のタイプを指定する
PrintTypeと、左右の余白値を設定するNum、表のタイトル文字を設定するTitleです。 - ワークシートのページ設定は、
PageSetupオブジェクトが担当します。このオブジェクトは、ページ設定に関するさまざまなプロパティを持っており、これらを使ってコードからページ設定を操作できます。 - 上下左右の余白(マージン)は、
LeftMargin、RightMargin、TopMargin、BottomMarginプロパティで設定します。値はポイント単位で指定します。ここでは左右の余白値をプロシージャの引数Numでセットしますが、プロシージャを呼び出す側ではcmで余白値をセットするため、CentimetersToPointsメソッドを使用してcmからポイントに換算してからセットします。 - ヘッダー・フッター用のマージン設定は、
HeaderMargin、FooterMarginプロパティを使用します。これは、上下の余白内に設定される領域です。ここでは、あらかじめ「1.5」と「0」にセットしておきますが、計算しやすいように値をcmで指定し、CentimetersToPointsメソッドでポイントに換算してからプロパティに設定しています。 - ヘッダーを設定するには、3つのプロパティを使用します。
LeftHeader、CenterHeader、RightHeaderプロパティで、用紙内にヘッダーを表示する位置によって使い分けます。
Sub PrintSet(ByVal PrintType As String, ByVal Num As Integer, _ ByVal Title As String)
PrintTypeの設定値を使い、Select Caseステートメントで処理を分岐します。プロパティPaperSizeは、用紙サイズを設定します。引数は定数値で別表1の値を使うことでいろいろな用紙に切り替えることができます(ただし、使用できる用紙サイズはプリンタによって制限される場合があります)。Orientationプロパティは用紙の縦横を決めるプロパティで、「xlPortrait(縦)」「xlLandscape(横)」のどちらかを設定します。With Worksheets("Sheet1").PageSetup Select Case PrintType Case "A4縦" .PaperSize = xlPaperA4 .Orientation = xlPortrait Case "A4横" .PaperSize = xlPaperA4 .Orientation = xlLandscape End Select
.LeftMargin = Application.CentimetersToPoints(Num) .RightMargin = Application.CentimetersToPoints(Num) .TopMargin = Application.CentimetersToPoints(2) .BottomMargin = Application.CentimetersToPoints(1)
.HeaderMargin = Application.CentimetersToPoints(1.5) .FooterMargin = Application.CentimetersToPoints(0)
Titleから受け取った表のタイトル文字を左側のヘッダーに、日付・時刻を右側のヘッダー領域にセットしています。
.LeftHeader = Title
.RightHeader = "&D&T"
End With
End Sub
'用紙切り替え用印刷処理 Sub PrintSet(ByVal PrintType As String, ByVal Num As Integer, _ ByVal Title As String) With Worksheets("Sheet1").PageSetup Select Case PrintType Case "A4縦" .PaperSize = xlPaperA4 .Orientation = xlPortrait Case "A4横" .PaperSize = xlPaperA4 .Orientation = xlLandscape End Select '左右のマージンを可変にしておく .LeftMargin = Application.CentimetersToPoints(Num) .RightMargin = Application.CentimetersToPoints(Num) .TopMargin = Application.CentimetersToPoints(2) .BottomMargin = Application.CentimetersToPoints(1) .HeaderMargin = Application.CentimetersToPoints(1.5) .FooterMargin = Application.CentimetersToPoints(0) 'ヘッダーの設定 .LeftHeader = Title .RightHeader = "&D&T" End With End Sub
| 書式コード | 内容 |
| &L | このコードに続く文字列を左詰めに配置します。 |
| &C | このコードに続く文字列を中央揃えに配置します。 |
| &R | このコードに続く文字列を右詰めに配置します。 |
| &E | 文字列を二重下線付きで印刷します。 |
| &X | 上付き文字を印刷します。 |
| &Y | 下付き文字を印刷します。 |
| &B | 文字列を太字で印刷します。 |
| &I | 文字列を斜体で印刷します。 |
| &U | 文字列を下線付きで印刷します。 |
| &S | 文字列を取り消し線付きで印刷します。 |
| &D | 現在の日付を印刷します。 |
| &T | 現在の時刻を印刷します。 |
| &F | ファイルの名前を印刷します。 |
| &A | シート見出し名を印刷します。 |
| &P | ページ番号を印刷します。 |
| &P+ <数値> | ページ番号に指定した<数値>を加えた値を印刷します。 |
| &P- <数値> | ページ番号から指定した<数値>を引いた値を印刷します。 |
| && | アンパサンド(&)を1つ印刷します。 |
| & " <フォント名> " | 指定したフォントで文字を印刷します。フォント名は、必ず半角の二重引用符(")で囲んでください。 |
| &nn | 指定したフォントサイズで文字を印刷します。nnには、ポイント数を表す2桁の数値を指定します。 |
| &N | ファイルのすべてのページ数を印刷します。 |
1枚の用紙に収めて印刷する処理と罫線操作-リスト4
もう1つ、左右方向を1ページ以内に収める設定を行うFitPageを作成します。
- このプロシージャは、引数を1つだけ持ちます。表のタイトル文字を格納する
Titleです。 - 違うのは、用紙の左右方向にデータを全部納めてしまうことです。この処理は、
PageSetupオブジェクトのFitToPagesWideを使用します。このプロパティは、収めたいページ数を指定すると、自動的にそこにデータが収まるように縮小印刷を設定してくれます。ここでは、1ページに収めたいので、「1」をセットします。 - 表に罫線を設定する場合は、
Bordersコレクションオブジェクトを使用し、セルの四つの枠1つ1つに罫線を設定する操作をします。 - これで、コードからページ設定を行う処理が出来上がりました。
PrintSetと同じです。Sub FitPage(ByVal Title As String) With Worksheets("Sheet1").PageSetup .PaperSize = xlPaperA4 .Orientation = xlLandscape .LeftMargin = Application.CentimetersToPoints(1) .RightMargin = Application.CentimetersToPoints(1) .TopMargin = Application.CentimetersToPoints(2) .BottomMargin = Application.CentimetersToPoints(1) .HeaderMargin = Application.CentimetersToPoints(1.5) .FooterMargin = Application.CentimetersToPoints(0) .LeftHeader = Title .RightHeader = "&D&T"
FitToPagesTallプロパティを使用すると、縦方向を指定するページ内に収めるような操作ができます。
.Zoom = False '戻すには100をセット
.FitToPagesWide = 1
'.FitToPagesTall = 1
End With
End Sub
Bordersプロパティに罫線を設定したいセルの枠位置を定数で指定します。そして、LineStyleプロパティで線種を、Weightで線の太さをColorIndexで線の色を指定します。Bordersプロパティの設定値「xlInsideVertical」は、囲まれた枠線を表す値で、ここではセル範囲全体の外枠が太い線に、セル範囲の内側が細い線になるように各プロパティを設定しています。Sub 罫線設定(ByVal RowNo As Integer, ByVal ColumnNo As Integer) Range(Cells(1, 1), Cells(RowNo, ColumnNo)).Select '外側を太く With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With ' 内側を細く With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With Range("A1").Select End Sub
'データを1枚の用紙に収めて印刷する場合 Sub FitPage(ByVal Title As String) With Worksheets("Sheet1").PageSetup .PaperSize = xlPaperA4 .Orientation = xlLandscape .LeftMargin = Application.CentimetersToPoints(1) .RightMargin = Application.CentimetersToPoints(1) .TopMargin = Application.CentimetersToPoints(2) .BottomMargin = Application.CentimetersToPoints(1) .HeaderMargin = Application.CentimetersToPoints(1.5) .FooterMargin = Application.CentimetersToPoints(0) .LeftHeader = Title .RightHeader = "&D&T" '印刷倍率を無効にし収めるページ数を指定 .Zoom = False '戻すには100をセット .FitToPagesWide = 1 '.FitToPagesTall = 1 End With End Sub Sub 罫線設定(ByVal RowNo As Integer, ByVal ColumnNo As Integer) Range(Cells(1, 1), Cells(RowNo, ColumnNo)).Select '外側を太く With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With ' 内側を細く With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With Range("A1").Select End Sub
まとめ
Excelのページ設定と印刷設定機能を組み合わせ、印刷処理を自動化してみました。
RangeオブジェクトのWidth、Heightプロパティを使うことで、セルのサイズをミリやセンチメートルという単位で把握できます。そして、データの入力されているセル範囲をこれを把握すれば、1ページに収まるのかどうか、何ページになるのかなどを計算することもできます。
