PDA

View Full Version : Update Fields Problem



Stan Banner
10-26-2004, 11:55 AM
I have a form on a template that I use quite often, and this template has both text boxes and fields. I can update the fields quite easily by selecting them, one at a time and hitting F9. Every time I use "Select All" then F9, or when I use a macro that does the same thing, the text boxes go blank.
Will someone offer me a solution.

Thanks in advance

Stan

Kelly
10-26-2004, 01:18 PM
Hi Stan!

I may be able to help you, but first I have several questions about this.

When you are using the form (meaning: when you are filling in the form) isn't the form protection turned on, allowing you to "TAB through" the fields? Because when the form protection is activated (which it typically should be when the form is being used, not created), I don't think you are ABLE to choose "select all." I am using Word 2000, and that is the case for me.

Also, when you say "text box," are you referring to the text box that is created by going to the "Insert" drop down menu and choosing "Text Box?"
or are you referring to a Text Form Field, that is essentially a "blank" for filling in text into the form?

If you are referring to a Text Form Field (the kind inserted by using the Forms toolbar), then I must ask:
what exactly were you trying to accomplish by updating the fields? what information changes that creates a need to update the content of all fields? Are the fields linked to bookmarks or linked documents or something? or are the fields simply blank spots for the user to enter text?
Because if you have a Text Form Field that can only get its content from someone typing text into it, then the ONLY result you get by "updating" such a field is for the Text Form Field to revert back to its "Default Text." In your case, it appears that the Default Text consists of an empty string.
You can find out what the default is by right-clicking the Text Form Field and choosing "Properties."

So, if you are referring to Text Form Fields, then to be able to help you I would need to know what you want to happen when you update. As I said above, the 'erasing' of the contents is rather the expected and natural result from updating the field.

If you are referring to Text Boxes of the "Insert >> Text Box" type, then I am considerably more baffled. I have never heard of a Text Box going blank from updating any field.

I look forward to further discussion on this. And if these truly are Text Boxes and not Text Form Fields, then I really really hope someone else has some ideas about this, because it sounds really strange.

"See" you back here soon! :hi:

