Excel VBA 面倒な印刷設定を高速自動化

スポンサーリンク

PrintOut【プリントアウト】メソッド

印刷を実行するにはPrintOutメソッドを使います。このPrintOut【プリントアウト】メソッドはWorksheet【ワークシート】オブジェクトだけでなくWookbook【ワークブック】オブジェクトでも使用可能です。

Workbook全体を印刷するコード

Sub sample()
    ActiveWorkbook.PrintOut
End sub

Worksheetを印刷するコード

Sub sample()
    Sheets("Sheet1").PrintOut
End Sub

印刷する前にプレビューを表示する

PrintOut【プリントアウト】メソッドの引数Preview【プレビュー】にTrueを指定すると、印刷前にプレビューが表示されます。プレビュー画面で印刷ボタンを押さないと印刷はされません。PrintPreview【プリントプレビュー】メソッドを使う方法もあります。

印刷する前にプレビュー表示するコード

Sub sample()
   Sheets("sheet1").PrintOut Preview:=True
End Sub

PrintPreviewの場合

Sub sample()
   ActiveSheet.PrintPreview
End Sub

用紙の向きを設定する

印刷する用紙の向きは、PageSetup【ページセットアップ】オブジェクトのOrientation【オリエンテーション】プロパティで設定します。
Orientationプロパティの定数は

  • xlLandscape(ランドスケープ) 横向き
  • xlPortrait(ポートレート)   縦向き

の2種類です。

用紙の向きを縦に設定したコード

Sub sample()
  With ActiveSheet
       .PageSetup.Orientation = xlPortrait
       .PrintPreview
  End With
End Sub

余白を設定する

余白を設定するには、PageSetupオブジェクトのMargin【マージン】プロパティで設定します。Marginプロパティの設定値はptポイントでわかりづらいのでApplication【アプリケーション】オブジェクトのCentimetersToPoints【センチメーターズトウポインツ】メソッドを使用しでセンチ単位に変換して指定します。
Marginの位置は

  • LeftMargin 【左余白の設定】
  • RightMargin 【右余白の設定】
  • TopMargin 【上余白の設定】
  • BottomMargin 【下余白の設定】
  • HeaderMargin 【ヘッダー余白の設定】
  • FooterMargin 【フッター余白の設定】

余白設定をした印刷設定のコード

Sub sample()
  With ActiveSheet.PageSetup
       .Orientation = xlPortrait
       .LeftMargin = Application.CentimetersToPoints(1)
       .RightMargin = Application.CentimetersToPoints(1)
       .TopMargin = Application.CentimetersToPoints(1.5)
       .BottomMargin = Application.CentimetersToPoints(1.5)
       .HeaderMargin = Application.CentimetersToPoints(0.5)
       .FooterMargin = Application.CentimetersToPoints(0.5)
  End With 
   ActiveSheet.PrintPreview
End Sub

余白を左右1cm 上下1.5cm フッターヘッター0.5cmに設定したコードです。

用紙の中央に印刷する方法

用紙の中央に印刷するには、PageSetupオブジェクトのCenterHorizontally【センターホリゾンタル】プロパティ(水平)CenterVertically(センターバーティカリー)プロパティ (垂直)にTrueを設定します。

用紙の中央に印刷する設定をしたコード

Sub sample()
  With ActiveSheet.PageSetup
       .Orientation = xlPortrait
       .LeftMargin = Application.CentimetersToPoints(1)
       .RightMargin = Application.CentimetersToPoints(1)
       .TopMargin = Application.CentimetersToPoints(1.5)
       .BottomMargin = Application.CentimetersToPoints(1.5)
       .HeaderMargin = Application.CentimetersToPoints(0.5)
       .FooterMargin = Application.CentimetersToPoints(0.5)
       .CenterHorizontally = True
       .CenterVertically = True
  End With 
   ActiveSheet.PrintPreview
End Sub

1枚の用紙に強制的に収めて印刷する

印刷する範囲の大きさによらす、1枚の用紙に収めて印刷するには、PageSetupオブジェクトのFitToPagesWide(フィットトウページズワイド)プロパティ(幅)FitTOpagesTall(フィットトウページズトール)プロパティ(高さ)に1を設定します。収まらないときは自動的に縮小して1枚の用紙に収めます。

1枚の用紙に強制的に収めて印刷するコードを追加したコード

Sub sample()
  With ActiveSheet.PageSetup
       .Orientation = xlPortrait
       .LeftMargin = Application.CentimetersToPoints(1)
       .RightMargin = Application.CentimetersToPoints(1)
       .TopMargin = Application.CentimetersToPoints(1.5)
       .BottomMargin = Application.CentimetersToPoints(1.5)
       .HeaderMargin = Application.CentimetersToPoints(0.5)
       .FooterMargin = Application.CentimetersToPoints(0.5)
       .CenterHorizontally = True
       .CenterVertically = True
       .FitToPagesWide = 1
       .FitTOpagesTall = 1
  End With 
   ActiveSheet.PrintPreview
End Sub

印刷の設定を高速化する

上記のコードでもわかるように、印刷各種設定で操作するPageSetupオブジェクトは、設定に時間がかかるオブジェクトです。そんなときは、Excelとプリンターとの通信を一時的に遮断します。遮断状態では、PageSetupオブジェクトの設定が高速に行われるので、設定を終えてからプリンターとの通信を再開し、キャッシュされた設定をプリンターに送信します。
Application【アプリケーション】オブジェクトのPrintCommunication【プリントコミュニケーション】プロパティを使用します。
なお、このプロパティはExcel 2010から追加されたプロパティです。2010以前は使用できません。設定の最初に Falseで遮断して、プレビューの前にTrueで通信を再開します。

印刷高速化を追加したコード

Sub sample()
  Application.PrintCommunication = False
  With ActiveSheet.PageSetup
       .Orientation = xlPortrait
       .LeftMargin = Application.CentimetersToPoints(1)
       .RightMargin = Application.CentimetersToPoints(1)
       .TopMargin = Application.CentimetersToPoints(1.5)
       .BottomMargin = Application.CentimetersToPoints(1.5)
       .HeaderMargin = Application.CentimetersToPoints(0.5)
       .FooterMargin = Application.CentimetersToPoints(0.5)
       .CenterHorizontally = True
       .CenterVertically = True
       .FitToPagesWide = 1
       .FitTOpagesTall = 1
  End With 
  Application.PrintCommunication = True
  ActiveSheet.PrintPreview
End Sub

以上で、面倒な印刷設定を高速自動化についての解説を終了します。
ありがとうございました。

スポンサーリンク

関連記事・広告