RegOpenKeyEx Subroutine Example
The following example is the actual PrintReport subroutine from FactoryGlobals global subroutines. Here, we implement late binding to run Crystal Reports. First, find out if Crystal Reports is installed on the user's machine with RegOpenKeyEx. If it is, we create an instance of it. Then, we close the registry key with RegCloseKey.
NOTE: This example does not apply to Crystal XI; it applies to an earlier version, such as Crystal 7. For Crystal XI, use the PrintReport subroutine instead. The PrintReport subroutine will do all of the registry entries for you.
Public Sub PrintReport(ByVal Report As String, Optional Prompt As Boolean, Optional ByVal Copies As Long, Optional ByVal Coll As Boolean, Optional ByVal StartNo As Long, Optional ByVal EndNo As Long)
Dim CrystalApplication As Object
Dim CrystalReport As Object
Dim lngResult As Long
Dim lngRes As Long
On Error GoTo ErrorHandler
'Check if Crystal Reports is installed.
lngResult = RegOpenKeyEx(&H80000000, "CrystalReports", &O0, &H20000, lngRes)
'If it is, create an instance of it.
If lngResult = 0 Then
Set CrystalApplication = CreateObject("Crystal.CRPE.Application")
'If not, send the user a message.
Else
MsgBox "You do not have Crystal Reports installed."
End
End If
'Close the registry key.
lngResult = RegCloseKey(&H80000000)
Set CrystalReport = CrystalApplication.OpenReport(Report)
CrystalReport.PrintOut Prompt, Copies, Coll, StartNo, EndNo
Exit Sub
ErrorHandler:
HandleError
End Sub