【问题标题】:How can I use VBA to lock/unlock all fields in a Microsoft Word 2010 document?如何使用 VBA 锁定/解锁 Microsoft Word 2010 文档中的所有字段?
【发布时间】:2013-02-20 13:14:34
【问题描述】:

我遇到的问题是我的公司模板集在每个 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

【问题讨论】:

  • 也许我理解错了。让我回到你身边
  • 非常感谢,如果您有时间,不胜感激
  • 我正在做一些测试,完成后会回复:)

标签: vba ms-office ms-word


【解决方案1】:

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

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

【讨论】:

  • 我正在尝试这个 - 到目前为止它实际上看起来像是一个有效的解决方案 - 似乎可以捕获所有内容。只是做一些测试,所以我会让你知道我的表现!
  • 试图将它与 Sub AutoNew() 一起用于模板 - 但如果模板有多个字段,它就不起作用,不知道为什么..
【解决方案2】:

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

Sub SelectUnlink()
    ActiveDocument.Range(0, 0).Select
    Selection.WholeStory
    Selection.Range.Fields.Unlink
    Selection.End = Selection.Start
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    相关资源
    最近更新 更多