Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to do Chemical Notation with Word Macro

144 views
Skip to first unread message

Larry

unread,
Apr 10, 2002, 1:34:23 PM4/10/02
to
Here is a little macro that will enable the writer to execute the macro,
and have the chemical formula put into correct notation
Sub chemnotation()
'
' chemnotation Macro
' This macro takes the selected word and puts it into
' chemical notation
' this macro works in two ways:
' * Select the chemical formula and execute the macro
' * Immediately after typing in the formula execute the macro
' Example:
' typing c6h12o6 and executing the macro will produce
' C6H12O6 with the numbers subscripted

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.AllCaps = True
myrange.Characters(i).Font.Subscript = False
End If
Next i
Selection.MoveRight Unit:=wdCharacter, Count:=1
With Selection.Font
.AllCaps = False
.Subscript = False
End With
End Sub

Bob

unread,
Apr 10, 2002, 11:15:29 PM4/10/02
to
On 10 Apr 2002 10:34:23 -0700, ld...@mad.scientist.com (Larry) wrote:

>Here is a little macro that will enable the writer to execute the macro,
>and have the chemical formula put into correct notation

There are a number of such macros around. They vary in complexity,
depending on how complex a situation they are designed to deal with.
There is no way to write a program that can unambiguously format any
chemical text, but one can expand a program to deal with more and more
cases that come up.

For a more versatile macro, which will deal with formulas including
ions, and can do blocks of text, not just isolated formulas, see my
web site at:

http://www.geocities.com/Athens/Thebes/5118/download.htm

Choose ChemFormula. The Figure there will show examples of what the
macro can do. You can DL it; it is free. (A version in Dutch is also
available there.)

The ChemFormula macro at my site was written by a high school student
in England a few years ago.

bob

pan

unread,
Apr 23, 2002, 4:51:37 PM4/23/02
to
The apporaches both Larry and Bob gave above require that the user to:

[1] type the formula;
[2] select the typed text (either move fingers away from keyboard to grab
the mouse or use keystrokes to select);
[3] pick the macro to run (again either use mouse or keystroke);

This actually slows down the typing process significantly.

I can think of 3 possible faster approaches:

[-A-]

write a VBA code to do the following :

(1) Search backward from the current cursor position (pos-A) until a space
is found (pos-B);
(2) Select the text between pos-A and pos-B;
(3) Attach the formula conversion code either Larry or Bob provided here;

Assign this entire code to a key-stroke, say, ctrl-n.

Now, what the user needs to do is:

[1] type the formula;
[2] hit ctrl-n;

That's all !!!

The following approaches are even faster:

[-B-]

type and formulate all possible formula and save them into AutoCorrect. (Who
is gonna do this ?)

[-C-]

write a code to intercept the user input and analyze it --- whenever a space
is entered, an algorithm similar to that in the approach [-A-] is launched
to checked if the text between "this space" and "the previous space" is a
valid chemical formula. If yes, do the conversion.

In approaches [-B-] and [-C-], what users need to do is simiply "type the
formula as normal text" !!!

However, [-B-] needs to setup the format for each individual formula one by
one, which is not only time-consuming but also not "extendable". So it might
only applies to frequent use of a limited set of formula.

For [-C-], you need to be able to capture the user keystroke, and to my best
knowledge it's not an eazy task. If it is even possible, I believe it needs
to invoke the Windows API. This is actually something I am still looking for
a solution to. ("ways to capture user keystrokes" it is).

pan


Bob

unread,
Apr 30, 2002, 8:24:48 PM4/30/02
to
On Tue, 23 Apr 2002 15:51:37 -0500, "pan" <chica...@runsun.info>
wrote:

I have added sci.chem for this msg, along with the word-processing
group. The thread discusses Word macros to assist in formatting
chemical expressions, so some sci.chem people might actually find this
useful. The thread was started by a post of one simple macro. I then
noted Greg Pearce's Word macro ChemFormula, which can be downloaded
(free) from my web site

http://www.geocities.com/Athens/Thebes/5118/download.htm

Choose ChemFormula. A figure there shows examples of what it can do.

ChemFormula is more versatile (and more complex) than the macro that
started the discussion.

more below after pan's msg, to which I am replying.

pan,

Thanks for your thoughtful comments on how a chem formatter might
work. It seems that each variation involves some trade-offs.

I think it is a useful point that your method B, using auto-correct,
could indeed be useful for some, with specific well-defined needs.

One proposal is that the formatting macro should work automatically,
without the user activating it. I'm not sure this would be a good
idea. There is no way to write a general formatter that can resolve
all ambiguities on its own. For example, the character string X2 might
be x-squared, or X-subscript 2 -- depending on the context. And the
character string O2- could reasonably have the 2 either as a subscript
or superscript, depending on which ion is intended. The ChemFormula
macro intentionally separates writing the characters from formatting
them, so the more tedious step of formatting can be done later. It
would be distracting if it occurred while typing.

However, you also noted the need to first select the text to be
formatted. ChemFormula does requires this, and returns a message if
you don't. Perhaps it would be a useful default that if no text is
selected it formats the most recent "word". This could be a useful
enhancement for some situations, without impacting on its current use.

bob

0 new messages