locked
Opening, Running, and Exporting a Crystal Report RRS feed

  • Question

  • Hello,

    I am attempting to write a script that will open a Crystal Report that has already been created, then use keyboard commands to run and export the results.  I have what I hope will work below (tested using Notepad successfully), but when I try to open the Crystal Report file, I get the following error:

    New-Object : Exception calling ".ctor" with "0" argument(s): "The type initializer for 'CrystalDecisions.CrystalReports.Engine.ReportDocument' thew an exception."
    At line: 1 char: 11
    + $report = New0Object CrystalDecisions..CrystalReports.Engine.ReportDocument
    +           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
          + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    As a PS beginner, I was able to find this sample code online and do my best to adapt it to my environment, but I don't fully understand what the top part of the code is doing.

    Here is the full script I have so far.  Like I said, if I switch the top part to opening a notepad file, but bottom keystroke part works.  I just need it to do the same for the Crystal Report.  Any help would be greatly appreciated.

     

    add-type -AssemblyName microsoft.VisualBasic

    add-type -AssemblyName System.Windows.Forms

    #load the assemblies

    [reflection.assembly]::LoadWithPartialName('CrystalDecisions.Shared')
    [reflection.assembly]::LoadWithPartialName('CrystalDecisions.CrystalReports.Engine') 
    [reflection.assembly]::LoadWithPartialName('CrystalDecisions.Windows.Forms')  

    $report = New-Object CrystalDecisions.CrystalReports.Engine.ReportDocument
    $report.Load("C:\Report\DailyUPRPT.rpt")


    start-sleep -Seconds 5

    [Microsoft.VisualBasic.Interaction]::AppActivate("C:\Report\DailyUPRPT.rpt")

    [System.Windows.Forms.SendKeys]::SendWait("{F5}")
    [System.Windows.Forms.SendKeys]::SendWait("{Enter}")
    start-sleep -Seconds 10
    [System.Windows.Forms.SendKeys]::SendWait("%F")
    [System.Windows.Forms.SendKeys]::SendWait("E")
    [System.Windows.Forms.SendKeys]::SendWait("{Enter}")
    start-sleep -Seconds 1
    [System.Windows.Forms.SendKeys]::SendWait("{Enter}")
    start-sleep -Seconds 1
    [System.Windows.Forms.SendKeys]::SendWait("{Enter}")
    start-sleep -Seconds 1
    [System.Windows.Forms.SendKeys]::SendWait("{Enter}")
    start-sleep -Seconds 1
    [System.Windows.Forms.SendKeys]::SendWait("{Enter}")

    • Moved by Bill_Stewart Monday, November 23, 2015 9:25 PM This is not a third-party software support forum
    Tuesday, September 29, 2015 3:39 PM

Answers

  • Aside from the fact that we definitely do not recommend sending keystrokes to a window (too error-prone), this is not a support forum for Crystal Reports. You need to ask your question in a more appropriate place, such as a Crystal Reports forum. (You should be able to search for one.)


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Tuesday, September 29, 2015 4:11 PM
    • Marked as answer by Bill_Stewart Monday, November 23, 2015 9:24 PM
    Tuesday, September 29, 2015 4:01 PM

All replies

  • Aside from the fact that we definitely do not recommend sending keystrokes to a window (too error-prone), this is not a support forum for Crystal Reports. You need to ask your question in a more appropriate place, such as a Crystal Reports forum. (You should be able to search for one.)


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Tuesday, September 29, 2015 4:11 PM
    • Marked as answer by Bill_Stewart Monday, November 23, 2015 9:24 PM
    Tuesday, September 29, 2015 4:01 PM
  • I second Bill's suggestion to not use SendKeys. If you really absolutely have to do GUI manipulation, use a tool that's more suited to the task, such as AutoIt.

    Tuesday, September 29, 2015 4:11 PM
  • Crystal can be fully automated without keystrokes.  Look at the object model and documentation on the Crystal web site and post in the Crystal forum for help.

    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Tuesday, September 29, 2015 4:19 PM
    Tuesday, September 29, 2015 4:19 PM