[RESOLVED] Footer field update...
Hi, Yep it's me again... sorry
We've had a few issues with different docs that contain fields, and those fields becoming blank after our previous code updates, so we wrote a new code that omits some drops down and input fields, so it doesn't update all.
The only prob is, the below code now update the fields but not the one we originally set out to update, The footer field. I've been at it a few hours now and can't seem to get it to work without it updating all fields, which we don't want to do. any help would be greatly appreciated.
Code:
Sub UpdateDocFields()
Dim iCount As Integer
Dim aField As Field
Dim DocProtect As Boolean
' 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
ActiveDocument.StoryRanges(wdPrimaryFooterStory).Fields.Update
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
End Sub
Re: Footer field update...
Shouldn't you be updating the footer field like this?
Code:
ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers(1).Range.Fields.Update
Re: Footer field update...
Hi Sid,
Tried that one, and still no joy. I thought it was a bugged field code, as I F8'ed it and it ran but just didnt update with the new file path. do you think it might be getting confused with afield.update and the activedoc...update?
Re: Footer field update...
Let me cook some sample code for you...
Edit:
Try this...
Code:
'~~> Updates only fields in footer and no other fileds
Sub UpdateDocumentFooter()
Dim DocProtect As Boolean
'~~> If protected, unprotect document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
DocProtect = True
End If
'~~> Prevent Screen Flickering
Application.ScreenUpdating = False
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
Re: Footer field update...
As always Sid your a Don, Thanks for helping
Although I've tested the above, but same result, no change to the footer. I remmed out the old code and just tried the above, but with no luck.
Re: Footer field update...
Try this...
Code:
'~~> In some versions of Word you might have to move the selection
'~~> to the section you're updating
'~~> Updates only fields in footer and no other fileds
Sub UpdateDocumentFooter()
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
If it still doesn't work then upload your file. Lemme have a look at it...
Re: Footer field update...
Hi Sid, Sorry my mistake, I started a fresh Normal.dot and the first and second codes you submitted worked fine, the normal I'm updating has been through the wars, updated and built apone for more then 4 years now.. needs a good clean up!
Thanks again...
Re: [RESOLVED] Footer field update...
lolzzz
You are welcome... :wave: