Form Fields Reset

E

Eric

I have a template with several Form Fields, REF Text and a TOC. I have a
macro that updates all Fields on the tool bar that allows users to update
fields when the Doc is protected. But the Form fields get reset when the
user uses this macro. Please help.

Sub UpdateFields()

'
'
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub
 
M

macropod

Hi Eric,

The formfields are being reset because the code doesn't look to see what type of field it is beforehand.

Provided you set your formfield properties to set a bookmark value and 'calculate on exit', any REF fields & formfields dependent on
them should update automatically. That would only leave fields like Tables Of Contents, Authorities and Figures, which would only
need updating if the data going into the formfields is liable to change a heading's text or which page a given heading/reference
appears on. For that you could use a sub like:

Sub RefreshDocTables()
Dim TOC As TableOfContents ' Table of Contents Object
Dim TOA As TableOfAuthorities ' Table of Authorities Object
Dim TOF As TableOfFigures ' Table of Figures Object
Dim pRange As Word.Range ' Word Range Object
Dim strPwd As String ' Password variable
Dim pState As Boolean ' Protection State
With ActiveDocument
If .ProtectionType = wdAllowOnlyFormFields Then
pState = True
strPwd = InputBox("Please input the Password", "Document Protected for Forms")
.Unprotect (strPwd)
End If
' The following routines update the TOC, TOA or TOF contents.
' Loop through Tables Of Contents and update
For Each TOC In .TablesOfContents
TOC.Update
Next
' Loop through Tables Of Authorities and update
For Each TOA In .TablesOfAuthorities
TOA.Update
Next
' Loop through Tables Of Figures and update
For Each TOF In .TablesOfFigures
TOF.Update
Next
If pState = True Then .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=strPwd
pState = False
End With
End Sub
 
E

Eric

Thanks Macro....

If I didn't need a password to unprotect etc. This is what I have done.

strPwd = ""


Sub RefreshDocTables()
Dim TOC As TableOfContents ' Table of Contents Object
Dim TOA As TableOfAuthorities ' Table of Authorities Object
Dim TOF As TableOfFigures ' Table of Figures Object
Dim pRange As Word.Range ' Word Range Object
Dim strPwd As String ' Password variable
Dim pState As Boolean ' Protection State
With ActiveDocument
If .ProtectionType = wdAllowOnlyFormFields Then
pState = True
strPwd = ""
..Unprotect (strPwd)
End If
' The following routines update the TOC, TOA or TOF contents.
' Loop through Tables Of Contents and update
For Each TOC In .TablesOfContents
TOC.Update
Next
' Loop through Tables Of Authorities and update
For Each TOA In .TablesOfAuthorities
TOA.Update
Next
' Loop through Tables Of Figures and update
For Each TOF In .TablesOfFigures
TOF.Update
Next
If pState = True Then .Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:=strPwd
pState = False
End With
End Sub

I tried it and it seems to work...... I just would like another set of eyes
on it.

Your awesome.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top