以编程方式打印Word文档的目录...总是跳过最后一个文件
我在尝试以编程方式打印 Word 文档目录时遇到问题。 在此示例中,我尝试仅打印带有“3_”前缀的文件。 问题是,除非有两个带有 3_ 前缀的文件,否则该文件不会打印。 我一直在四处寻找以解决这个问题。 我打开文件的方式有问题吗? 仅当目录中有两个文件时它才起作用,在这种情况下它只会打印出两个文件之一。
编辑:我确实尝试了消息框,路径是正确的。 文件名正确。 另外,如果我正在查看打印机文件夹中的打印机,文档将闪烁一秒钟,然后消失(我已暂停打印,以便可以看到输出)。 如果 word 给我一个错误,为什么它不显示? 如果目录中有两个带有 3_ 前缀的文件,为什么这会起作用?
编辑:我认为这是 printout() 方法的问题。 当我将应用程序设置为可见并运行它时,文档可以正常打开,但没有打印任何内容。 我可以手动打开文档并打印(效果很好)。
编辑:谢谢大家的回答。 printout() 方法中的背景参数是问题所在。 该程序将在打印完全假脱机之前退出(这就是为什么我会看到文档在打印队列中闪烁并消失。关闭后台打印需要文档保持打开状态并打印,这是关键。谢谢
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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试设置 PrintOut( ) 调用 False。
由于 Word COM 对象释放得太早,最后一个打印作业可能未完全假脱机并取消。
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.
尝试使用
而不是
try use
instead of
您发布的算法似乎没有任何问题。 您可以尝试以下
另外,您可以发布获取 filesToCheck 对象的代码吗?
编辑
OP提到所有文件肯定都会通过。 尝试将打印操作包装在 try/catch 块中,并查看是否抛出任何阻止文件打印的异常。
There doesn't appear to be anything wrong with your posted algorithm. Can you try the following
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.