2

我遇到的问题是,我的公司模板集在每个 word 文档的页脚中使用了 SaveDate 字段 - 用于详细说明文档的保存时间,这与我们的自定义文档管理系统相关联。

随后,当用户想要制作旧文档的 PDF 时,使用 Office 2010 的另存为 PDF 功能,会更新保存日期 - 创建旧文档的 PDF,但使用今天的日期。这是错误的。我们只是试图为原始文档中的任何内容创建一个真正的 PDF 版本。

为了解决这个问题,我正在编写一个宏解决方案,它锁定字段,将文档导出为 PDF,然后再次解锁字段。

我遇到了一个问题,我可以识别和锁定页眉/页脚中的所有字段(这实际上是我正在尝试做的),但为了使其更健壮,需要找到一种方法来锁定所有字段所有部分。

在下面向您展示我的代码,我如何识别所有部分中的所有字段?这是否必须使用索引工具来完成?

Sub CPE_CustomPDFExport()

'20-02-2013

    'The function of this script is to export a PDF of the active document WITHOUT updating the fields.
    'This is to create a PDF of the document as it appears - to get around Microsoft Word 2010's native behaviour.

 'Route errors to the correct label
 'On Error GoTo errHandler

'This sub does the following:

    ' -1- Locks all fields in the specified ranges of the document.
    ' -2- Exports the document as a PDF with various arguments.
    ' -3- Unlocks all fields in the specified ranges again.
    ' -4- Opens up the PDF file to show the user that the PDF has been generated.

        'Lock document fields
        Call CPE_LockFields

        'Export as PDF and open afterwards
        Call CPE_ExportAsPDF

        'Unlock document fields
        Call CPE_UnlockFields

'errHandler:
 ' MsgBox "Error" & Str(Err) & ": " &

End Sub
Sub CPE_LockFields()

   'Update MS Word status bar
        Application.StatusBar = "Saving document as PDF. Please wait..."

   'Update MS Word status bar
        Application.StatusBar = "Locking fields in all section of the active document..."

   'Declare a variable we can use to iterate through sections of the active document
        Dim docSec As section

   'Loop through all document sections and lock fields in the specified ranges
        For Each docSec In ActiveDocument.Sections
             docSec.Footers(wdHeaderFooterFirstPage).Range.fields.Locked = True
             docSec.Footers(wdHeaderFooterPrimary).Range.fields.Locked = True
             docSec.Footers(wdHeaderFooterEvenPages).Range.fields.Locked = True
        Next

End Sub
Sub CPE_UnlockFields()

   'Update MS Word status bar
        Application.StatusBar = "PDF saved to DocMan Temp. Now unlocking fields in active document. Please wait..."

   'Declare a variable we can use to iterate through sections of the active document
        Dim docSec As section

   'Loop through all document sections and unlock fields in the specified ranges
        For Each docSec In ActiveDocument.Sections
                  docSec.Footers(wdHeaderFooterFirstPage).Range.fields.Locked = False
                  docSec.Footers(wdHeaderFooterPrimary).Range.fields.Locked = False
                  docSec.Footers(wdHeaderFooterEvenPages).Range.fields.Locked = False
        Next

End Sub
Sub CPE_ExportAsPDF()

    'Update MS Word status bar
    Application.StatusBar = "Saving document as PDF. Please wait..."

    'Chop up the filename so that we can remove the file extension (identified by everything right of the first dot)
    Dim adFilename As String
    adFilename = Left(ActiveDocument.FullName, (InStrRev(ActiveDocument.FullName, ".", -1, vbTextCompare) - 1)) & ".pdf"

     'Export to PDF with various arguments (here we specify file name, opening after export and exporting with bookmarks)
        With ActiveDocument

                    .ExportAsFixedFormat outPutFileName:=adFilename, _
                    ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, _
                    OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
                    Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
                    CreateBookmarks:=wdExportCreateWordBookmarks, DocStructureTags:=True, _
                    BitmapMissingFonts:=True, UseISO19005_1:=False

        End With

        'Update MS Word status bar
        Application.StatusBar = "PDF saved to DocMan Temp."

End Sub
4

2 回答 2

0

尝试以下操作以获取文档、页眉、页脚、背景和正文中的所有字段:

Sub LockAllFieldsInDocument(poDoc As Document, Optional pbLock As Boolean = True)
    Dim oRange As Range

    If Not poDoc Is Nothing Then
        For Each oRange In poDoc.StoryRanges
            oRange.Fields.Locked = pbLock
        Next
    End If

    Set oRange = Nothing
End Sub
于 2013-02-20T13:50:31.107 回答
0

这是另一种方法。它会选择整个文档,然后锁定所有字段,然后再取消选择所有内容。

Sub SelectUnlink()
    ActiveDocument.Range(0, 0).Select
    Selection.WholeStory
    Selection.Range.Fields.Unlink
    Selection.End = Selection.Start
End Sub
于 2019-04-26T13:14:13.333 回答