PDA

View Full Version : Updating Fields in VBA



xSmityx
03-26-2016, 02:01 PM
Quick background: I've created a form that through user input it creates bookmarks throughout the document as they're typing and using the form. They run a Keystroke attached to Alt+P designated to print out the index.

When the index is printed out, it creates headings that relate to the bookmarks created. I then insert a cross reference to the bookmark and input the page number. Thus creating an updateable field so that if pages get shifted around, all the user has to do is update the fields and the cross references get updated to the correct page number. Here's an example of the index being generated:

EXHIBITS Page #

Plaintiff's Exhibit 1 3
Plaintiff's Exhibit 2 5

Now what I want to have happen is at the end of the index printing out that it updates the fields automatically so that they don't have to do it. These indexes being generated have the potential to span over several pages so the first few pages won't have correct page numbers. Here's the code I'm currently using:

Selection.WholeStory
Selection.Fields.Update
Selection.EndKey Unit:=wdStory

So this code is at the very end of the function. When I insert a breakpoint and step through it, it works. When I let the code run without the breakpoint it doesn't work. I've assigned this piece of code to a separate macro for now reminding the users to update the fileds at the end using Alt+Shift+U. At which point it runs this code and works just fine. So for whatever reason the only issue I'm having is that when the code is at the end of the function generating the index, it does not execute successfully.

Any ideas on a solution to this problem would be much appreciated.

gmayor
03-26-2016, 09:51 PM
If you insert an index (using the Insert Index function) the index is created in a new section of the document. The update code you posted only addresses the section (story range) of the document that the cursor is in. At the very least you require a code that addresses all the story ranges in the document (and you may need to update Index or Table of Contents fields separately). See the example code as http://www.gmayor.com/installing_macro.htm

xSmityx
03-26-2016, 11:59 PM
Thank you for your quick reply. I'm not using anything related to the built in indexing that word offers. I have changed the code though for the updating of the fields. So here is where I'm at now. The code I have is as follows:

Dim objDoc As Document
Dim objFld As Field
Dim sFldStr As String
Dim i As Long, lFldStart As Long


Set objDoc = ActiveDocument
' Loop through fields in the ActiveDocument
For Each objFld In objDoc.Fields
' If the field is a cross-ref, do something to it.
If objFld.Type = 37 Then


objFld.Select
Selection.Fields.Update
End If
Next objFld

I've also tried the other one you suggested as well with regard to the story range. They both work fine. So here's where the problem is. I have a function that when the user hits Alt+P it runs the PrintIndex Function. This function will go to the first page which is the cover page and add a pagebreak below it so to generate the index. This is of course where it looks through all the bookmarks in the document and gets the crossreference page number for each bookmark and starts to populate this index page. At the end of it all I run the code above. I can visually see it go through each field, select it and 'update' the field. Unfortunately the page numbers remain incorrect. But if I take the above function and run it outside of the PrintIndex Function, meaning I let the index print, then after it's done, I go and run another function called updatefields with the exact same code and the page numbers update.

Of course the ideal in my situation is that as soon as it's done printing the index out, I want it to update the fields then and have the correct page numbers without the user having to run another function just to make sure they're all correct. I hope this description isn't too convoluted. And do note that I've tried using this code and many other variations of it including the one that you linked me to above and all the same result.

Thanks for taking the time to help.