简体   繁体   English

使用VBA以Word格式另存为PDF会导致文档更改

[英]Saving as PDF in word using VBA causes changes to the document

I use the following code to quickly convert an active word document to PDF and save to desktop. 我使用以下代码将活动的Word文档快速转换为PDF并保存到桌面。 It works very well but when I go to exit the document immediately afterwards it always asks if I want to make changes, when none should have been made. 效果很好,但是当我之后立即退出文档时,它总是询问我是否要进行更改,何时不应该进行更改。

Is there a way I can prevent it making these invisible changes to the document so that it will just immediately quit (if it was saved just before the export, for example). 有没有一种方法可以防止它对文档进行这些无形的更改,以便使其立即退出(例如,如果它是在导出之前保存的)。

Sub PdfToDesktop()
'
'
Dim DeskTop As String
DeskTop = CreateObject("WScript.Shell").SpecialFolders("Desktop")

Dim fileNameOnly As String
Dim fileNameDot As Integer
fileNameDot = InStr(1, ActiveDocument.Name, ".")

fileNameOnly = Left(ActiveDocument.Name, fileNameDot - 1)

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        DeskTop & "\" & fileNameOnly & ".pdf", ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
        wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
        IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
        wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
        True, UseISO19005_1:=False
    ChangeFileOpenDirectory DeskTop
End Sub

Either have the code close the document with: 可以使用以下任一代码关闭文档:

ActiveDocument.Close SaveChanges:= False
End Sub

Or save the document again at the end of the code: 或在代码末尾再次保存文档:

ActiveDocument.Save
End Sub

Put either of these right at the end. 将其中任何一项放在最后。

This doesn't entirely answer your question, but will prevent the pop-up window from appearing when you close the document (either through code in the first method or manually later in the second). 这不能完全回答您的问题,但是可以防止关闭文档时弹出窗口出现(通过第一种方法中的代码或第二种方法中的手动)。

我不想保存文档或将其作为PDF生成的一部分关闭,因此我在代码的开头使用了这一行。 If ActiveDocument.Saved = True Then fileSaved = True来保存保存状态,然后在结束还原, If fileSaved = True Then ActiveDocument.Saved = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM