Business Central - Previewing when printing gives printername as empty string

124 views Asked by At

When going to the sales invoices page in business central, you can print an invoice and you will get the following popup:

enter image description here

Here you can select a printer in the printer dropdown, we then catch that printer name in the OnBeforeMergeDocument event, which gives you the printername as an argument.

The problem is, this only works when you press the print button at the bottom of the popup in the image, if you press preview, it will give you an empty string.

I need the printername in both situations, is it possible to get the printername when previewing?

1

There are 1 answers

1
Alexander Drogin On BEST ANSWER

Apparently it's not possible in OnBeforeMergeDocument. This event is raised from a procedure MergeWordLayout which is a bit weird. It does not have the printer name in its parameters.

MergeWordLayout(ReportID: Integer; ReportAction: Option SaveAsPdf,SaveAsWord,SaveAsExcel,Preview,Print,SaveAsHtml; InStrXmlData: InStream; FileName: Text; var DocumentStream: OutStream)

But it assigns the file name to the printer name before calling the event if the selected action is "Print".

        if ReportAction = ReportAction::Print then
            PrinterName := FileName;

So I would assume that the printer name is actually passed to the procedure in the FileName parameter. Anyway, this code is obsolete now. I don't know which version of BC you are using, but OnBeforeMergeDocument is replaced with 'OnCustomDocumentMerger' in version 20, and this one is in its turn replaced by OnCustomDocumentMergerEx in v.21. The latter has the printer info in the ObjectPayload object.

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-oncustomdocumentmergerex-event