Question

I am having a problem when trying to programmatically print a directory of word documents. In this example, I am trying to print only the files with the "3_" prefix. The problem is that the file does not print unless there are two files with the 3_ prefix. I have been looking around forever to figure this problem out. Is there something wrong with the way I am opening the file? It works only when there are two files in the directory, in which case it will print out only one of the two files.

Edit: I did try a messagebox and the path is correct. The filename is correct. Also, if I am watching the printer in the printers folder, a document will flash up for a brief second and then disappear ( I have printing paused so that I can see the output). If word is giving me an error, why doesn't it show? And why does this work if there are two files in the directory with the 3_ prefix?

Edit: I think it is a problem with the printout() method. When I set the app to visible and run it, the document opens fine, but nothing is printed. I can open the document manually and print (which works fine).

Edit: Thank you all for the answers. The background parameter in the printout() method was the issue. The program would quit before printing could fully spool (which is why I would see a document flash in the print queue and disappear. Turning background printing off required the document to stay open and print, which was key. Thank you

string[] filesToCheck = Directory.GetFiles(clientDirectoryPath);
    Object filename = null;
        for (int i = 0; i < filesToCheck.Count();i++ )
        {
            if(filesToCheck[i].Contains("3_"))
            {
                filename = filesToCheck[i];
                wrdDoc = wrdApp.Documents.Open(ref filename, ref oMissing, ref oTrue, ref oFalse,
                                               ref oMissing, ref oMissing,
                                               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                               ref oMissing,
                                               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                               ref oMissing);
                wrdDoc.PageSetup.FirstPageTray = letterHeadTray;
                wrdDoc.PageSetup.OtherPagesTray = defaultTray;
                wrdDoc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                ref oMissing, ref oMissing, ref oMissing);
                wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
                wrdDoc = null;
            }
        }
Was it helpful?

Solution

Try and set the Background parameter (1st param) of the PrintOut() call to False.

Probably the last print job is not completely spooled and canceled since the Word COM object is released too early.

OTHER TIPS

try use

string[] files = Directory.GetFiles(dir, "3_*.doc");
foreach(string file in files) { }

instead of

for (int i = 0; i < filesToCheck.Count(); i++ ) { }

There doesn't appear to be anything wrong with your posted algorithm. Can you try the following

  1. Add a breakpoint and make sure the filesToCheck object has all of the file names in it? It's possible this value is incorrect and throwing off your algorithm
  2. Instead of printing the document, instead change the code to throw up a message box for each file name. That will eliminate the actual printing being part of the problem (don't see how it could be).

Also, could you post the code that gets the filesToCheck object?

EDIT

OP mentioned that all of the files are definitely getting through. Try wrapping the print operation in a try/catch block and see if there are any exceptions being thrown that are preventing the files from being printed.

Try not using an "_" in the name of the file. I've seen some strange quirks before with that charger and PrintOut.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top