Simple 'Update Field' Macro not working on a machine

L

Laphan

Hi All

Wondered if you could help.

A few months ago my friend wanted me to look at how I could cut down on the
amount of copying and pasting he has to do in a Word doc.

Basically he's a football scout for a amateur/semi-pro football team and he
creates a really long report per match on how the set plays go. Now the way
he's got it laid out is great, but he had to copy and paste the players'
names and numbers about 50 or so times into each set play.

What I did was make him enter the player names and numbers once at the top
of the Word doc using form fields (is that right - I mean the grey boxes :0)
and then I created reference/field tags in all of the other places so that
when he typed the name for player 1 then it would auto-type this name in all
of the other references. This worked great until I found that textboxes
don't update automatically so you have to either click into each textbox and
press F9 to update them or do I select all (still flaky and prone to foul
ups!) and press F9.

My solution to this was to use the following Macro code and tag it to a
button, which resides just under the grey boxes form:

Dim oField As Field, oStory As Range

ActiveDocument.Unprotect

For Each oStory In ActiveDocument.StoryRanges
Do
For Each oField In oStory.Fields
If oField.Type = wdFieldRef Then
oField.Update
End If
Next oField
Set oStory = oStory.Next
Loop Until oStory Is Nothing
Next oStory

MsgBox "Report populated"

This routine simply unlocked the form and then 'touched' each appropriate
textbox/field to cause the update. The final bit just brought up a message
box so that the user would know that all is done.

I saved the Word Doc as a template file and then let him use it. It saved
him ages and proved so successful that his mate started to use it as well.
Then after a few months something went wrong! It still works fine on his
WinXP SP2 laptop with Office 2003 SP2 and AVG 8 as his AV, but his mate's
laptop has stop updating. His mate has the exact same laptop as when it was
working, ie Vista Home with Office 2003, but I've noticed that he has
Virgin's PCGuard on it and his Office 2003 is now running as SP3.

The table type fields still populate OK on his laptop, but it's as if the
field update routine above isn't doing what it used to. I know the data is
there, because if I go into a field and press F9 it updates.

NOTE 1: both my friend and his mate both have Macro security set to Low as
this is the only Word doc they use on their computers. Sad I know but they
do have a combined age of 116!!

NOTE 2: I've tried running the Macro whilst it appears that Virgin's PC
guard is completely quit, but it still won't populate.

My questions are:

1) Is there a better way to get the textboxes to 'update' than the above
routine, as I'm more than willing to try something else as it does take a
good few mintues to complete even on a good spec dual-core machine?

2) Have you got any ideas why this doesn't work any more? Does it have
anything to do with SP3?

Thanks

Laphan
 
D

Doug Robbins - Word MVP

What happens if you do a Print Preview (assuming that the option to Update
Fields on Print is set)?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Graham Mayor

The problem revolves around the fact that text boxes are not in the text
layer of the document. If you replace the text boxes with frames or table
cells, then the REF fields they contain will be updated by the simple
expedient of checking the calculate on exit check box of the calling
formfield without the need for macros.

If the REF fields are to be in a header/footer a similar problem arises, but
this can be resolved by formatting the formfield in a unique paragraph style
(it's only the style name that matters) and use a Styleref field to
reproduce it (again without the need for macros)

If you must have text boxes for some reason, then you can run an update
macro on exit from the field. The form should not need unprotecting in order
to update fields, and the macro I would use for that purpose is

Sub UpdateAllFields()
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

However rather than use REF fields, you can use the macro to populate other
formfields directly - but not if you put them in a text box.
eg
Sub OnExitText1()
Dim oFld As FormFields
Dim sRes As String
Set oFld = ActiveDocument.FormFields
sRes = oFld("Text1").Result
oFld("Text2").Result = sRes
oFld("Text3").Result = sRes
'etc
End Sub

Again the form does not need unprotecting.

You'll find more adventurous examples at
http://www.gmayor.com/SelectFile.htm

The frame/table approach saves a lot of trouble :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
L

Laphan

Hi Doug

When I do Print Preview and then go back to the doc the fields would be
populated as I wanted. It sort of refreshed the page and would be a good
'cludge', but I wanted to make work like it used to.

I've actually changed the macro code to look for shapes (?) rather than
stories and it did the trick - I've no idea why!!

Thanks for your help.

What happens if you do a Print Preview (assuming that the option to Update
Fields on Print is set)?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

Laphan

Hi Graham

Thanks for the multitude of options.

As per my posting to Doug, I did manage to get round it by using Shapes (?)
my macro code rather than Stories, but I do want to change my methods so
that I hit the fields direct like you have suggested.

It's just time in getting the values of all the controls and doing the
formatting. I'll get time to do it eventually!! :0)

Thanks again.

The problem revolves around the fact that text boxes are not in the text
layer of the document. If you replace the text boxes with frames or table
cells, then the REF fields they contain will be updated by the simple
expedient of checking the calculate on exit check box of the calling
formfield without the need for macros.

If the REF fields are to be in a header/footer a similar problem arises, but
this can be resolved by formatting the formfield in a unique paragraph style
(it's only the style name that matters) and use a Styleref field to
reproduce it (again without the need for macros)

If you must have text boxes for some reason, then you can run an update
macro on exit from the field. The form should not need unprotecting in order
to update fields, and the macro I would use for that purpose is

Sub UpdateAllFields()
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

However rather than use REF fields, you can use the macro to populate other
formfields directly - but not if you put them in a text box.
eg
Sub OnExitText1()
Dim oFld As FormFields
Dim sRes As String
Set oFld = ActiveDocument.FormFields
sRes = oFld("Text1").Result
oFld("Text2").Result = sRes
oFld("Text3").Result = sRes
'etc
End Sub

Again the form does not need unprotecting.

You'll find more adventurous examples at
http://www.gmayor.com/SelectFile.htm

The frame/table approach saves a lot of trouble :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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