I want to update all fields in a Microsoft Word Document in both the main story and the second page header story. So far the closest I can get to updating the fields in the main story is
tell application "Microsoft Word"
update every field of active document
end tell
This command however takes an excessively long time, requiring me to force quit the application.
Manually, I can easily update the fields via a GUI by clicking Word’s menu item Select All or by the following script
tell application "System Events"
tell application process "Microsoft Word"
tell menu bar 1
tell its menu bar item "Edit"
tell its menu "Edit"
click its menu item "Select All"
end tell
end tell
end tell
end tell
end tell
Whereas when I manually control click on the window after All is selected, a drop down menu appears with a command to Update Field, I’m unable to script the window to show that drop down menu, using AppleScript.
What UI script will show a drop down menu in a Microsoft Word window, after all of its text is selected, and then click on the Update Field command?
Is there a more simple Microsoft Word AppleScript that will update all fields in Microsoft Word?
After posting the above to Scripting Forums, I found that Microsoft’s key command to update its fields is initiated by pressing F9. Using a post from KniazidisR at clicking-fn-and-enter-together-code-63-not-working, I assembled the following code:
tell application "System Events"
tell application process "Microsoft Word"
tell menu bar 1
tell its menu bar item "Edit"
tell its menu "Edit"
click its menu item "Select All"
delay 0.2
end tell
end tell
end tell
key down 63 -- press "Fn" key
key code 101 --key "F9" ( functional key to update fields in MW)
key up 63 --release "Fn" key
end tell
end tell
But unfortunately this failed for me, so my questions remain. I appreciate any insights to this problem.
Word can have multiple keyboard shortcuts for a given command. In my setup (word 2011), command-option-shift-U is also a shortcut for ‘update field’. My version of Word has Tools > Customize Keyboard…, in which you can both view and create shortcuts. From that dialogue, look for the ‘Insert’ category and then for the ‘UpdateFields’ command and you should see what commands are set for it. If it only has F9, then you can easily add one that doesn’t require the function key and then have your script operate accordingly. Here is an example:
Note that I use the regular approach to select all text within the document.
tell application "Microsoft Word"
activate
select text object of document 1 -- equivalent to select all
tell application "System Events"
tell application process "Microsoft Word" -- key code 32 is the letter 'u'
key code 32 using {command down, option down, shift down}
end tell
end tell
end tell
By the way, here is the native way to update all fields… by using a repeat loop:
tell application "Microsoft Word"
tell document 1
set fieldList to fields
repeat with ef in fieldList
update field ef
end repeat
end tell
end tell
The update field command is intended for use on an individual field.
By the way, if you wished to update only a range of fields, you could do something like this: define a text object that includes the desired fields, and then update the fields within that range.
tell application "Microsoft Word"
tell document 1
set t1 to create range start 0 end 200 -- first 200 characters of document
select t1 -- not necessary but allows you to see affected area
set fieldList to fields of t1
repeat with ef in fieldList
update field ef
end repeat
end tell
end tell
And of course, since you’re working with a list, you can also use that to control which fields are updated.
I combined your two ideas of creating a custom command in Microsoft Word, and then calling that custom key binding from System Events.
The following AppleScript directs Microsoft to:
Designate Microsoft Word’s Normal template as the reference in which to store new key bindings
Build a key code pattern using four keys: command, option ( which Microsoft Word designates as alt), shift, and the letter “u”(which Microsoft Word designates as u_kwy. The designation kwy is not a typo but is for reasons not clear to me, Microsoft’s unusual designation for the u key. )
Assign a key binding to Microsoft Word’s command to update fields
Execute the keybinding
tell application "Microsoft Word"
# Set template object representing the template to store new key bindings changes.
set customization context to normal template
# Build unique pattern with specified key combination
set KeyCode to (build key code key1 command_key ¬
key2 key alt ¬
key3 shift_key ¬
key4 u_kwy)
# Create custom key binding assignment for the command "UpdateFields" in the customization context
make new key binding with properties ¬
{key code:KeyCode, key category:key category command, command:"UpdateFields"}
# Execute the command associated with the key code specified key combination
execute key binding (find key key code KeyCode)
end tell
The result, which I was seeking, does not stall or occupy excessive time that my original AppleScript required as Microsoft looped through all the fields in a document, but operates more like Microsoft’s UI Update Fields command, or even like Microsoft’s VBA UpdateFields command.
That’s a different approach but I’m glad it works for you. I guess that sometimes, the key shortcut is the quickest approach.
One thing though… you should find that you only have to update the template once and after that, any document based upon the normal template will have access to that functionality.
Mockman, yes I agree with you that the Normal Template needs updating only once. I found it, however of interest, that no error occurred from rewriting or overwriting the Microsoft Word’s key codes, even when the AppleScript was repeated.
That makes sense. I don’t think it will ever generate any errors — it’s just that the setting gets set to the same thing again and again and there really isn’t an advantage in doing so.