views:

764

answers:

4

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;
            }
        }
+1  A: 

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.

JaredPar
The MessageBox showed the correct filename... I think the filename is getting through correct.
jle
@jle, but are both getting through?
JaredPar
yea, I post the messagebox after filename = filesToCheck[i]; in the loop
jle
+2  A: 

try use

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

instead of

for (int i = 0; i < filesToCheck.Count(); i++ ) { }
abatishchev
it printed, but I got an index out of range error
jle
The code shows that this is a standard C# string array. That would cause an ArgumentOutOfRangeException in this case. That would be the way to go if the collection were coming from the Office COM APIs since they are 1-based.
Dustin Campbell
Sorry, I meant an IndexOutOfRangeException.
Dustin Campbell
You wouldn't access the array using index at all. Remember to change every "filesToCheck[i]" to "file".
Guffa
A: 

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

+3  A: 

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.

devio
the thought had not even crossed my mind. Amazing. Thank you much.
jle