[英]VBA Close Word document after opening and not saving
現在使用正確的代碼
Sub pdf()
Dim WordApp As Object
Dim WordDoc As Object
Set WordApp = CreateObject("Word.Application")
With WordApp.Application
.Visible = True
Set WordDoc = .Documents.Open("C:\testdoc.docx")
WordDoc.ExportAsFixedFormat OutputFileName:= _
"C:\testdoc.docx.pdf", ExportFormat:= _
17, OpenAfterExport:=False, OptimizeFor:= _
0, Range:=0, From:=1, to:=1, _
Item:=0, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=0, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
'WordDoc.Application.Quit
Set WordDoc = Nothing
End With
Set WordApp = Nothing
End Sub
您可以在Set WordDoc = Nothing
之前嘗試WordDoc.Close SaveChanges:=wdDoNotSaveChanges
此外,如果您想完全退出程序,您確實應該在將變量設置為Nothing
之前發送命令退出它:
WordApp.Quit SaveChanges:=wdDoNotSaveChanges
Quit
和Close
方法的其他選項可以在文檔中找到: Application.Quit ; 文檔。關閉。
您的最終代碼可能如下所示:
Sub pdf()
Dim WordApp As Object
Dim WordDoc As Object
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open("C:\testdoc.docx")
WordDoc.ExportAsFixedFormat OutputFileName:= _
"C:\testdoc.docx.pdf", ExportFormat:= _
17, OpenAfterExport:=False, OptimizeFor:= _
0, Range:=0, From:=1, to:=1, _
Item:=0, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=0, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
'If you want to quit the entire Application:
WordApp.Quit SaveChanges:=wdDoNotSaveChanges
'If you want to quit only the Document:
'WordDoc.Close SaveChanges:=wdDoNotSaveChanges
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.