0

Im saving (generated) Word documents as file via by printing them into a file (i have to do it like this because the file is required from legacy programms)

The problem is if i do this for multiple files, every second file is not written correctly.

  • The first file is ok.
  • 2nd is only written about 80% of the file.
  • 3rd is ok
  • 4th is the same as the 2nd (exactly the same filesize as the 2nd)
  • ... and so on

This is my code.

public static void main(String args[]) {
    Variant background = new Variant(false);        
    Variant append = new Variant(false);        
    Variant range = new Variant(0);//wdPrintAllDocument 

    ActiveXComponent oleComponent = new ActiveXComponent("Word.Application");       
    Variant var = Dispatch.get(oleComponent, "Documents");
    Dispatch document = var.getDispatch();
    Dispatch doc = Dispatch.call(document, "Open", "c:/temp/Test.rtf").toDispatch();

    for (int i = 0; i < 10; i++) {
        Dispatch.call(doc, "PrintOut", background, append, range, new Variant("c:/temp/test" + i));
    }

    Dispatch.call(doc, "Close", 0);
    Dispatch.call(oleComponent, "Quit");
}

The problem appears on different printers (the pdf printer for example works)

why every 2nd file? word problem? printer (driver) problem?

help is very much appreciated

griFlo
  • 2,084
  • 18
  • 28
  • Are you really printing the same document 10 times or is this a reduced sample? If it is the latter, please add what else is happening inside the loop body. – Dirk Vollmar Sep 14 '16 at 08:41
  • @DirkVollmar With the code in the question i can reproduce the problem. So this is the complete code for the problem. But i found a solution/workaround. If i set the printer to empty and then back to the original printer, it works – griFlo Sep 15 '16 at 09:35

0 Answers0