C# Printout method

V

Virgul

Hi,

I try in my winform to open a word document print it and close.

But I have some problem when the document is to big word dont print it.

I think it's because I close it before that he have the time to complete
open it and launch the command to print.

I have put Thread.Sleep(5000); for wait a moment and that work!

But I dont to make wait my programm if they dont need to wait and if I have
a larger document that will not work so my question is what I need to make
for that my word document is print before close it?

My Code:

Word.Application newApp = new Word.Application();
Word.Document newdoc;
private void printWord(string ctp, string Temp)
{
newApp = new Word.Application();
string filePath = temp + "\\form" + cpt + ".pnet";

object o = System.Reflection.Missing.Value;
object True = true;
object Source = filePath;
newApp.Visible = false;

newdoc = newApp.Documents.Open(ref Source, ref o, ref o, ref o, ref o, ref
o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);

newdoc.PrintOut(ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref
o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);

Thread.Sleep(5000);

if (SaveEhra == "1")
{
object Source2 = temp + "\\EHRA.xml";

newdoc.XMLSaveDataOnly = true;
newdoc.SaveAs(ref Source2, ref o, ref o, ref o, ref o, ref o, ref o,
ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
}

newdoc.Close(ref o, ref o, ref o);
object Savequit = false;

newApp.Quit(ref Savequit, ref o, ref o);
}

Have you any idea about what I'm doing wrong?

Thanks a lot for your help

++

Thierry
 
J

Jay Freedman

The first parameter of the PrintOut method is Background, which controls
whether Word uses a background thread to send the document to the
printer/spooler. The default value of the parameter is True, which allows
the program to continue while the document prints. That's why the document
closes before printing is complete.

You need to send the value False, which forces Word to print in the
foreground thread and not continue your code until printing is complete.
With that in place, you can remove the Sleep call.

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

Virgul

Thanks Jay!

I have read all other properties execpt this one beacuase I was thinking
that is for for print background picture...

++

Thierry
 

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