#include #Region Header ; #HEADER# ======================================================================================================================= ; Title .........: Crystal Reports Object UDF for AutoIt3 ; AutoIt Version : 3.4.4 ; Language ......: English ; Description ...: Functions to integrate Crystal Reports object with AutoIt ; Author(s) .....: ; Modified.......: ; Requirements...: AutoIt v3.3 +, Developed/Tested on Windows 7 64-bit ; using Crystal Reports 11.5.12.1838 and Crystal Reports and Crystal Reports Runtime 11 ; =============================================================================================================================== #EndRegion Header #Region Functions #Region _CrystalReportsExport() ; #FUNCTION# ;=============================================================================== ; Name...........: _CrystalReportsExport() ; Description ...: Exports a Crystal Report to a file ; Syntax.........: _CrystalReportsViewer($fFile, $fSelect = "", $fSaveLoc) ; Parameters ....: $fFile - The Crystal Report file to open. ; $fSelect - The record selection formula to used. "" = none ; $fSaveLoc - The location to save the report. ; Return values .: On Success - Creates a file in the designated location. ; On Failure - Returns False. ; Author ........: ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; ;========================================================================================== Func _CrystalReportsExport($fFile, $fusername, $fpassword, $fSelect = "", $fSaveLoc = "C:\" & @UserName & "\Desktop\report.pdf") Do ; Create Crystal Reports object Local $oCrystal = ObjCreate("CrystalRunTime.Application.11") If Not IsObj($oCrystal) Then MsgBox(0, "Error", "Could not create CrystalRuntime Object") ExitLoop EndIf ; Open the report Local $oReport = $oCrystal.OpenReport($fFile) If Not IsObj($oReport) Then MsgBox(0, "Error", "Could not create Crystal Report Object") ExitLoop EndIf ; Setup Report With $oReport .Database.Tables(1).ConnectionProperties.Item('User ID') = $fusername ; For database login .Database.Tables(1).ConnectionProperties.Item('Password') = $fpassword ; For database login .DiscardSavedData ; Discard the rest .DisplayProgressDialog = False ; Hide the export progress window .RecordSelectionFormula = $fSelect ; Select records to display EndWith Local $objExportOptions = $oReport.ExportOptions With $objExportOptions .DestinationType = 1 ; Export destination, 1 = File .DiskFileName = $fSaveLoc ; File name .FormatType = 31 ; File format type, 31 = PDF .PDFExportAllPages = True ; Export all pages EndWith $oReport.Export(False) ; Start export process, False = Skip selection window and start exporting Until 1 EndFunc #EndRegion _CrystalReportsExport() #Region _CrystalReportsViewer() ; #FUNCTION# ;=============================================================================== ; Name...........: _CrystalReportsViewer() ; Description ...: Pops up a window and displays a Crystal Report ; Syntax.........: _CrystalReportsViewer($fFile, $fSelect) ; Parameters ....: $fFile - The Crystal Report file to open. ; $fSelect - The record selection formula to used. "" = none ; Return values .: On Success - Returns an array with the latitude and longitude of the address. ; On Failure - Returns False. ; Author ........: ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; ;========================================================================================== Func _CrystalReportsViewer($fFile, $fusername, $fpassword, $fSelect = "") Do $oCRViewer = ObjCreate("CrystalReports.ActiveXReportViewer") ; Create a Crystal Reports Viewer control If Not IsObj($oCRViewer) Then MsgBox(0, "Error", "Could not create ActiveXReportViewer Object") ExitLoop EndIf $oCRViewerEvt = ObjEvent($oCRViewer,"CRViewerEvent_") ; Catch events from the control With $oCRViewer .DisplayBorder = False ; Makes the report fill the entire form .DisplayTabs = False ; Display tabs .EnableDrillDown = False ; Enable drill down .EnableRefreshButton = True ; Enable refresh button .DisplayGroupTree = False ; Hide Group Tree at start EndWith $oCrystal = ObjCreate("CrystalRunTime.Application") If Not IsObj($oCRViewer) Then MsgBox(0, "Error", "Could not create CrystalRuntime Object") ExitLoop EndIf $oReport = $oCrystal.OpenReport($fFile) ; Open the report If Not IsObj($oReport) Then MsgBox(0, "Error", "Could not open Crystal Report file:" & @CRLF & $fFile) ExitLoop EndIf With $oReport .Database.Tables(1).ConnectionProperties.Item('User ID') = $fusername .Database.Tables(1).ConnectionProperties.Item('Password') = $fpassword .RecordSelectionFormula = $fSelect .DiscardSavedData EndWith $oCRViewer.ReportSource = $oReport $oCRViewer.ViewReport With $oCRViewer .Zoom(90) ; Set zoom to 90% (has to be done after loaded) EndWith GUICreate ( "Crystal Reports Viewer", 780, 740 ) $GUI_FileMenu = GUICtrlCreateMenu ("&File") ; Create File Menu $GUI_FileOpen = GUICtrlCreateMenuitem ("&Open...", $GUI_FileMenu) $GUI_FileSepa = GUICtrlCreateMenuitem ("", $GUI_FileMenu) ; Ceate a separator line $GUI_FileExit = GUICtrlCreateMenuitem ("E&xit", $GUI_FileMenu) $GUI_ActiveX = GUICtrlCreateObj ( $oCRViewer, 0, 0 , 780 , 720 ) GUISetState () ;Show GUI ; Waiting for user to close the window While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case $msg = $GUI_FileExit ExitLoop Case Else if $msg = $GUI_FileOpen Then $file = FileOpenDialog("Select RPT Source File", ".", "RPT (*.rpt)", 3) ;Filter "." means all EndIf EndSelect Wend GUIDelete () Until 1 EndFunc #EndRegion _CrystalReportsViewer() #EndRegion Functions