How to set up Printer by JACOB to print word document?

Fonlin 0 Reputation points
2023-03-02T09:08:23.7633333+00:00

Background

We need to specify a printer to print word doc by java(jacob), and this may happen in a multithreaded environment.

Code

At the beginning we use this method but concurrency problems occurred. When two threads execute this code at the same time, the printer of the two print tasks will be mixed.

word.setProperty("ActivePrinter", new Variant(printerName));

So we found another way to achieve it with FilePrintSetup, in this article. https://support.microsoft.com/en-US/topic/activeprinter-property-in-word-sets-system-default-printer-4b4afb8f-cbc1-1187-14cc-a97fdd0f596e

Dispatch wordBasic = Dispatch.call(word, "WordBasic").toDispatch();
Dispatch.call(wordBasic, "FilePrintSetup", new Variant(printerName), new Variant(false));

But when we actually use it, the second parameter can only pass false, because when true is passed, there will be a UI dailog of the printer to block the continued execution of the program, as shown in the figure below.

User's image

When pass false, the same concurrency problems still occur. Is there any good solution? Thank you

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,509 questions
0 comments No comments
{count} votes