Dynamics 365 Business Central: How to preview Word (.docx) in web client without downloading (No control add-in and APIs)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to discuss an interesting topic, how to preview Word (.docx) in web client without downloading (No control add-in and APIs).

In Business Central 2025 wave 1 (BC26), Microsoft released the Preview PDF attachments directly in web client feature. This feature lets you open PDF attachments directly in the Business Central web client without downloading them first. Files are shown in preview mode in a specialized viewer experience, similar to the print preview feature, and you can always download a PDF file from there. More details: Business Central 2025 wave 1 (BC26): Preview PDF attachments directly in web client (No control add-in and APIs)

However, this feature cannot preview formats other than PDF, such as Word (.docx).

In Dynamics 365 Business Central: Preview files within BC (PDF, JPG, PPTX, DOCX, XLSX, TXT, CSV, MP4…… Most file types supported for previewing files in SharePoint) – No JavaScript control add-in, we discussed how to use the Graph API to save files to SharePoint and then preview them. Although this can preview the Word (.docx), it is quite complicated.

In this blog, I will share two simple solutions, hoping to give you some tips.

Convert Word to PDF, and then preview it as PDF

This method is mainly combined by the following two methods we have discussed.

Test video:

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(PreviewWord_Promoted; PreviewWord)
            {
            }
        }
        addfirst(processing)
        {
            action(PreviewWord)
            {
                Caption = 'Preview Word';
                ApplicationArea = All;
                Image = View;

                trigger OnAction()
                var
                    DocAttach: 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.ConvertWordToPdf(TempBlob, 0);
                            TempBlob.CreateInStream(InStr);
                            FileName := Format(DocAttach."File Name" + '.pdf');
                            File.ViewFromStream(InStr, FileName + '.' + 'pdf', true);
                        end;
                end;
            }
        }
    }
}

Convert Word to HTML, and then preview as HTML

This method is mainly combined by the following two methods we have discussed.

Test video:

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

pageextension 50110 DocumentAttachmentDetailsExt extends "Document Attachment Details"
{
    actions
    {
        addafter(Preview_Promoted)
        {
            actionref(PreviewWord_Promoted; PreviewWord)
            {
            }
        }
        addfirst(processing)
        {
            action(PreviewWord)
            {
                Caption = 'Preview Word';
                ApplicationArea = All;
                Image = View;
                trigger OnAction()
                var
                    PreviewFiles: Page "Preview Files";
                    DocAttach: Record "Document Attachment";
                    HTMLText: Text;
                    InStr: InStream;
                    OutStr: OutStream;
                    TempBlob: Codeunit "Temp Blob";
                    DocumentReportMgt: Codeunit "Document Report Mgt.";
                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);
                            InStr.ReadText(HTMLText);
                            PreviewFiles.SetURL(HTMLText);
                            PreviewFiles.Run();
                        end;
                end;
            }
        }
    }
}
page 50111 "Preview Files"
{
    Caption = 'Preview';
    Editable = false;
    PageType = Card; //Use new UserControlHost PageType from BC26
    layout
    {
        area(content)
        {
            usercontrol(WebPageViewer; WebPageViewer)
            {
                ApplicationArea = All;
                trigger ControlAddInReady(callbackUrl: Text)
                begin
                    CurrPage.WebPageViewer.SetContent(HTMLText);
                end;

                trigger Callback(data: Text)
                begin
                    CurrPage.Close();
                end;
            }
        }
    }
    var
        HTMLText: Text;

    procedure SetURL(NavigateToURL: Text)
    begin
        HTMLText := NavigateToURL;
    end;
}

Very simple, give it a try!!!😁

PS: Dynamics 365 Business Central Blog Series: Preview files directly in the web client

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL