Pages

Thursday, May 16, 2013

Automate date formatting tasks with a macro (Microsoft Word 2003/2007/2010)

If you have to reformat dates frequently, you might wish to have at your fingertips a macro to accomplish the task in a flash. We will show you here how to change dates from a dd-mm-yy format to mm-dd-yy format (example: 6th June 1944 to June 6, 1944). Choose Tools | Macro | Record New Macro and give a name to the macro in the Macro Name text box (use the Developer tab in Word 2007/2010). If you choose the default entry, All Documents (Normal.dot), in the dropdown box, you’ll be able to access the macro from anywhere inside Word.

You can choose to assign the macro to either a toolbar or the keyboard by choosing accordingly in the Assign Macro To box:

  1. Click on the Keyboard icon to assign a keyboard shortcut for the macro. On clicking the Keyboard icon, you are presented with the Customize Keyboard dialog box.
  2. Enter a shortcut key combination such as [Alt][Shift][Ctrl][F] in the Press new shortcut key textbox. If the shortcut key combination is already assigned to another action, choose another combination.
  3. Choose an option from the Save Changes In dropdown list, then click the Assign button.
  4. Select in the Save changes in dropdown list whether you want the shortcut key combination to be available from the current document only or from any document based on the Normal.dot template.
  5. Click Close to start recording the macro.
When you click Close, you will see a small icon on the screen with Stop Recording and Pause Recording buttons on it.

Now you can record the keystrokes you want the macro to perform. Be very careful while recording the macro—it will perform exactly what you record each time you run it.

To change the date formats, press [Ctrl][Home] first—this will ensure that each time you run the macro, it will start searching from the beginning of the document. We want the macro to start searching from the beginning of the document to the end so as not to miss any occurrences of what we are trying to find and replace.

Follow these next steps to finish out the process:
  1. Click [Ctrl][H] to open the Find and Replace dialog box.
  2. Click [Alt][U] to select the Use wildcards check box and then [Alt][N] to place the cursor inside the Find what text box.
  3. Now, you have to type the following text carefully:
    ([0-9]{1,2})([dhnrst]{2})[space](<[ AFJMNSOD]*>) ]*>)[space]([0-9]{4}). Press the spacebar once for each instance of [space] in the foregoing search string.
  4. Then, type \3 \1, \4 in the Replace with textbox.
  5. Press [Alt][A] to change the date format of all dates in the document.
  6. At this point, you need to click on the Stop Recording button to stop recording the macro.
Now you’ve got your macro, and any time you want to format dates, you have to only press the keyboard shortcut for the macro to change the date formats instantaneously.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.