【问题标题】:Saving as PDF in word using VBA causes changes to the document使用 VBA 在 word 中另存为 PDF 会导致文档发生更改
【发布时间】:2015-02-20 16:41:24
【问题描述】:

我使用以下代码快速将活动 Word 文档转换为 PDF 并保存到桌面。它工作得很好,但是当我之后立即退出文档时,它总是询问我是否要进行更改,什么时候不应该进行。

有没有办法阻止它对文档进行这些不可见的更改,以便它立即退出(例如,如果它是在导出之前保存的)。

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

【问题讨论】:

  • 如果您添加ActiveDocument.Saved = True,则不会提示您保存更改(但请注意,如果您在运行导出宏之前进行了其他更改,那么如果您不保存,这些更改也可能会丢失关闭...)

标签: vba pdf ms-word


【解决方案1】:

要么让代码关闭文档:

ActiveDocument.Close SaveChanges:= False
End Sub

或者在代码末尾再次保存文档:

ActiveDocument.Save
End Sub

将其中任何一个放在最后。

这并不能完全回答您的问题,但会在您关闭文档时阻止弹出窗口出现(通过第一种方法中的代码或稍后在第二种方法中手动)。

【讨论】:

    【解决方案2】:

    我不想保存文档或将其作为 PDF 生成的一部分关闭,所以我在代码 If ActiveDocument.Saved = True Then fileSaved = True 的开头使用这一行来存储保存状态,然后在末尾使用这一行来恢复那If fileSaved = True Then ActiveDocument.Saved = True

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多