2
votes

The problem I have got is that my corporate template set uses a SaveDate field in the footer of every word document - which is used to detail when the document was saved, which ties in with our custom document management system.

Subsequently, when users want to make a PDF of an old document, using the Save As PDF function of Office 2010, the Save Date is updated - creating a PDF of the old document, but with today's date. This is wrong. We are just trying to create a true PDF version of whatever the original document has in it.

To get around this, I am writing a macro solution which locks the fields, exports the document as a PDF and then unlocks the fields again.

I have come up against an issue where I can identify and lock all fields in the headers/footers (which is actually what I'm trying to do) but to make it more robust, need to find out a way to lock ALL FIELDS in ALL SECTIONS.

Showing you my code below, how can I identify all fields in all sections? Will this have to be done using the Index facility?

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
2
Maybe I misunderstood. Let me come back to you on thisSiddharth Rout
many thanks, appreciated if you have the timeuser1064180
I am doing some tests and will post back once they complete :)Siddharth Rout

2 Answers

0
votes

Try something like the following to get to all fields in the document, header, footer, background and main text:

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
0
votes

Here is another way to do it. It'll select the entire document and then lock all fields, before deselecting everything.

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