MORE INFORMATION
The
ReportListener class provides object-based assistance to the
REPORT FORM and
LABEL FORM commands. When the VFP9 Report Engine processes a report in object-assisted mode, it does not directly send the output to a printer or preview device. Instead, it sends information to a
ReportListener object first. The
ReportListener object "listens" for these instructions. Then, it must evaluate and render the content of your report appropriately for your output device. Therefore, you can "hook into" the report processing. Then, you can use code to directly affect elements of the report. A report can have multiple
ReportListener objects. You can design each
ReportListener object to perform a different function. For example, multiple
ReportListener objects may be used to produce multiple types of output during a single report run. These
ReportListener objects may also be used to perform secondary tasks. For example, the objects may be used to compress and to e-mail a report. Report enhancement possibilities are almost limitless with
ReportListener objects.
When you run the code that is in this article, the sample report file that is named Colors.frx runs. Colors.frx is included with Visual FoxPro 9.0. The code outputs the report to an HTML page. Then, the code sends the HTML page through e-mail by using automation of Microsoft Outlook. Depending on specific settings, the report is added to the e-mail message in one of the following ways:
- The report is embedded in an HTML-based e-mail message. The report is embedded in the body of the message.
- The report is sent as a compressed attachment together with the message.
The following conditions must be true for this code to work as-is:
- You must have Microsoft Outlook installed on your Visual FoxPro 9.0 development computer. Also, Microsoft Outlook must be configured correctly to send and to receive e-mail.
- To test the compression and attachment part of the code, you must obtain Cabarc.exe. Cabarc.exe is a command-line tool that lets users create, query, and extract CAB files. Cabarc.exe is available in the Microsoft Cabinet Software Development Kit. It is also availabe in other packages and SDKs from Microsoft.
For more information about the Microsoft Cabinet Software Development Kit, click the following article number to view the article in the Microsoft Knowledge Base:
310618
Microsoft Cabinet Software Development Kit
Note Cabarc.exe is just one of many options for compressing files by using code. It is used in this example for demonstration purposes only. For additional information about the redistribution of Cabarc.exe, see the Redist.txt file that is included with the Microsoft Cabinet Software Development Kit.
To use this code, follow these steps:
- In Visual FoxPro 9.0, copy the following code to a new program file.
- Change the following variables at the start of the code:
- Set the value of the lcMailTo variable to your own e-mail address.
- Set the value of the llSendAsAttachment variable. For additional information, see the following table.
|
To send the report as a compressed attachment | .T. |
To send the report as embedded content in the HTML body of the message | .F. |
- Save the code. Note where you saved the code.
- Note This step is optional.
If you want to test the compression and the attachment of the report, the Cabarc.exe file must be in the same folder as the Visual FoxPro 9.0 program that you saved in step 3. To copy the Cabarc.exe file to this folder, follow these steps:- Locate the Cabarc.exe file in the Bin subfolder where the Microsoft Cabinet Software Development Kit was uncompressed.
- Copy the file to the folder where you saved the sample code in step 3.
- Run the code.
If you set the value of the
lcMailTo variable to your own e-mail address, the report should be in a new e-mail message in Outlook. If you experience any problems, set the
llShowReportProgress_Errs variable in the code to a value of
.T.. When you do this, any errors that occur when you use the
ReportListener class are reported.
*-----------------------------------
* AUTHOR: Trevor Hancock
* CREATED: 03/01/05 02:31:14 PM
* ABSTRACT: Sample code for Microsoft
* Knowledge Base article 895277.
* Demo of sending a report
* to a .HTML file and then e-mailing it
* by using Outlook automation
* and chained ReportListener objects.
*-----------------------------------
#DEFINE HTML_Listener 5
LOCAL lcThisDir AS STRING, ;
loHTMLRL AS ReportListenenr, ;
loMailerRL AS ReportMailer OF SYS(16), ;
lcMailTo AS STRING, ;
lcMailSubjectLine AS STRING, ;
lcReport AS STRING, ;
llSendAsAttach AS Boolean, ;
llShowReportProgress_Errs AS Boolean
*-- Set up some variables.
*-- Set the path of the folder that this program is in.
lcThisDir = ADDBS( JUSTPATH( SYS( 16 ) ) )
*-- This variable holds the object reference to an HTML ReportListener object.
loHTMLRL = NULL
*-- This variable is a second ReportListener object that is responsible for e-mail.
*-- This ReportListener object is defined later in the program.
loMailerRL = NEWOBJECT( 'ReportMailer' )
*-- This variable is the e-mail address of the person that you want to receive the report.
*-- For example, someone@example.com.
lcMailTo = ''
*-- This variable is the subject line for the e-mail.
lcMailSubjectLine = ;
'Report generated and e-mailed by Visual FoxPro 9.0!'
*-- Report to e-mail.
lcReport = HOME( ) + ;
'Samples\Solution\Reports\colors.frx'
*-- This variable controls whether the report is
*-- sent embedded in the HTML body of the
*-- e-mail message (.F.) or is sent as a .CAB file
*-- attachment (.T.)
llSendAsAttachment = .F.
*-- This variable is used to set the QuietMode
*-- property of the HTML ReportListener object. When the
*-- QuietMode property is set, no report progress or error messages
*-- are displayed. Set the property to on (.T.) during development. Then, set
*-- the property to off (.F.) when you are finished. This reduces the need for
*-- any user interaction.
llShowReportProgress_Errs = .F.
*-- Set the current folder, and then clean up a bit.
CD ( lcThisDir )
ERASE TEST.HTM
*-- This code performs error checking.
IF EMPTY( lcMailTo )
MESSAGEBOX( ;
'Please specify an e-mail address by ' + ;
'setting the "lcMailTo" variable in the program.',0,'No Destination')
RETURN .F.
ENDIF
IF llSendAsAttachment AND !FILE('CabArc.exe')
MESSAGEBOX( ;
'Cabarc.exe is required in the same ' + ;
'folder as this program to e-mail the report ' + ;
'as an attachment. For additional information, see the Knowledge Base article.', ;
0,'Cabarc.exe is missing')
RETURN .F.
ENDIF
*-- Run the application that is registered to handle report
*-- output. Then, ask it to store a ReportListener
*-- object reference to the loHTMLRL variable.
*-- The application knows that we want an HTML ReportListener object because
*-- the first parameter, HTML_Listener, equals 5.
*-- The HTML_Listener parameter is defined at the start of this code.
DO (_REPORTOUTPUT) WITH HTML_Listener, loHTMLRL
*--Set up the HTML ReportListener object.
WITH loHTMLRL
*-- Tell the object where to put the .htm file.
.TargetFileName = lcThisDir + 'TEST.HTM'
*-- Setting the QuietMode property tells the object not to prompt with any dialog boxes.
*-- Warning: This setting also disables error reporting.
.QUIETMODE = IIF(llShowReportProgress_Errs, .F., .T.)
*-- Set the successor to the ReportMailer
*-- ReportListener object that is defined later in this program.
*-- By doing this, the code tells the Report Engine to
*-- process instructions in the ReportMailer ReportListener object
*-- after the instructions in this listener.
.Successor = loMailerRL
ENDWITH
*-- Set up the e-mail ReportMailer ReportListener object.
WITH loMailerRL
.Report2Mail = lcThisDir + 'TEST.HTM'
.SendReportasAttachment = llSendAsAttachment
.MailSubject = lcMailSubjectLine
.MailTo = lcMailTo
ENDWITH
*-- Run the report by using the HTML ReportMailer ReportListener object.
*-- First, an HTML file is created. Then, the second ReportMailer ReportListener object
*-- (loMailerRL) is used. In the UnloadReport event
*-- of that object, the report is e-mailed.
REPORT FORM ( lcReport ) OBJECT loHTMLRL
RELEASE loHTMLRL, loMailerRL
*---------------------------------------
*---------------------------------------
*-- Warning: This code only contains light error trapping.
#DEFINE olMailItem 0
DEFINE CLASS ReportMailer AS REPORTLISTENER
Report2Mail = ''
SendReportasAttachment = .F.
MailSubject = ''
MailTo = ''
PROCEDURE UNLOADREPORT()
IF !FILE( THIS.Report2Mail )
DODEFAULT( )
RETURN
ENDIF
LOCAL loOutlook AS Outlook.APPLICATION, ;
loMsg AS Outlook.MESSAGE
ERASE ( THIS.Report2Mail )
ERASE 'REPORT.CAB'
loOutlook = CREATEOBJECT( 'Outlook.Application' )
loMsg = loOutlook.CreateItem( olMailItem )
WITH loMsg
.Recipients.ADD( THIS.MailTo )
.Subject = THIS.MailSubject
IF THIS.SendReportasAttachment
*-- Run Cabarc.exe to make a CAB file.
RUN cabarc N REPORT.cab TEST.HTM
IF !FILE('Report.cab')
DODEFAULT()
RETURN
ENDIF
.Attachments.ADD( ;
JUSTPATH( THIS.Report2Mail ) + ;
'\Report.cab' )
ELSE
.HTMLBody = FILETOSTR( THIS.Report2Mail )
ENDIF
.SEND()
ENDWITH
RELEASE loMsg, loOutlook
ERASE ( THIS.Report2Mail )
ERASE 'REPORT.CAB'
DODEFAULT()
ENDPROC
ENDDEFINE