Visual Basic Concepts

Exporting a Data Report

After compiling a report you may wish to reuse it, either as part of a larger document or perhaps for distribution on an intranet or the Internet. The Data Report designer's ExportReport method allows you to accomplish these tasks. Using the ExportReport method, you can export any report as a text file or as an HTML file. Additionally, you can use any of a number of ExportFormat objects to tailor the content and appearance of an exported file.

Important   The ExportReport method does not support the exporting of images or graphic shapes.

ExportFormat Objects

The ExportReport method was designed to work with the ExportFormats collection. Each ExportFormat object in the collection represents a separate format for the report. For example, a report formatted for intranet distribution might include names of groups or employees as part of the report header; for Internet distribution, those same names would be removed or replaced. You would therefore create at least two ExportFormat objects, each tailored for the distribution mechanism. However, it is possible to export a report without creating any ExportFormat objects because four are already provided for you.

Four Default ExportFormat Objects

By default, the ExportFormats collection contains four members. The four members and their associated file filters are shown in the chart below:

Object File Filter Description
ExportFormats(1) *.htm, *.html HTML
ExportFormats(2) *.htm, *.html Unicode HTML
ExportFormats(3) *.txt Text
ExportFormats(4) *.txt Unicode Text

If you need to use any of the default types, you can also use the Key property to specify a default type. The Key property values and the constants are shown below:

Object Key Constant
ExportFormats(1) key_def_HTML rptKeyHTML
ExportFormats(2) key_def_UnicodeHTML_UTF8 rptKeyUnicodeHTML_UTF8
ExportFormats(3) key_def_Text rptKeyText
ExportFormats(4) key_def_UnicodeText rptKeyUnicodeText

By using one of the four members, you can export a report without creating another ExportFormat object, provided the default meets your requirements. For example, to export a daily HTML report, you might use the following code:

DataReport1.ExportReport rptKeyHTML

Displaying a Dialog Box Is Optional

The programmer can determine whether or not a dialog box will be presented when exporting a report. For example, if the report is created automatically every morning, and written to the same file for distribution by an intranet, there is no need to display a dialog box. As long as a valid file path and key are supplied, and the Overwrite parameter is set to True, the dialog will not be displayed.

' Export a report as HTML, overwriting any existing file. Export
' all pages to the Daily_Report.htm file.
DataReport1.ExportReport rptKeyHTML, "C:\Temp\Daily_Report", True, , _
rptRangeAllPages

Note   In the above code the second argument seems to be a directory but is actually the file name. "Daily_Report.htm" is the name of the written file. The ExportFormat object supplies the file extension (.htm), and thus there's no need to write it in the file name argument.

ExportFormat Supplies Dialog Information

The ExportFormat object also contains the information that is displayed when the user invokes the ExportReport method. In particular, the FileFormatString property sets the text that is displayed in the Export dialog box's Save As Type box. For example, imagine that a company has a standard ExportFormat object to be used with all reports. The following code would ensure that the ExportFormat is available from the list of format types on the Export dialog box:

Dim strTemplate As String

' First create the template for the ExportFormat object.
strTemplate = "MyCompany Daily Report" & vbCrLf & rptTagBody

' Add an ExportFormat object. The FileFormatString determines
' what will be displayed in the Export dialog box.
DataReport1.ExportFormats.Add _
   Key:="StandardReport", _
   FormatType:=rptFmtText, _
   FileFormatString:="Standard Report (*.txt)", _
   FileFilter:="*.txt", _
   Template:=strTemplate

' Invoke the ExportReport method specifying the ExportFormat
' object named StandardReport to use.
DataReport1.ExportReport "StandardReport", , False, True, _
rptRangeFromTo, 1, 10

When invoked, the Export dialog box resembles this:

Template Codes

The core of an ExportFormat object is its template. A template is simply a string containing both the text you want to appear along with constants that represent various parts of the data report. The constants, values, and descriptions are shown in the table below:

Constant Value Description
rptTagTitle <!--MSDBRPT_Template_Title--> Represents the title of the report, as found in the Title property.
rptTagBody <!--MSDBRPT_Template_Body--> Represents the body of the report.

To create a simple data report that includes only the name of the author followed by the body of the report, the template would resemble this:

Dim strT As String
strT = "Author: " & InputBox("Your name") & vbCrLf & rptTagBody
drpNwind.ExportFormats.Add "AuExp", rptFmtText, _
"Author Only Text File", "*.txt", strT