MS Word 在后台运行并请求保存文档,即使它已经保存

MS Word runs on background and requests documents to be saved even though it is already saved

我有一个根据 ms word 模板创建 PDF 文件的过程,它的数据是从数据库中检索的。

它工作正常,完美地创建了一个 PDF 文件,没有 运行 时间错误。问题是每当我关闭计算机时,ms word 都会阻止关机,如果我按取消,ms word 会显示一条消息;

代码是这样的;

   Dim wordApp As Word.Application
   Dim templateBookmarks As Word.Bookmarks
   Dim templateName As String
   Dim template As Word.Document
   'Some other variables for computations

   wordApp = CreateObject("Word.Application")
   sourceTable = New DataTable
   'Some other procs to fill the data table

   templateName = "Foo Template.docx" 
   template = wordApp.Documents.Add(templatePath & templateName)
   templateBookmarks = template.Bookmarks
   templateBookmarks.Item("sample bookmark").Range.Text = "foo"

   'Then fills the table in the template like...
   template.Tables(1).Cell(1, 1).Range.Text = dataSource.Rows(0).Item(0)

  'Then saves the document as a pdf
  Dim saveName As String = "sample file"
  template.SaveAs2(savePath & saveName, Word.WdSaveFormat.wdFormatPDF)

我已经尝试对单词 COM 资源强制进行垃圾回收,并将模板从实际文档(即 docx)更改为单词模板 .dotx。我还尝试了 Quit() 方法,但它只更早地显示 ms 字消息。这是我第一次需要使用互操作,如果我对此不太了解,请原谅。

我需要的文件已保存,唯一的问题是ms word消息和未保存和不需要的文件,例如Document1、Document2、Document3 似乎是在所需的 PDF 之外创建的

使用Document.Close方法在使用PDF文件格式保存文件后关闭指定文档。它允许指定 SaveChanges 参数,该参数指定文档的保存操作。可以是以下 WdSaveOptions 常量之一:wdDoNotSaveChangeswdPromptToSaveChangeswdSaveChanges.

On Error GoTo errorHandler 

ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

errorHandler: 
If Err = 4198 Then MsgBox "Document was not closed"