簡體   English   中英

VBA 打開后關閉Word文檔而不保存

[英]VBA Close Word document after opening and not saving

我編寫了一段宏腳本,用於打開 Word 文檔並將其導出到 PDF。 我怎樣才能添加一個關閉word文檔而不保存的片段? 請記住,Word 文檔必須打開,因為文檔中有需要刷新的字段

在此處輸入圖像描述

任何治療請謝謝

現在使用正確的代碼

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

QuitClose方法的其他選項可以在文檔中找到: 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM