The goal of this hint is to automate the process of re-creating an Equation Editor equation in Microsoft Word.
Note: This hint applies to equations created in Microsoft Word using Equation Editor. It does not apply to the new kind of equations that can be created in Office 2011.
We have many large documents that contain hundreds of equation objects each. When attempting to revise some of these documents in Word 2011, the equation objects appeard to be corrupted. Double clicking on them would open the object in Equation Editor, but changes were not reflected in the document. In addition, the baselines for each equation object did not line up with the surrounding text. The most frustrating part is that we had just invested several tens of thousands of dollars re-typesetting equations.
The only solution I could find was to open each equation object, select all of its contents and copy it, close Equation Editor, create a new equation object, and then paste the contents of the old equation. The new equation could then be edited, with changes reflected in in the document as expected. Also, the baselines were correctly aligned (although the character immediately after was usually messed up). Unfortunately, this meant that thousands of equations had to be updated.
Although Word 2011 can be controled by Visual Basic, Equation Editor cannot. Nor does Equation Editor support Applescript. I'm not a huge fan of Applescript, but in this case, UI Scripting came to the rescue. I have been able to automate the entire process.
Below is a script that I have applied to one of our documents, and it seems to work quite well. It could certainly be improved (such as updating equations in the current selection instead of the whole document), but it does what I need.
A few notes: There are a couple delays embedded in the script. These are necessary to make sure the script doesn't get ahead of the user interface. Maybe someone can come up with a more elegant solution. Also, Equation Editor doesn't seem to respond to the 'keystroke' command, so menu actions had to be used. If there are 'empty' equation objects in your document, Equation Editor will issue an alert that needs to be dismissed. I suppose the script could try to detect this situation (another opportunity for improvement). Finally, in my test case, lower-case 'phi' gets converted to an alternate form.
I hope this is helpful to someone else that runs into a similar situation.
tell application "Microsoft Word" activate set myFrontDoc to front document set idx to 1 try set myField to field idx of myFrontDoc repeat while (myField exists) select myField tell me to ReconstructEquation() set idx to idx + 1 set myField to field idx of myFrontDoc end repeat end try end tell on ReconstructEquation() -- Assumes an equation object is currently selected delay 1 -- Give Word a chance to update the menus try tell application "System Events" set theEditMenu to menu 1 of menu bar item "Edit" of menu bar 1 of process "Microsoft Word" set theEqnMenu to menu 1 of menu item "Equation Object" of theEditMenu click menu item "Open " of theEqnMenu -- note the space repeat until process "Equation Editor" exists end repeat delay 2 -- Equation Editor process exists, but we need to give it a chance to open tell process "Equation Editor" set theEEFileMenu to menu 1 of menu bar item "File" of menu bar 1 set theEEEditMenu to menu 1 of menu bar item "Edit" of menu bar 1 click menu item "Select All" of theEEEditMenu click menu item "Copy" of theEEEditMenu click menu item 2 of theEEFileMenu -- Menu item name depends on the document being edited -- For some reason, keystrokes don't work: --keystroke "A" using command down --keystroke "C" using command down --keystroke "W" using command down end tell repeat while process "Equation Editor" exists end repeat click menu item "Paste" of theEditMenu -- Overwrite the selected equation end tell end try end ReconstructEquation
Mac OS X Hints
http://hints.macworld.com/article.php?story=20110805200638450