Changing chemical notation

F

filo666

Hello, I have very large documents with chemical formulas and notation (ex.
CH4 or H2SO4 but never 3H2O (The three is at the biggining of the word))

I would like to:

For each word in the selection (after I selected something) chek if there is
a number sticked (and after) the first and or second and or third ......
letter; if so the to change all the numbers in that word to subscript numbers.
I dont have any experience working with word VBA (some with excell VBA)


thanks a lot
 
D

Doug Robbins - Word MVP

Use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[A-Z]{1}[0-9]{1}", Forward:=True, _
MatchWildcards:=True, MatchCase:=True, Wrap:=wdFindStop) = True
Set myrange = Selection.Range
myrange.start = myrange.start + 1
myrange.Font.Subscript = True
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
F

filo666

Thanks for the answer, the code does not work, it sends me automaticly to end
with and then to end

Doug Robbins - Word MVP said:
Use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[A-Z]{1}[0-9]{1}", Forward:=True, _
MatchWildcards:=True, MatchCase:=True, Wrap:=wdFindStop) = True
Set myrange = Selection.Range
myrange.start = myrange.start + 1
myrange.Font.Subscript = True
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

filo666 said:
Hello, I have very large documents with chemical formulas and notation
(ex.
CH4 or H2SO4 but never 3H2O (The three is at the biggining of the word))

I would like to:

For each word in the selection (after I selected something) chek if there
is
a number sticked (and after) the first and or second and or third ......
letter; if so the to change all the numbers in that word to subscript
numbers.
I dont have any experience working with word VBA (some with excell VBA)


thanks a lot
 
G

Greg Maxey

This might work:
Sub ScratchMacro()
Dim oWord As Range
Dim i As Long
For Each oWord In Selection.Words
For i = 1 To oWord.Characters.Count
If oWord.Characters(1) Like "#" Then Exit For
If oWord.Characters(i) Like "#" Then oWord.Characters(i).Font.Subscript
= True
Next
Next oWord
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Thanks for the answer, the code does not work, it sends me
automaticly to end with and then to end

Doug Robbins - Word MVP said:
Use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[A-Z]{1}[0-9]{1}",
Forward:=True, _ MatchWildcards:=True, MatchCase:=True,
Wrap:=wdFindStop) = True Set myrange = Selection.Range
myrange.start = myrange.start + 1
myrange.Font.Subscript = True
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

filo666 said:
Hello, I have very large documents with chemical formulas and
notation (ex.
CH4 or H2SO4 but never 3H2O (The three is at the biggining of the
word))

I would like to:

For each word in the selection (after I selected something) chek if
there is
a number sticked (and after) the first and or second and or third
...... letter; if so the to change all the numbers in that word to
subscript numbers.
I dont have any experience working with word VBA (some with excell
VBA)


thanks a lot
 
H

Helmut Weber

Hi Filo,

interesting, linguistics in a nutshell,
if you want a perfect solution, and just for fun, ;-)

then one would need what linguistis call a grammar,
that is a set of rules, which defines allowed sequences
of characters. You may call such a sequences "word".

Now, what are the rules for a word in this grammar?
1. They are enclosed in delimiters.
We may take Word's definition of a word for that.
2. Words do not start with a digit
3. Words contain at least one digit
(NaCl is no word then.)
3. The words parts are found in an enumeration,
which consists of all abbreviations for chemical elements.

Now it would be your part to supply the list...
including (Uun) Ununnilium, (Uuu) Unununium, (Uub) Ununbium ...

after that, I think, the task would be managable.
:)

A small macro, interacting with you,
without theoretical overhead,
might be faster nevertheless.

Sub Test0098()
Dim rTmp As Range
Dim rChr As Range
Dim rPly As Long
' Set rTmp = Selection.Range or for the whole doc
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Text = "[A-z]{1;}[0-9]{1;}"
.MatchWildcards = True
While .Execute
rTmp.Select ' for testing
rPly = MsgBox("change?", vbYesNo)
If rPly = vbYes Then
For Each rChr In rTmp.Characters
If IsNumeric(rChr) Then rChr.Font.Subscript = True
Next
End If
Wend
End With
End Sub

Have some fun.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Greg Maxey

Helmut,

We have been over this before ;-)

..Text = "[A-Z,a-z]{1,}[0-9]{1,}" 'Remember that there are six not alpha
characters between Z and a.
 
H

Helmut Weber

Hi Greg,
We have been over this before ;-)

yes, sure.
.Text = "[A-Z,a-z]{1,}[0-9]{1,}"
'Remember that there are six not alpha characters between Z and a.

I was stretching a point, maybe was lazy too,
as the macro would need human interaction anyway,
for mm3, cm2, mm2 etc.

Still I'm finding the possibly perfect solution very interesting,
but it might be (an expression i've learned here lately)
"more pain than gain". ;-)
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

....

urghh, always the same pain in the ***

.Text = "[A-z]{1;}[0-9]{1;}" ' German and others
.Text = "[A-z]{1,}[0-9]{1,}" ' US and others

and see Greg's remark as well:
.Text = "[A-Z,a-z]{1,}[0-9]{1,}"

HTH

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
D

Doug Robbins - Word MVP

It coverts

CH4 or H2SO4 but never 3H2O

to

CH4 or H2SO4 but never 3H2O

for me.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

filo666 said:
Thanks for the answer, the code does not work, it sends me automaticly to end
with and then to end

Doug Robbins - Word MVP said:
Use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[A-Z]{1}[0-9]{1}", Forward:=True, _
MatchWildcards:=True, MatchCase:=True, Wrap:=wdFindStop) = True
Set myrange = Selection.Range
myrange.start = myrange.start + 1
myrange.Font.Subscript = True
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

filo666 said:
Hello, I have very large documents with chemical formulas and notation
(ex.
CH4 or H2SO4 but never 3H2O (The three is at the biggining of the word))

I would like to:

For each word in the selection (after I selected something) chek if there
is
a number sticked (and after) the first and or second and or third .......
letter; if so the to change all the numbers in that word to subscript
numbers.
I dont have any experience working with word VBA (some with excell VBA)


thanks a lot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top