如何使用 VB 脚本完全关闭 Word 应用程序

How to Completely Close Word application using VB script

我在 word 中有一个宏/脚本可以将 docx 文件转换为 PDF;文件转换后,我只想简单地关闭应用程序一词(宏)。转换部分运行良好,但宏仍然保持打开状态。 …一旦所有文件都转换完毕,我将如何关闭宏/MS word 应用程序?

脚本:

Sub BatchConvertDocxToPDF()
  Dim objDoc As Document
  Dim strFile As String, strFolder As String
 
  'Initialization
  strFolder = "C:\Users\Public\ConvertedFiles\"
  strFile = Dir(strFolder & "*.docx", vbNormal)
 
  'Precess each file in the file folder and convert them to pdf.
  While strFile <> ""
    Set objDoc = Documents.Open(FileName:=strFolder & strFile)
 
    objDoc.ExportAsFixedFormat _
      OutputFileName:=Replace(objDoc.FullName, ".docx", ".pdf"), _
      ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, _
      Range:=wdExportAllDocument, Item:=wdExportDocumentContent
 
    objDoc.Close
    strFile = Dir()
  Wend
  
  'close word
  Set AppWord = CreateObject("Word.Application")
  AppWord.Visible = True
  AppWord.Application.Quit

End Sub

提前感谢您的帮助

简单使用

Application.Quit

而不是

Set AppWord = CreateObject("Word.Application")
AppWord.Visible = True
AppWord.Application.Quit

这个位创建一个新的Word实例只是为了立即关闭它,这是完全没有用的。