以编程方式打印Word文档的目录...总是跳过最后一个文件

发布于 2024-07-15 17:57:15 字数 1971 浏览 4 评论 0原文

我在尝试以编程方式打印 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城花音 2024-07-22 17:57:15

尝试设置 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.

酒与心事 2024-07-22 17:57:15

尝试使用

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

而不是

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

try use

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

instead of

for (int i = 0; i < filesToCheck.Count(); i++ ) { }
迷离° 2024-07-22 17:57:15

您发布的算法似乎没有任何问题。 您可以尝试以下

  1. 添加断点并确保 filesToCheck 对象中包含所有文件名吗? 该值可能不正确,并且会影响您的算法。
  2. 不要打印文档,而是更改代码以针对每个文件名显示一个消息框。 这将消除实际打印问题的一部分(不明白它是怎么回事)。

另外,您可以发布获取 filesToCheck 对象的代码吗?

编辑

OP提到所有文件肯定都会通过。 尝试将打印操作包装在 try/catch 块中,并查看是否抛出任何阻止​​文件打印的异常。

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.

为人所爱 2024-07-22 17:57:15

尝试不要在文件名中使用“_”。 我以前见过那个充电器和打印输出的一些奇怪的怪癖。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文