-Kelly (http://kellyjones.netfirms.com)

mdmackillop
10-26-2004, 01:47 PM
Hi Stan,
Can you zip the file and post it here, if its not confidential. It makes things a lot easier.
MD

Stan Banner
10-26-2004, 02:54 PM
Hi Kelly:
Thanks for the quick reply.
The form that I am using is actually a Table which takes up the full page, with several dozen cells. Some of the cells have Text Boxes added by the Forms tool bar. These text boxes are linked to Fields on following pages, allowing header information to be passed on to the successive pages. These following pages are added to this document as needed, and a final document may have as many as 20 pages, each with about a dozen fields linked back to the text boxes on page 1. I also have Two fields on page 1, Page Number and Page Count, and these fields also appear on each of the following pages. There are several blank cells on the first page and each of the following pages into which text is entered or pictures are pasted. To use this template, I lock the form and fill in the text boxes on page 1, then I unlock the template and use Autotext to add additional pages as required, adding text and pasting pictures as I go. I would like to be able to run a macro when I am done to update all page numbers, but when I try something like this, all of the text boxes on page 1 go blank.
Is this enough information for you?

Stan

mdmackillop
10-26-2004, 04:36 PM
Hi Stan,
Your problem is similar to that in spellchecking forms and I've listed the code for that below. You can insert your update code within the body of the text, before the ReProtect portion, and your data should be preserved.


Sub SpellCheckProtected()
' If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

' Set the language for the document.
Selection.WholeStory
Selection.LanguageID = wdEnglishUK

' Perform Spelling/Grammar check.
If Options.CheckGrammarWithSpelling = True Then
ActiveDocument.CheckGrammar
Else
ActiveDocument.CheckSpelling
End If
' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If

End Sub

Kelly
10-26-2004, 06:18 PM
Hello again.

Stan- that was a great description!

Actually, your document sounds a lot like one that I use at my job. And it sounds like you need to make your form work in a lot of the same ways that I have managed to get mine to work.

Well, I actually spent a few minutes making a small demo macro that would update only page number fields, but then I re-read your description and now I am not sure if you are using the BUILT-IN "PAGE" fields or if you are using some sort self-styled method for numbering your pages. (like for example in my case, I have documents with pages 1, 2, 3a, 3b, and 4, and I use my own field to make sure the page count says "4" and not 5.)

Anyway, assuming your page number fields are "the regular old kind," then the following is an example of a macro that will update ONLY the page number fields in a document and nothing else:

Sub UpdatePageFieldsOnly()

Dim myRange As Range

For Each myRange In ActiveDocument.StoryRanges

For Each aField In myRange.Fields

If aField.Type = wdFieldPage Then

aField.Update

End If

Next aField

Next

End Sub

Make sure you have your document backed up, and then try this macro and see if it accomplishes anything at all for you. Of course, the macro takes action on whichever document is "ActiveDocument," so make sure that the document in question is the "active" one. Or to be absolutely sure, have only one document open when you run this.

Let me know if this is at least along the lines of what you need. We have several other options for how to identify specific fields for updating while excluding other fields from the process.

Stan Banner
10-27-2004, 07:43 AM
Hi again Kelly:
I tried the macro that you suggested and it works, except that it only updates the Page field. I also need to update the NumPages field. I tried to add a second For - Next loop to update the NumPages field, but it appears to have no affect. This loop is identical to the wdFieldPage loop except that wdFieldPage is repalaced with wdFieldNumPages. I cannot figure out why this isnt working if the first loop works.
Any suggestions?

Stan

Kelly
10-27-2004, 12:08 PM
Hmm....

Well, if I were going to add the NumPages field into my macro, the first thing I would try would be this:

Sub UpdatePageFieldsAndNumPagesFields()

Dim myRange As Range

For Each myRange In ActiveDocument.StoryRanges

For Each aField In myRange.Fields

If aField.Type = wdFieldPage Or aField.Type = wdFieldNumPages Then

aField.Update

End If

Next aField

Next

End Sub

This single macro should handle both types of fields.

I only changed one line of the macro, and now when I test it, it seems to be working for me on a "dummy document" that I created for testing.

Hopefully that will work. But what you tried to do (re-creating a second loop and changing wdFieldPage to wdFieldNumPages) should have worked. Therefore, I have less confidence that my macro (above) will work if your second macro didn't work.

Hmmm...

Well, I guess you should try what I just posted. If it works, then great! If not, then I would be interested to know the following:
when you run the ineffective macro that you wrote, do you get an error message? Or does it simply "run" and yet basically not accomplish anything?

Also: are the PAGE fields in a header or footer? (it shouldn't matter, but...) are the NUMPAGES fields in the same place as the page fields? I ask this because the whole business (in the macro) about "StoryRanges" is what we have to do to make MS Word look for fields in the headers and footers rather than only in the main body area. So I'm thinking MAYBE (a VERY slight possibility) we are having difficulty because the NUMPAGES fields are in some part of the document where the macro isn't finding them. But that is such a remote possibility. I hope you will tell me that the NUMPAGES fields are right next to the PAGE fields so that we can rule out that possibility. If the PAGE and NUMPAGES fields are in essentially the same place, then their whereabouts shouldn't be causing the problem, because the PAGE fields clearly ARE found by the macro.

Obviously, if my latest macro works, then you need not answer all of my complicated questions.

Stan Banner
10-27-2004, 12:31 PM
Hi Kelly:
I tried your suggested macro to find both Pages and NumPages, but still only the Pages field updates.
These fields are in the main body of the document, in seperate but nearly adjacent cells of a table. I get no error messages.
I am sure that the macro is finding these NumPages fields but just not updating them because, with a 3 page document which has 3 Page fields and 3 NumPages fields, after I run the macro, it takes 6 UnDos to get back to the original state.

Stan

Kelly
10-28-2004, 03:26 PM
Hello again.

Thank you, Stan, for emailing your document to me.

Are you sitting down for this?

... of COURSE (Murphy's law, I guess)... when I use your document (and use your AutoText entry) and then run the macro on your document, it works. I'm happy it works, but I'm perturbed that you don't get to enjoy the results.

Now, I have some more ideas for further experiments to perform.

You said in your email to me that you transfered the document to Word 97 in order to email it. My question is: did you test the macro in Word 97? I know that when you pasted, the fields got updated (causing the text fields to be cleared), so obviously this update would have fixed the page numbers. but what if you add another page or two (in Word 97) using your AutoText, and THEN run the macro in Word 97. Does it work in Word 97?

If it does, then my follow up question/experiment would be: save the 97 version on a disk, take it back to your Word XP computer and open it up there. Does the macro still work on the 97 document when you open the 97 document using XP?

Also, when the macro does NOT work, what is the exact result you get? For example, I would assume that you WANT to get this:
1 of 3
2 of 3
3 of 3

So what do you get?

1 of 1
2 of 2
3 of 3 ??????
(is that what you get?)

Also, when it doesn't work, let's say you get something wrong, like
1 of 1
2 of 1
In that case, what do you get when you manually update only your NumPages fields?? Do you get the correct result when you individually manually update them?

One final thing:
I am attaching a dummy document here in the forum that is loosely based on your document. Obviously the dummy document does not contain any company info or authorship info.

This dummy document was created in Word 2000. It has one macro (to update the fields in question) and it has one AutoText entry (to add additional pages).

I would appreciate it if ANYONE out there who has read this forum topic would test this dummy document in different versions of Word (97, 2000, 2003, XP, whatever) and let me know if it works.

And obviously if ANYONE has further help for Stan, please jump in.

Kelly
10-28-2004, 03:40 PM
Oh! I almost forgot.

One other thing:

When you find that all fields are being updated whenever you protect or unprotect the form, then the solution is as mdmackillop described. You CANNOT use the usual protect/unprotect option on the Word menu. You must use a macro. You could simply write a macro like the following and perhaps assign it to a "hotkey" combination like Alt+P (for protection) or something.

Sub ToggleProtect_dont_reset()

If ActiveDocument.ProtectionType = 2 Then

On Error Resume Next
ActiveDocument.Unprotect " "

If Err.Number = 5485 Then
MsgBox "The document is password protected." & vbCr & vbCr & "Macro failed.": End
End If

ElseIf ActiveDocument.ProtectionType = wdNoProtection Then

ActiveDocument.Protect Password:="", NoReset:=True, Type:=wdAllowOnlyFormFields

Else

MsgBox "The document is not protected with ""All-but-form-field"" protection.": End

End If

End Sub

Then you could toggle the form protection on and off (without using passwords) by pressing Alt+p, or whatever key combo you choose to assign.

mdmackillop
10-28-2004, 03:43 PM
Hi Kelly,
Works in 2000
MD

Kelly
10-28-2004, 03:46 PM
So sweet of you, mdmackillop!

I appreciate it.

mdmackillop
10-28-2004, 03:48 PM
Still strangling the penguin I see!!!

Kelly
10-28-2004, 03:53 PM
Still strangling the penguin I see!!!
You're going to make me laugh so hard I'll cry!! :bawl

I shudder to think what unintended meaning "stangling a penguin" would have for the uninitiated.

Especially for Linux users! LOL :rofl

Stan Banner
10-29-2004, 08:21 AM
Hi again Kelly:
I tried running your dummy template file on my Word 97 computer and it seems to work fine.
I tried to run the dummy template file on my Word XL computer and it still only updates the page fields, i.e. 1 of 1, 2 of 1, 3 of 1 etc. Updating the NumPage fields by hand works fine. I copied the Macro into my original template and the same thing happens.
I then saved my original template as a Word 97 template, and opened it in Word XL and tested it again with the same results. Only the Page fields update, but the NumPages fields update manually just fine.
Is there something about Word XL that we dont know?
One other thing that I just noticed. When I used Organizer to try to move the Macro from the Word 97 template to the Word XL template, Organizer did not seem to see the Word 97 template, and it did not seen it after it had been saved as a document by Word XL. Is there something else about these templates that I need to know?

Stan

Kelly
10-31-2004, 01:35 PM
Hi again everybody!

Okay, Stan, I have two more ideas.

I need you to try the following on your WORD XP computer:

Open either your original document or the "dummy" doc. (actually, you might as well try it with both)

in the regular Word window (not from the macro editor area), select/highlight any ONE of your PageNum fields.

Then run this macro. You should get a message box reporting a number to you. Does it say "26" ? Let me know what it says.

Sub GetFieldTypeInformation()

x = Selection.Fields.Count
If x <> 1 Then MsgBox "you must select ONE and ONLY ONE field to run this macro": End

MsgBox Selection.Fields(1).Type

End Sub

Secondly,

highlight only the PageNum field and then RECORD A MACRO. while recording, just hit F9 (to update the field). then LOOK at the macro that was recorded. does it simply say:

Selection.Fields.Update

Again, let me know the result.

I basically only have two last ditch efforts remaining to make this thing work. My two remaining ideas are based on the two above-mentioned experiments.

The first idea is this: if the Message Box (from the GetFieldType macro) says "26", then we could try changing the previous macro (Sub UpdatePageFieldsAndNumPagesFields) so that the fourth line says:

If aField.Type = wdFieldPage Or aField.Type = 26 Then

Instead of:

If aField.Type = wdFieldPage Or aField.Type = wdFieldNumPages Then

________________________________________________
My final idea:

(by the way... I have slightly more hope for my final idea than for the "aField.Type = 26" idea)

this idea is a little more complex. we will have to base it on your REAL document, not the dummy doc. Therefore, you will have to analyze your REAL document and tell me this:

your document can end up with any number of pages, right? sometimes 2 pages, sometimes 3,4,5,etc ?? is that correct??

Assuming that's correct, then this is what we are hoping:

are the PageNum fields always in the same row of EACH table?

for example, are the PageNum fields ALWAYS in the last cell of the fifth row? or the tenth row? (it doesn't matter which row, it only matters that it be consistent)

If so, then let me know which row, and I can make a macro to select the appropriate cell on each page (or each table) and then do the "Selection.Fields.Update"

mdmackillop
10-31-2004, 02:17 PM
Hi,
A couple of thoughts re Kelly's dummy.
On my PC the numbering is correct when the new document is formed. Is it only needing updated when new pages are added?
If the document is similar to Kelly's dummy, the page numbering element would be better kept in a page header or footer, otherwise the positioning changes as text/additional pages are inserted. How would this affect the updating procedure?
MD

Stan Banner
11-01-2004, 09:44 AM
Hi Kelly:
I have tried the suggestions that you have offered and have the following results:
On both my Template and your dummy, Page = FieldType 33 while NumPages = FieldType 26.
Recording a Macro while updating the fields gives the following line: Selection.Fields.Update for both the Page and NumPages fields.
Running your original Macro using aField.Type = 26 produces same results as the original Macro.
A final document from my template will have at least 2 pages, and may have up to 20 pages.
Page 1 of my Template has the Page Field at Row 9, Column 5 and the NumPages Field at Row 9, Column 7.
All Continuation Pages, when added, have the PageField at Row 6, Column 5 and the NumPages Field at Row 6, Column 7.
I hope that this helps.
Stan

Kelly
11-01-2004, 01:01 PM
Hi Stan!

Okay, here is another macro to try. This macro will "interrupt itself" to display messages to you before and after updating each field. This macro will ONLY work if the Page and NumPages fields are ALWAYS in the rows that you have described. If this works, then we can take out the messages that are displayed to you in order to speed up the process.

I forgot that you had emailed me your exact document! Today, I went back to the ACTUAL/REAL document (not the dummy) in order to write this macro.

the key difference I found between your "real" document and the dummy is this: in the dummy that I made, each page contains a table that is SEPARATE from the table on the previous page. In your REAL document, as you add on your "continuation" autotext, what actually happens is that more rows are added to the table on the first page. the new rows appear on the second (and third, and so on) page, yet they are really part of one long unified table.

Well, here goes.....

Sub UpdatePageNumberingViaTableCells()

With ActiveDocument.Tables(1).Rows(9)

.Cells(5).Range.Select
MsgBox "The selected area is about to be updated."
Selection.Fields.Update
MsgBox "Make a note of whether it worked or not, then click OK to continue"

.Cells(7).Range.Select
MsgBox "The selected area is about to be updated."
Selection.Fields.Update
MsgBox "Make a note of whether it worked or not, then click OK to continue"

End With


For x = 26 To ActiveDocument.Tables(1).Rows.Count Step 16

With ActiveDocument.Tables(1).Rows(x)

.Cells(5).Range.Select
MsgBox "The selected area is about to be updated."
Selection.Fields.Update
MsgBox "Make a note of whether it worked or not, then click OK to continue"

.Cells(7).Range.Select
MsgBox "The selected area is about to be updated."
Selection.Fields.Update
MsgBox "Make a note of whether it worked or not, then click OK to continue"

End With

Next


End Sub

Stan Banner
11-02-2004, 08:41 AM
Hi again Kelly:
This seems to be a tough nut to crack.
I ran your new macro and the following happened:
The macro operated on the 2 fields on Page 1, however, the NumPages updated to 1, not to the number of pages in the new document, in this case 3. The macro then skipped the rest of the document and ended. I tried stepping through the macro, and the macro went from the first line in the second part of the macro right to End Sub. The logic in the second part of this macro must not be quite correct.
One thing that I have not mentioned, but I dont see how it could affect the document is that I add the continuation pages using a Macro that inserts the AutoText, and I have assigned the Macro to a key combination "Alt-F1". I hope that this isnt a problem.
Stan

Stan Banner
11-02-2004, 09:09 AM
Kelly:
I have tried editing the code in your macro and the following macro runs to completion, looking at every page field.

Sub UpdatePageNumberingViaTableCells()

With ActiveDocument.Tables(1).Rows(9)

.Cells(5).Range.Select
MsgBox "The selected area is about to be updated."
Selection.Fields.Update
MsgBox "Make a note of whether it worked or not, then click OK to continue"

.Cells(7).Range.Select
MsgBox "The selected area is about to be updated."
Selection.Fields.Update
MsgBox "Make a note of whether it worked or not, then click OK to continue"

End With

'MsgBox ActiveDocument.Tables(2).Rows.Count
For x = 2 To ActiveDocument.Tables.Count

With ActiveDocument.Tables(x).Rows(6)

.Cells(5).Range.Select
MsgBox "The selected area is about to be updated."
Selection.Fields.Update
MsgBox "Make a note of whether it worked or not, then click OK to continue"

.Cells(7).Range.Select
MsgBox "The selected area is about to be updated."
Selection.Fields.Update
MsgBox "Make a note of whether it worked or not, then click OK to continue"

End With

Next


End Sub

The problem is that even though the Page field updates every time, the NumPages fields do not update. It appears to me as if there is something intrinsically wrong with my NumPages fields such that Word does not recognise it.
What has occured to me, however, is that if I am going to run this kind of macro to update the Page fields anyway, what if the Page and NumPage fields are deleted, and this Macro is edited so that when it is run, it simply inserts the page number and count as text into these Table Cells.
Stan

Kelly
11-02-2004, 07:35 PM
It appears to me as if there is something intrinsically wrong with my NumPages fields such that Word does not recognise it.
Yes. I agree. It doesn't make sense that
If aField.Type = wdFieldNumPages Then aField.Update
would work in Word 2000 (for me and for mdmackillop) and also work for you in Word 1997, and then totally fail in Word XP.

On the bright side, congratulations! you are probably the first person to discover this annoying flaw in the way VBA works in Word XP. Maybe if you contact Microsoft they will send you a free service pack! :rofl
Maybe they'll name the next Word patch after you!




What has occured to me, however, is that if I am going to run this kind of macro to update the Page fields anyway, what if the Page and NumPage fields are deleted, and this Macro is edited so that when it is run, it simply inserts the page number and count as text into these Table Cells.
Stan
That sounds like a wonderful idea. I was so upset over this issue that I had reached the point of brain paralysis, so your solution is like a ray of sunlight breaking through the dark stormclouds.

Based on the excellent re-write you performed on my last macro, it appears to me that you probably already know how to achieve your idea of inserting the page count as text.

Just off of the top of my head, you might try getting the page count by using either of these two ways: (I'm sure there are probably other ways, too)

ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)

or

ActiveDocument.Range.Information(wdActiveEndPageNumber)

Stan Banner
11-03-2004, 09:34 AM
Thanks for the help Kelly:
I have now fixed up the macro do update the Page and to insert the Number of pages as text. I have also integrated this into my Auto Text insert continuation pages macro so that every time I insert a continuation page into the document, the whole document page numbering is updated, and it works like a charm.
Stan