印刷プレビューに一発で行く方法
下記はExcel経由で自動的に印刷プレビュー(または自動印刷)まで遷移するプログラムである。
GC.Collect()
Dim filename As String = "こちらにファイル名を入れてください"
' Excel起動
Dim xlApp As New Excel.Application
xlApp.Visible = False
xlApp.DisplayAlerts = False
Dim xlBook As Excel.Workbook = Nothing
Dim xlSheet As Excel.Worksheet = Nothing
Try
' Excelファイルを開く
xlBook = xlApp.Workbooks.Open(filename)
' シート1を取得(xlBook を開いた後に行う)
xlSheet = CType(xlBook.Sheets(1), Excel.Worksheet)
' PrintPreview は Excel が可視状態でないと失敗する場合あり
xlApp.Visible = True
' 印刷プレビュー、または自動印刷
xlSheet.PrintPreview()
' ★ 自動印刷(ユーザーにはExcel起動が見えない) xlApp.Visible = Trueをfalseに変更してください
‘xlSheet.PrintOut()
Catch ex As Exception
MessageBox.Show("エラー:" & ex.Message)
Finally
' Excelを閉じる(必要なら)
If xlBook IsNot Nothing Then
xlBook.Close(False)
End If
xlApp.Quit()
' COMオブジェクト解放
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
Me.Close()