i'm working at trying to get printing to work out properly myself. In order to print it out, it must be exported first into something such as a *.pdf file. I've got code that is working for that, but I'd like to have a print button where it would export into *.pdf, then call Adobe Acrobat and open the exported file there for printing.
Here's my code that is exporting the report into *.pdf:
Sub PrintReport()
' This subroutine exports a file into Adobe Acrobat in order to print it.
' ********************************
'Check to see if the application directory has a subdirectory called "C:\ExportFileName\".
'If not, create the directory since exported files will be placed here.
'This uses the Directory class of the System.IO namespace.
Dim ExportPath As String
ExportPath = "C:\ExportFileName\"
If Directory.Exists(ExportPath) = False Then
Directory.CreateDirectory("C:\ExportFileName")
End If
' ********************************
'Set the time for the file name in order to affix it the the created file.
Dim DateTimeNow As DateTime
Dim Yr As Int16
Dim Yr2 As String
Dim Mth As Int16
Dim mth2 As String
Dim Dy As Int16
Dim dy2 As String
Dim Hr As Int16
Dim hr2 As String
Dim Mn As Int16
Dim mn2 As String
Dim strDateTimeNow As String
DateTimeNow = Now()
Yr = Year(DateTimeNow)
Yr2 = Yr.ToString
Mth = Month(DateTimeNow)
If Mth <= 9 Then
mth2 = "0" + Mth.ToString
Else
mth2 = Mth.ToString
End If
Dy = Day(DateTimeNow)
If Dy <= 9 Then
dy2 = "0" + Dy.ToString
Else
dy2 = Dy.ToString
End If
Hr = Hour(DateTimeNow)
If Hr <= 9 Then
hr2 = "0" + Hr.ToString
Else
hr2 = Hr.ToString
End If
Mn = Minute(DateTimeNow)
If Mn <= 9 Then
mn2 = "0" + Mn.ToString
Else
mn2 = Mn.ToString
End If
strDateTimeNow = Yr2 + mth2 + dy2 + hr2 + mn2
' ********************************
' this allows me to login to my file. If no logon is needed, skip this step.
Dim crtableLogoninfos As New TableLogOnInfos
Dim crtableLogoninfo As New TableLogOnInfo
Dim crConnectionInfo As New ConnectionInfo
Dim CrTables As Tables
Dim CrTable As Table
Dim TableCounter
Dim crReportDocument As New ReportDocument
'ReportFullPathName is a variable I created that holds the path and name of the report file.
crReportDocument.Load(ReportFullPathName)
'Set the ConnectionInfo properties for logging on to the Database
'If you are using ODBC, this should be the DSN name NOT the physical server name. If
'you are NOT using ODBC, this should be the physical server name
With crConnectionInfo
.ServerName = "MRI_Dwh_Dev"
'If you are connecting to Oracle there is no DatabaseName. Use an empty string.
'For example, .DatabaseName = ""
.DatabaseName = ""
.UserID = strUID
.Password = strPWD
End With
'This code works for both user tables and stored procedures. Set the CrTables to the Tables collection
'of the report
CrTables = crReportDocument.Database.Tables
'Loop through each table in the report and apply the LogonInfo information
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next
crDiskFileDestinationOptions = New DiskFileDestinationOptions
crExportOptions = crReportDocument.ExportOptions
'Export to PDF
'append a filename to the export path and set this file as the filename property for
'the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportPath + ReportName + strDateTimeNow + ".pdf"
'set the required report ExportOptions properties
With crExportOptions
.DestinationOptions = crDiskFileDestinationOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
End With
'Once the export options have been set for the report, the report can be exported. The Export command
'does not take any arguments
Try
' Export the report
crReportDocument.Export()
'Response.WriteFile(ExportPath + ReportName + strDateTimeNow + ".pdf")
Catch err As Exception
Response.Write("<BR>")
Response.Write(err.Message.ToString)
End Try
End Sub
|