Hack 27 Crunch Numbers Quickly in WordSometimes a spreadsheet is overkill. For quick and dirty math, dust off one of Word's oldest commands: Calculate. If you used Word when the first President Bush was in office, you might remember a handy feature from the Tools menu: the Calculate command. With the standard four functions, plus exponents and percentages, Calculate could handle a lot of the math needed for simple sales reports or budget proposals. Though Microsoft removed the command from the Tools menu in Word 6.0, you can still find it if you know where to look.
4.2.1 Resurrecting Calculate
First, select Tools Figure 4-1. Locating the Calculate command buried deep in WordDrag the command to the Tools menu and place it right under the Speech option (or anywhere else on the menu). Right-click the new menu item and rename it "Calculate," as shown in Figure 4-2. Figure 4-2. Returning Calculate to the Tools menuWhen you first place the command on a menu or toolbar, it may appear grayed-out. Calculate is available only when you've selected some text. 4.2.2 Using Calculate
By default, the Calculate command will add any set of selected numbers separated by whitespace. Word temporarily displays the result in the status bar, as shown in Figure 4-3, and also places it on the clipboard. Figure 4-3. The sum of the selected numbers is temporarily displayed in the status barCalculate ignores any text that isn't a number, except for currency symbols, periods, and commas, which it recognizes when these are part of a number. For operations other than addition, you must include the mathematical operator. Table 4-1 lists the operations in reverse order of precedence. To force a calculation out of precedence order, enclose the expression in parentheses. Addition and subtraction are of equal precedence and are evaluated left to right. Multiplication and division also are of equal precedence and are evaluated left to right.
Though Calculate is most often used in tables, it works on any selected text. For example, you can use this command to quickly add all the numbers in a paragraph of text, as shown in Figure 4-4. Figure 4-4. Calculate works with selected text and displays the results in the status bar4.2.3 Hacking the Hack
The calculation results are displayed in the status bar for only a few seconds. After that, if you want to see the results, you must either paste them from the clipboard or redo the calculation, paying closer attention to the status bar. If you prefer to display the calculation results more directly, you can intercept the command [Hack #61] and have Word display the results in a message box. Place the following macro in the template of
your choice [Hack #50] .
It will run in place of the Calculate command when you select
Tools Sub ToolsCalculate( ) MsgBox Selection.Range.Calculate End Sub However, when you intercept the command, Word neither displays the calculation results in the status bar nor copies them to the clipboard. To also put the results in the status bar, use the following code instead: Sub ToolsCalculate( ) Dim sResult as String sResult = Selection.Range.Calculate StatusBar = "The result of the calculation is: " & sResult Msgbox sResult End Sub It takes a bit more work to get the results copied to the clipboard. There's no direct way to access the clipboard from VBA, so you need to use Windows API calls. You can find sample code for accessing text on the clipboard at http://support.microsoft.com/default.aspx?scid=kb;en-us;138909. With the code from that site included in the same module, use the site's example Clipboard_SetData subroutine to put the results on the clipboard: Sub ToolsCalculate( ) Dim sResult As String sResult = Selection.Range.Calculate StatusBar = "The result of the calculation is: " & sResult MsgBox sResult ClipBoard_SetData (sResult) End Sub |