PowerShell – Crystal Reports COM Object

December 17, 2017

Recently I was going through my files and found (again) Smile the very nice Crystal Reports COM libraries



As you can see these are more then 15 years old !!

But still works like a charm … many years back I created an example in AutoIT using the CRViewer COM object.


Actually the Crystal Reports at that time came with some nice COM libraries, which are still my all time favourites.

And tons of functionality to automate your tasks…


It can access all kinds of Database formats using ODBC and native DB connections.

As you can see it also has a multitude of Export and printing abilities.

$Crystal_Report = 1
$Data_Interchange = 2
$Record_Style = 3
$CSV = 5
$Tab_Sep_Val = 6
$CharSepVal = 7
$Text = 8
$TabSepText = 9
$Paginated_Text = 10
$Lotus_23WKS = 11
$Lotus_123WK1 = 12
$Lotus_123WK3 = 13
$Word = 14
$Excel_5 = 21
$Excel_5_Tabular = 22
$ODBC = 23
$HTML32 = 24
$Explorer32 = 25
$Excel_7= 27
$Excel_7_Tabular = 28
$Excel_8 = 29
$Excel_8_Tabluar = 30
$PDF = 31
$HTML40 = 32
$Crystal_Report_7 = 33
$Report_Definition = 34
$RTF = 35
$XML = 36


PowerShell COM Backwards compatible ?

So the idea now is to see how PowerShell is able to handle these old COM libraries.

Here we go …  Smile

This script will read a Crystal Report 8.5 report using a MS SQL database data source.

Refreshes the data in the report, Exports it to Excel and next E-mails this to 1 or more recipients.


$Excel_7= 27

$ServerType = "p2lodbc.dll"
$ServerName = "ServerName"
$databaseName = "DatabaseName"
$user = "UserName"
$pswd = "Password"
$vFilenameReport = "C:\Deliveries per customer.rpt"
$vFilenameExport = "C:\Deliveries per customer.xls"

$oApp = New-Object -ComObject "CrystalRuntime.Application" # Com Object

# LogOnServer
    $oApp.LogOnServer($ServerType, $ServerName, $databaseName, $user, $pswd)

# Open Report
  $oRpt = $oApp.OpenReport($vFilenameReport, 0) # 0 / 1

# Report Options
  $oRpt.DiscardSavedData() # DiscardSavedData In Report to REFRESH
  $oRpt.EnableParameterPrompting = $False
  $oRpt.DisplayProgressDialog = $False
  $oRpt.MorePrintEngineErrorMessages = $False

# Export Options
Start-Sleep 2

  # $oRpt.ExportOptions.Reset | out-null
   $oRpt.ExportOptions.DestinationType = 1 # 1=>filesystem 
   $oRpt.ExportOptions.FormatType = $Excel_7  # 27=> Excel 7
   $oRpt.ExportOptions.DiskFileName = $vFilenameExport 

  # $oRpt.ExportOptions.ExcelExportAllPages = $True 
  # $oRpt.Export($True) # Export without prompting = False

   $oRpt.Export($False) # Export without prompting = False

# Send Email
 Send-MailMessage -to Your.Name@Company.com `
-from admin@Company.com `
-Subject "Monthly Email Service" `
-body "Hello !!! `nThis is your personal E-mail service.`nPlease find the updates enclosed.`nUntill next time. `nRegards," `
-Attachments $vFilenameExport `
-smtpserver YourEmailServer  

# Remove Attachment File
   Start-Sleep -s 2
   Remove-Item $vFilenameExport


If you schedule this script you have created your own reporting platform.



Keep in mind that you need to you the 32Bit PowerShell version



But it doesn’t spoil the fun Smile

And this proves PowerShell is 100% backwards COM compatible !!


Enjoy !


Crystal Reports – Using Shared Variable in version 8.5

February 28, 2014

This example show how to use Shared Variables in CR 8.5

I know it is an old version but the principles can be used in later versions as well.

Objective was to transfer a value from a sub-report into the main report for further calculations.

1. First create the sub-report, and link it to the Main report using the Insert Menu


2. When the link has been setup, insert a new Formula Field


In the new formula field you add a Shared Variable.

Shared NumberVar myTotal :=Sum ({Your.Field})

3. Next you go the Main Report and add as well a new Formula field.


In the new formula field you add reference to the previous  Shared Variable, of the sub-report.

Shared NumberVar myTotal;

4. Add the new Formula field in the main report


Keep in mind that you need to add the field in a SECTION BELOW the detailed section of the main report. In order for the variable to grab the values correct.

5. Making calculations using Shared Variables.

You can use the this just like any other field value to make your calculations.

Shared NumberVar myTotal;
If {Your.Field} <> 0 then
myTotal / {Your.Field};


To get the sub-report field properly formatted in your report it’s a bit tricky.

Remove the BORDERS and if needed uncheck the Keep Objects Together




Enjoy !!