Thursday, October 29, 2009

Print Crystal Report Directly to the printer using VBA

One of my friends asked me about how to directly print the crystal report to the printer using VBA on pressing a button on any of Microsoft Dynamics GP Windows, and I thought I should share this tip:

First, on your VBA project, go to Tools>> References and add a reference to

"Crystal Reports ActiveX Designer Design and Runtime Library"

image

Place your report in an accessible location to be loaded, then place code below where you need to print the report:

Dim CRApp As New CRAXDDRT.Application
Dim CRReport As New CRAXDDRT.Report
Set CRReport = CRApp.OpenReport("C:\rptAccounts.rpt", 1)

Dim CP As CRAXDDRT.ConnectionProperties
For Each tbl In CRReport.Database.Tables
    Set CP = tbl.ConnectionProperties
    CP.Item("User ID") = "sa"
    CP.Item("Password") = "123"
    CP.Item("Data Source") = ".\SQL2005"
    CP.Item("Initial Catalog") = "TWO"
Next tbl
CRReport.PrintOut False

Change "C:\rptAccounts.rpt" to your actual report path and enjoy!

Regards,
--
Mohammad R. Daoud
MVP, MCP, MCT, MCBMSP, MCTS, MCBMSS
CTO
+962 - 79 - 999 65 85
Dynamics Innovations
daoudm@dynamicsinnovations.com
http://www.dynamicsinnovations.com

3 comments:

Indah said...

Hi, Mr.Daoud..

may you have some another tip about crystal report, about how to change database connection in rumtime.

thanks alot for your help.

Mohammad R. Daoud said...

You just will need to add a command to your called crystal report:

For Each Table in Crystal.Tables:

Table.Location = "DatabaseName".dbo."TableName"

Unknown said...

hi , please give me the code to Open The Crystal report

regards,
ajmal khan

Related Posts:

Related Posts with Thumbnails