microsoft / ALAppExtensions

Repository for collaboration on Microsoft AL application add-on and localization extensions for Microsoft Dynamics 365 Business Central.
MIT License
778 stars 612 forks source link

[Event Request] Codeunit 5054 - WordManagement - OnBeforeAddFieldsToMergeSource #5315

Closed MattiLE closed 4 years ago

MattiLE commented 4 years ago

Hi AL-Team, can you please add the following publishers to ExecuteMerge?

    local procedure ExecuteMerge(var WordApplication: DotNet ApplicationClass; var TempDeliverySorter: Record "Delivery Sorter" temporary)
    var
        Attachment: Record Attachment;
        InteractLogEntry: Record "Interaction Log Entry";
        TempSegLine: Record "Segment Line" temporary;
        [RunOnClient]
        WordDocument: DotNet Document;
        [RunOnClient]
        WordInlineShape: DotNet InlineShape;
        [RunOnClient]
        WordMergefile: DotNet MergeHandler;
        [RunOnClient]
        WordOLEFormat: DotNet OLEFormat;
        [RunOnClient]
        WordLinkFormat: DotNet LinkFormat;
        [RunOnClient]
        WordShape: DotNet Shape;
        MergeFile: File;
        MergeClientFileName: Text;
        MainFileName: Text;
        NoOfRecords: Integer;
        ParamBln: Boolean;
        ParamInt: Integer;
        Row: Integer;
        ShapesIndex: Integer;
        HeaderIsReady: Boolean;
        FaxMailToValue: Text;
        HeaderFieldCount: Integer;
    begin
        Window.Update(
          6, StrSubstNo(Text008,
            Format(TempDeliverySorter."Correspondence Type")));

        if TempDeliverySorter.Find('-') then
            NoOfRecords := TempDeliverySorter.Count;

        Attachment.Get(TempDeliverySorter."Attachment No.");
        Attachment.CalcFields("Attachment File");

        // Handle Word documents without mergefields
        if not DocumentContainMergefields(Attachment) and
           TempDeliverySorter."Send Word Docs. as Attmt."
        then begin
            SendAttachmentWithoutMergeFields(WordApplication, TempDeliverySorter, Attachment);
            exit;
        end;

        with TempDeliverySorter do begin
            SetCurrentKey("Attachment No.", "Correspondence Type", Subject);
            Find('-');
        end;
        Row := 2;

        MainFileName := FileMgt.ClientTempFileName('DOC');
        TempDeliverySorter.Find('-');
        Attachment.Get(TempDeliverySorter."Attachment No.");
        Attachment.CalcFields("Attachment File");
        if not IsWordDocumentExtension(Attachment."File Extension") then
            Error(Text010, Attachment.TableCaption, Attachment."No.", Attachment.FieldCaption("File Extension"));

        if not Attachment.ExportAttachmentToClientFile(MainFileName) then
            Error(Text011);

        Window.Update(6, Text012);
        Attachment.CalcFields("Merge Source");
        if Attachment."Merge Source".HasValue then begin
            CreateMergeSource(MergeFile);
            repeat
                PopulateInterLogEntryToMergeSource(
                  MergeFile, Attachment, TempDeliverySorter."No.", HeaderIsReady, TempDeliverySorter."Correspondence Type");
                Row := Row + 1;
                Window.Update(4, Round(Row / NoOfRecords * 10000, 1))
            until TempDeliverySorter.Next = 0;
            MergeClientFileName := CloseAndDownloadMergeSource(MergeFile);
        end else begin
            MergeClientFileName := FileMgt.ClientTempFileName('HTM');
            WordMergefile := WordMergefile.MergeHandler;
            HeaderFieldCount := CreateHeader(WordMergefile, false, MergeClientFileName, TempDeliverySorter."Language Code");
            repeat
                InteractLogEntry.Get(TempDeliverySorter."No.");

                // This field must come last in the merge source file
                case TempDeliverySorter."Correspondence Type" of
                    TempDeliverySorter."Correspondence Type"::Fax:
                        FaxMailToValue := AttachmentManagement.InteractionFax(InteractLogEntry);
                    TempDeliverySorter."Correspondence Type"::Email:
                        FaxMailToValue := AttachmentManagement.InteractionEMail(InteractLogEntry);
                    else
                        FaxMailToValue := '';
                end;
                // NEW >>>>
                OnBeforeAddFieldsToMergeSource(TempSegLine, TempDeliverySorter);
                // NEW <<<<
                AddFieldsToMergeSource(WordMergefile, InteractLogEntry, TempSegLine, FaxMailToValue, HeaderFieldCount);
                Row := Row + 1;
                Window.Update(4, Round(Row / NoOfRecords * 10000, 1))
            until TempDeliverySorter.Next = 0;
            WordMergefile.CloseMergeFile;
        end;

        WordDocument := WordHelper.CallOpen(WordApplication, MainFileName, false, false);
        WordDocument.MailMerge.MainDocumentType := 0;

        Window.Update(6, Text013);
        ParamInt := 7; // 7 = HTML
        WordHelper.CallMailMergeOpenDataSource(WordDocument, MergeClientFileName, ParamInt);
        Window.Update(6, StrSubstNo(Text014, TempDeliverySorter."Correspondence Type"));

        for ShapesIndex := 1 to WordDocument.InlineShapes.Count do begin
            WordInlineShape := WordHelper.GetInlineShapeItem(WordDocument, ShapesIndex);
            WordInlineShape.Select;
            if not IsNull(WordInlineShape) then begin
                WordShape := WordInlineShape.ConvertToShape;
                WordLinkFormat := WordShape.LinkFormat;
                WordOLEFormat := WordShape.OLEFormat;
                if not IsNull(WordOLEFormat) then
                    WordDocument.MailMerge.MailAsAttachment := WordDocument.MailMerge.MailAsAttachment or WordOLEFormat.DisplayAsIcon;
                if not IsNull(WordLinkFormat) then begin
                    WordLinkFormat.SavePictureWithDocument := true;
                    WordLinkFormat.BreakLink;
                    WordLinkFormat.Update;
                end;
                WordInlineShape := WordShape.ConvertToInlineShape;
            end;
        end;

        case TempDeliverySorter."Correspondence Type" of
            TempDeliverySorter."Correspondence Type"::Fax:
                begin
                    WordDocument.MailMerge.Destination := 3;
                    WordDocument.MailMerge.MailAddressFieldName := Text015;
                    WordDocument.MailMerge.MailAsAttachment := true;
                    WordHelper.CallMailMergeExecute(WordDocument);
                end;
            TempDeliverySorter."Correspondence Type"::Email:
                begin
                    WordDocument.MailMerge.Destination := 2;
                    WordDocument.MailMerge.MailAddressFieldName := Text015;
                    WordDocument.MailMerge.MailSubject := TempDeliverySorter.Subject;
                    WordDocument.MailMerge.MailAsAttachment :=
                      WordDocument.MailMerge.MailAsAttachment or TempDeliverySorter."Send Word Docs. as Attmt.";
                    WordHelper.CallMailMergeExecute(WordDocument);
                end;
            TempDeliverySorter."Correspondence Type"::"Hard Copy":
                begin
                    WordDocument.MailMerge.Destination := 0; // 0 = wdSendToNewDocument
                    WordHelper.CallMailMergeExecute(WordDocument);
                    WordHelper.CallPrintOut(WordHelper.GetActiveDocument(WordApplication));
                end;
        end;

        // Update delivery status on Interaction Log Entry
        if TempDeliverySorter.Find('-') then begin
            InteractLogEntry.LockTable;
            repeat
                with InteractLogEntry do begin
                    Get(TempDeliverySorter."No.");
                    "Delivery Status" := "Delivery Status"::" ";
                    Modify;
                end;
            until TempDeliverySorter.Next = 0;
            Commit;
        end;

        ParamBln := false;
        WordHelper.CallClose(WordDocument, ParamBln);
        if not Attachment."Merge Source".HasValue then
            AppendToMergeSource(MergeClientFileName);
        DeleteFile(MainFileName);
        DeleteFile(MergeClientFileName);

        if not IsNull(WordLinkFormat) then
            Clear(WordLinkFormat);
        if not IsNull(WordOLEFormat) then
            Clear(WordOLEFormat);
        Clear(WordMergefile);
        Clear(WordDocument);
    end;
    [IntegrationEvent(false, false)]
    local procedure OnBeforeAddFieldsToMergeSource(var TempSegLine: Record "Segment Line" temporary; var TempDeliverySorter: Record "Delivery Sorter" temporary);
    begin
    end;
bc-ghost commented 4 years ago

Thanks for reporting this. We agree, and we’ll publish a fix asap, either in an update for the current version or in the next major release. We will update this issue with information about availability.

bc-ghost commented 4 years ago

Thanks for reporting this. We agree, and we’ll publish a fix asap, either in an update for the current version or in the next major release. We will update this issue with information about availability.