Macro for Chemical Notation in MS Word

If you ever find yourself entering chemicals formulas1 into a Microsoft Word document you will quickly realize that it is much harder than it needs to be. Either you switch back and forth between the keyboard and the mouse to click "subscript", or use the keyboard shortcut Ctrl-(=), or enter the formula and then go back and select all the numbers (using Ctrl-mouse) and then set them to subscript all at once - all of these are too much work, especially if you have a large number of formulas to enter. It is much better to use the following macro to make formatting a chemical formula just take a couple keystrokes. (Direction for installing into Microsoft Word are below.)

#******************************#
Sub ChemNotation()
'
' Chemnotation Macro
'
'
Set myrange = Selection.Words(1)

If (Len(myrange) < 2) Then
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Set myrange = Selection.Words(1)
End If

For i = 1 To Len(myrange)
Char = Mid$(myrange, i, 1)
If (IsNumeric(Char)) Then
myrange.Characters(i).Font.Subscript = True
Else
myrange.Characters(i).Font.Subscript = False
End If
Next i

Selection.MoveRight Unit:=wdCharacter, Count:=1
With Selection.Font
.Subscript = False
End With

End Sub
#******************************#

Directions:

  • Open Microsoft Word

  • Go to the "View" tab in the ribbon
  • Click on the "Macros" button ("view macros", or click Alt-F8)
  • Type the name "ChemNotation" in the Macro name box, then click "Create"
  • Copy and paste the text above into the editing field
  • Open File->Options->Quick Access Toolbar (or right click on the ribbon and select "customize Quick Access Toolbar")
  • Use the drop down box "Choose commands from" to select "Macros", add the macro to the toolbar
  • Select the macro and click "Modify", change the name and icon as desired
  • To use the macro: type in the formula, then either with the cursor at the end of the formula or after selecting the formula, click alt, then click the number on the icon in the toolbar ((I have found that in normal writing I can type the formula and hit Alt, shortcut-number (3 on my quick access bar), but if I am working in a table cell then it only works if I have selected the formula beforehand.
  1. Things like H2O, CO2, or C14H28O2. []

Tags: , ,

Leave a Reply