将Microsoft Word文档转换为Pdf的问题 [英] Issues Converting Microsoft Word Documents To Pdf
问题描述
我正在尝试将Microsoft Word文档转换为PDF,并且在测试过程中遇到了一些问题.目前,我要转换大约5个文档,然后删除转换后的PDF,然后再次运行转换程序以模拟实际环境 如果出现故障,我可能必须重新运行它.在第二次或以后的运行中,出现以下错误.
I am trying to convert Microsoft Word Documents to PDF and having some issues during testing. At the moment I am converting about 5 documents and then deleting the converted PDFs and running the conversion program again to simulate a live environment scenario where I may have to re-run it in case of a failure. During the second or subsequent runs, I am getting the following error.
有时会显示以下消息:
我想在任务调度程序上运行该程序,而无需任何用户干预.因此,我想避免所有这些错误.我不希望Word文档打开或抛出任何消息.我需要了解如何避免这些错误.我尝试了不同 在程序中关闭Word文档的方法,但没有成功.谁能帮忙吗?我在下面给出了我的代码:
I want to run this program off the task scheduler without any user intervention. Therefore I want to avoid all these errors. I don't want the word document to open or throw any messages. I need to understand how to avoid these errors. I have tried different methods to close the word documents in the program but without success. Can anyone please help? I have given my code below:
Imports System.IO
Imports Microsoft.Office.Interop.Word
Module Module1
Sub Main()
Dim WordApplication As New Microsoft.Office.Interop.Word.Application()
Dim WordDocument As Microsoft.Office.Interop.Word.Document
Dim Files As String() = Directory.GetFiles("D:\Convert", "*.doc*")
For Each File As String In Files
Try
WordDocument = WordApplication.Documents.Open(File)
Dim PdFile As String = Path.ChangeExtension(File, "pdf")
WordDocument.ExportAsFixedFormat(PdFile, WdExportFormat.wdExportFormatPDF)
Catch ex As Exception
My.Computer.FileSystem.WriteAllText("D:\Convert\Logfile.txt", ex.Message & vbCrLf, True)
End Try
Next
WordApplication.Quit()
WordApplication = Nothing
End Sub
End Module
感谢您的帮助.
Appreciate any help.
推荐答案
在第二次或以后的运行中,出现以下错误.
During the second or subsequent runs, I am getting the following error.
您尚未提供任何代码来关闭WORD文档.文档很可能仍处于打开状态,因为WORD会弹出一个对话框,以响应您正在使用的任何关闭"过程.您需要显示如何关闭文档,并且 解释为什么您相信WORD将始终在没有用户干预的情况下执行关闭.
You haven't included any code to close the WORD document. Most likely the document is still open because WORD has thrown up a dialog in response to whatever Close procedure you are using. You need to show how you are closing the document, and explain why you believe that WORD will always execute the Close without user intervention.
这篇关于将Microsoft Word文档转换为Pdf的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!