|
■No79953 (かけだしSE さん) に返信 > CRYSTAL REPORTSで作成したレポートを ActiveX 版を、VB4 当時に少し使ったぐらいで、 .NET 版は使った事がありません…。
> ReportDocumentではなく、PrintDocumentならDocumentNameを指定する事で PrintDocument.DocumentName ではなく、 PrinterSettings.PrintFileName (と .PrintToFile)だと思っていました。
> 自動保存が可能でしたが、ReportDocumentではDocumentNameを指定出来ず .NET 版のCrystal Reports 初挑戦なので、外しているかもしれませんが、 手元の VB2008(.NET 3.5)では、下記のコードで出力されました。
VB2015 が手元に無かったので、かけだしSEさんの環境で動くかどうかは分かりません。
'--------------------- Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim report As New CrystalReport1()
If XPS直接出力 Then report.PrintToPrinter("さんぷる", "C:\TEMP\さんぷる.XPS", 1, True, 1, 1) Else report.PrintToPrinter(1, True, 1, 1) End If End Sub End Class
'--------------------- Imports System.Runtime.CompilerServices Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Imports System.Drawing.Printing Imports System.Reflection
Module ReportDocumentExtensions
<Extension()> _ Public Sub PrintToPrinter(ByVal this As ReportClass, ByVal documentName As String, ByVal printFileName As String, ByVal nCopies As Integer, ByVal collated As Boolean, ByVal startPageN As Integer, ByVal endPageN As Integer) Dim formatEngine As FormatEngine = this.FormatEngine Dim formatEngineType As Type = formatEngine.GetType() Dim bfs As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.Static Dim lastPageNumber As Integer = DirectCast(formatEngineType.GetProperty("ReportSource", bfs).GetValue(formatEngine, BindingFlags.GetProperty, Nothing, Nothing, Nothing), IReportSource).GetLastPageNumber(New PageRequestContext())
Dim document As New PrintDocument() this.PrintOptions.CopyTo(document.PrinterSettings, document.DefaultPageSettings)
document.DocumentName = documentName document.PrinterSettings.PrintFileName = printFileName document.PrinterSettings.PrintToFile = True
Dim handler As MethodInfo = formatEngineType.GetMethod("PrintPageEventHandler", bfs) AddHandler document.PrintPage, Function(sender, e) handler.Invoke(formatEngine, BindingFlags.InvokeMethod, Nothing, New Object() {sender, e}, Nothing) With document.PrinterSettings .Collate = collated .Copies = CShort(nCopies) .FromPage = Math.Max(1, startPageN) .ToPage = If(endPageN <= 0, lastPageNumber, Math.Min(lastPageNumber, endPageN)) If .FromPage <= .ToPage Then Dim bfi As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance formatEngineType.GetField("m_CurrentPrintingPage", bfi).SetValue(formatEngine, .FromPage, bfi, Nothing, Nothing) formatEngineType.GetField("m_XPrintingOffset", bfi).SetValue(formatEngine, 0, bfi, Nothing, Nothing)
document.Print() End If End With End Sub End Module
|