views:

14

answers:

1

I am using a virtual printer to print a word document into an image file in a C# program. So far everything is going fine except that I don't know when the printing process is finished so I can read the content of the generated image. Here's my code :

using System;
using Microsoft.Office.Interop.Word;
using Word=Microsoft.Office.Interop.Word;

var app = new ApplicationClass();
object filename = "C:\\ad.doc";
var missing = Type.Missing;
var doc = app.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                var oldPrinter = app.ActivePrinter;
                app.ActivePrinter = "Name of printer";
                object outputFileName = "c:\\ad.tif";
                object trueValue = true;
                object falseValue = false;

                doc.PrintOut(ref trueValue, ref falseValue, ref missing, ref outputFileName, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing, ref trueValue, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing, ref missing);


app.ActivePrinter=oldPrinter ;                
doc.Close(ref missing, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing);

Then how can I be sure that the print processing is finished so I can continue and get the image content?

+1  A: 

Unfortunately, about the only way I've found to check for printing status in word is one of two things.

1) Print synchronously. Not great though because it'll can hang you till the printing is complete. 2) Print the doc asynchronously, and then check the APPLICATION.BACKGROUNDPRINTINGSTATUS property in a loop or on a background worker thread continuously until it becomes 0 (no longer printing) or you hit a watchdog timeout

Something like this....

        Do Until _Doc.Application.BackgroundPrintingStatus = 0
            System.Windows.Forms.Application.DoEvents()
            System.Threading.Thread.Sleep(750)
        Loop

Not perfect, but it works.

Note that this will only tell you when its finished spooling from word. If you're talking about knowing when the document is actually completed PRINTING on the printer, that's a whole other issue. You'll need the print jobID and have to query the printer spooler stuff, which I couldn't help you on.

drventure