Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Rebuilding Equations in Word 2011 Apps
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"
  set myFrontDoc to front document
  set idx to 1
    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
    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

[crarko adds: I haven't tested this one; I don't really have the type of document this applies to. I did make sure the script compiles without error in the AppleScript Editor.]
  • Currently 3.25 / 5
  You rated: 5 / 5 (4 votes cast)

Rebuilding Equations in Word 2011 | 5 comments | Create New Account
Click here to return to the 'Rebuilding Equations in Word 2011' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Rebuilding Equations in Word 2011
Authored by: arcticmac on Aug 30, '11 09:22:01AM

If you're working with documents with tens of thousands of equations, wouldn't it have been better to write them in LaTeX?

[ Reply to This | # ]
Rebuilding Equations in Word 2011
Authored by: Anonymous on Aug 30, '11 10:54:08AM

Sounds like someone forgot to budget for support costs...

Or if they didn't, open a ticket with Microsoft.

[ Reply to This | # ]
Rebuilding Equations in Word 2011
Authored by: leamanc on Aug 30, '11 12:15:13PM

Hmmm...yeah, I have to agree with you. Right tool for the right job. Word may have its Equation Editor, but it seems to be meant for the occasional equation in an occasional document. If the hint submitter's company is so invested in documents with equations on this level, I can't believe they chose Word over LaTeX. Especially on OS X systems.

[ Reply to This | # ]
Rebuilding Equations in Word 2011
Authored by: egr on Aug 30, '11 10:12:08AM

> I'm not a huge fan of Applescript

Loving the Applescript denigration while using it to solve your problem.

[ Reply to This | # ]
Rebuilding Equations in Word 2011
Authored by: grav on Aug 31, '11 03:46:36AM

Did you try to figure out in what way the equations where messed up in the file itself? As far as I know, Word 2011 uses XML for document saving.

A simple way would be to examine the problem with a single equation, ie. creating it in the older version of Word, and resaving it in the newer version, and then comparing this with a document created with the newer version.

[ Reply to This | # ]