Report.SaveAsHtml(Text) Method
Version: Available or changed with runtime version 1.0.
Saves a report as an HTML file. The file is saved on the computer where the server instance is running, and then downloaded to the client when ready. This method is only supported when a report uses a Word report layout when it is run.
Note
This method is supported only in Business Central on-premises.
Syntax
[Ok := ] Report.SaveAsHtml(FileName: Text)
Parameters
Report
Type: Report
An instance of the Report data type.
FileName
Type: Text
The folder path and name of the file that you want to save the report as. The path must already exist and the service (login) account that is used by the server instance must have permission to write to the target folder. Otherwise, you will get errors.
Return Value
[Optional] Ok
Type: Boolean
true if the operation was successful; otherwise false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.
Remarks
The SaveAsHTML method can be used on the Report data type and on Report variables. When you are programming the SaveAsHTML method, if you do not know the specific report that you want to run, then use the static version of the SaveAsHTML method, see Report.SaveAsHtml(Integer, Text [, var Record]).
When you call the SaveAsHTML method, the report is generated and saved to the file specified in "FileName." The request page is not shown.
Note
The FileName parameter specifies a location on the computer that is running Business Central. If you call this method from a client, such as from an action on a page, then use the File.Download method to download the .pdf file from the computer that is running the Business Central server to the computer that is running the client.
Prior to version 23.0, the SaveAsHTML method uses the logic in the codeunit 9651 Document Report Mgt. code unit to handle the format transformation. From version 23.0, the platform handles everything.
Error conditions
The method can fail in the following four ways:
- If the default layout of the report is not of type Word,
- If the destination folder specified in FileName does not exist,
- If the file specified in FileName is being used, or
- If the Business Central server process does not have permission to write to the file specified in FileName.
Reports that use an RDLC or Excel layout when run cannot be saved in the HTML format. A runtime error will occur if SaveAsHTML is used on such a report layout.
If the destination folder that you specify in FileName does not exist, then you get the following error: The specified path is invalid.
If the file that you specify in FileName is being used, then you get the following error: An I/O exception occurred during the operation.
If the Business Central server process does not have permission to write to the file that you specify in FileName, then you get the following error: Either the caller does not have the required permission or the specified path is read-only.
Example
This example shows how to use the static SaveAsHtml method in a safe way (where no errors occur).
var
FileNameAndPath: Text[250];
MyReportInstance: Report MyReport;
begin
// setup that FileNameAndPath is valid to write to
// Check if the default layout of MyReport is of type Word
MyReportInstance.SaveAsHtml(FileNameAndPath);
end;
For a full example that shows how to use any of the Report.SaveAs* methods (SaveAsExcel, SaveAsHtml, SaveAsPDF, SaveAsWord, or SaveAsXML) to generate the report in the Business Central server and then write the report output to a file, see Report.SaveAsExcel(FileName: Text).