Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 24

Thread: Update Fields Problem

  1. #1

    Update Fields Problem

    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

  2. #2
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    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!

    -Kelly

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Stan,
    Can you zip the file and post it here, if its not confidential. It makes things a lot easier.
    MD

  4. #4
    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

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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.

    [VBA]
    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

    [/VBA]

  6. #6
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    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:

    [vba]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[/vba]

    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.

  7. #7
    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

  8. #8
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    Hmm....

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

    [vba]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[/vba]

    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.

  9. #9
    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

  10. #10
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    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.

  11. #11
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    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.

    [vba]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[/vba]

    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.

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Kelly,
    Works in 2000
    MD

  13. #13
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    So sweet of you, mdmackillop!

    I appreciate it.

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Still strangling the penguin I see!!!

  15. #15
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    Quote Originally Posted by mdmackillop
    Still strangling the penguin I see!!!
    You're going to make me laugh so hard I'll cry!!

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

    Especially for Linux users! LOL :rofl

  16. #16
    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

  17. #17
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    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.

    [vba]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[/vba]

    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"



  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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

  19. #19
    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

  20. #20
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    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.....

    [vba]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
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •