Question about PrintOut() and Quit()

I

Igor

I have an application that prints documents in the background and quits. So
the code goes something like this:

wordApp.Visible == false;

.....
.....

mydoc.PrintOut( ........ )

Thread.Sleep(2000); // Temporary Fix

mydoc.Close(......)
wordApp.Quit(.......)

Now, I was running into this issue where certain documents that are large
were not being printed. I narrowed it down to the closing of Word immediately
after PrintOut(). When I took away the Close() and Quit(), all documents
printed fine but were leaving the winword process running in the background.
So as a tamporary solution, I have added a Thread.Sleep(2000) immediately
after PrintOut and it works fine. Would the best solution be to have the
PrintOut command in a worker thread? It also seems odd that it's closing the
document before it finishes printing. Any ideas on why this is happening?
Also, when I stepped through the code in debug mode, it would work fine.

Thanks,
Igor
 
J

Jay Freedman

The PrintOut method takes an optional argument, Background, which defaults to
True. That causes Word to do its spooling on a background thread while
continuing the macro execution with the next statement. If the next statement is
Close or Quit, you hit the problem you've seen. The solution is to explicitly
include Background:=False (or whatever syntax your external automation language
requires) to force Word to spool in the foreground and block until spooling
completes.

The problem with the Thread.Sleep solution is that you can't predict how long it
will take to spool any arbitrary document.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top