Dynamics 365 Business Central: Convert Word (.docx) To HTML (.html) within BC (No control add-in and APIs)

Dynamics 365 Business Central

Hi, Readers.
Last week we briefly discussed how to convert Word (.docx) To PDF (.pdf) within BC (No control add-in and APIs). Today I would like to talk about another interesting topic, how to convert Word (.docx) To HTML (.html) within BC (No control add-in and APIs).

Microsoft Word is a word processing program developed by Microsoft. And in Business Central, with Word layouts, you use Word as the editor for the report.

And HTML is the technology that defines the content and structure of any website. For example,

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:DodgerBlue;">This is HTML Heading</h1>
<p style="background-color:Tomato;">This is HTML paragraph.</p>
<a href="https://yzhums.com">ZHU's blog</a>
</body>
</html>

More details: Dynamics 365 Business Central: Using WebPageViewer Add-in (“Microsoft.Dynamics.Nav.Client.WebPageViewer”)

As you know, we can convert to HTML through Word, or through third-party websites or tools, for example, Word to HTML.

PS:
1. Business Central 2023 wave 2 (BC23): Use the built-in rich text editor to enter data

2. Business Central 2023 wave 2 (BC23): Using rich text (HTML) content in RDL layouts

But do you know that there is also such a standard method in BC?

codeunit 9651 “Document Report Mgt.” -> procedure ConvertWordToHtml(var TempBlob: Codeunit “Temp Blob”)

Let’s look at two simple tests.

1. Convert the word file selected in Document Attachment Details into a HTML file

Test video:

2. Upload a Word document and convert it to HTML

Test video:

Very simple, give it a try!!!😁

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50112 DocumentAttachmentDetailsExt extends "Document Attachment Details"
{
    actions
    {
        addafter(Preview_Promoted)
        {
            actionref(ConvertWordToHTML_Promoted; ConvertWordToHTML)
            {
            }
        }
        addfirst(processing)
        {
            action(ConvertWordToHTML)
            {
                Caption = 'Convert Word To HTML';
                ApplicationArea = All;
                Image = View;
                trigger OnAction()
                var
                    DocAttach: Record "Document Attachment";
                    DocAttachHTML: Record "Document Attachment";
                    InStr: InStream;
                    OutStr: OutStream;
                    TempBlob: Codeunit "Temp Blob";
                    DocumentReportMgt: Codeunit "Document Report Mgt.";
                    FileName: Text;
                begin
                    DocAttach.Reset();
                    CurrPage.SetSelectionFilter(DocAttach);
                    if DocAttach.FindFirst() then
                        if DocAttach."Document Reference ID".HasValue then begin
                            TempBlob.CreateOutStream(OutStr);
                            DocAttach."Document Reference ID".ExportStream(OutStr);
                            DocumentReportMgt.ConvertWordToHtml(TempBlob);
                            TempBlob.CreateInStream(InStr);
                            FileName := DocAttach."File Name" + ' (1)';
                            DocAttachHTML.Init();
                            DocAttachHTML.Validate("Table ID", DocAttach."Table ID");
                            DocAttachHTML.Validate("No.", DocAttach."No.");
                            DocAttachHTML.Validate("File Name", FileName);
                            DocAttachHTML.Validate("File Extension", 'html');
                            DocAttachHTML."Document Reference ID".ImportStream(InStr, FileName);
                            DocAttachHTML.Insert(true);
                        end;
                end;
            }
        }
    }
}
pageextension 50100 CustomerListExt extends "Customer List"
{
    actions
    {
        addfirst(processing)
        {
            action(ConvertWordToHTML)
            {
                ApplicationArea = All;
                Caption = 'Convert Word To HTML';
                Image = Change;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction()
                var
                    WordFileName: Text[100];
                    HTMLFileName: Text[100];
                    InStr: InStream;
                    OutStr: OutStream;
                    FileMgt: Codeunit "File Management";
                    TempBlob: Codeunit "Temp Blob";
                    DocumentReportMgt: Codeunit "Document Report Mgt.";
                begin
                    TempBlob.CreateOutStream(OutStr);
                    FileMgt.BLOBImport(TempBlob, WordFileName);
                    DocumentReportMgt.ConvertWordToHtml(TempBlob);
                    TempBlob.CreateInStream(InStr);
                    HTMLFileName := 'Converted' + '.html';
                    DownloadFromStream(InStr, '', '', '', HTMLFileName);
                end;
            }
        }
    }
}

PS: If you encounter a file that cannot be converted, the following error message will be displayed, so it is okay not to add a control on the file type.

A call to Microsoft.BusinessCentral.DocumentProcessor.WordTransformation.ConvertToHtml failed with this message: Unsupported package content in Word document. Cannot detect the document part

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL