I've had a few users complain about opening docs with cross ref/table of content links. When we recieve docs with them, the below code checks them and goes nuts and tries to repaginate and gets stuck in a loop, till the doc crashes. The code below updates fields except 'formtextinput and 'dropdown' as the update code refreshes/blanks them, which is a nightmare for forms that are submitted to us from other staff.
Is there a way to omit table of content / cross references but still update fieldcodes such as file path fields inputted into a footer?
Code:
Sub UpdateDocumentFooter()
''SECOND ATTEMPT CODE
Dim iCount As Integer, DocProtect As Boolean
'~~> If protected, unprotect document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
DocProtect = True
End If
'~~> Prevent Screen Flickering
Application.ScreenUpdating = False
'~~> move selection to end of document
Selection.EndKey wdStory
ActiveDocument.Sections(ActiveDocument.Sections.Count) _
.Footers(1).Range.Fields.Update
Application.ScreenUpdating = True
'~~> If document was originally protected, then re-protect
If DocProtect = True Then
ActiveDocument.Protect Password:="", NoReset:=True, _
Type:=wdAllowOnlyFormFields
End If
End Sub
Sub UpdateDocFields()
Dim iCount As Integer
Dim aField As Field
Dim DocProtect As Boolean
'On Error GoTo ErrHandler
' If protected; unprotect document
If ActiveDocument.ProtectionType <> wdNoProtection Then
DocProtect = True
ActiveDocument.Unprotect
End If
' Loop through all text form fields in the document.
For Each aField In ActiveDocument.Fields
'If the form field is a text form OR drop down field...
If aField.Type = wdFieldFormTextInput Or aField.Type = wdFieldFormDropDown Then
'do nothing with FormText field
'MsgBox "Field not updated!" 'TEST MESSAGE ONLY - REMOVE FROM LIVE
Else
aField.Update
'MsgBox "Field updated!" 'TEST MESSAGE ONLY - REMOVE FROM LIVE
End If
' Increment icount
iCount = iCount + 1
Next aField
'if document was originally protected, then re-protect
If DocProtect = True Then
ActiveDocument.Protect Password:="", NoReset:=True, Type:=wdAllowOnlyFormFields
End If
'ErrHandler:
End Sub
*Edit
Forgot to mention the ToC's that cause the issue are from docs of quite some size, 15 pages or more.
I couldn't upload my normal(template) Darn Work restrictions. but just running the above script will lock the attached.
I've recreated the doc I initially had problems with, in doing so helped me find out why the issue is happening, We recieve alot of document like this, Where hyper links aren't working or the TOC is amended to suit the author and not the dynamics of the field updates.
Not to Worry Sid, I cleaned up our global template, and instructed our secretaries to avoid this kind of issue, so all is well. Thanks for your input anyways