When going to the sales invoices page in business central, you can print an invoice and you will get the following popup:
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?
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.But it assigns the file name to the printer name before calling the event if the selected action is "Print".
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 byOnCustomDocumentMergerEx
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