Use Macro(or VBA) to automatically replace m2 into m2(superscript)

S

Sylvester

I have many technical documents and am asked to replace all m2 into
m2(superscript).
The <Tool> -> <AutoCorrect> method is not applicable since all these
documents are existed.
Can I use VBA or macro to simply replace all "m2" into "m2(superscript" ?
p.s. I knew the way replacing m2 into mxxxx then replace xxxx into
superscript 2. It works but I have so many m2 or m3 or ft2 or ft3 or
H2SO4.........etc, so I have to do it so many times for only a single
document.
 
D

Doug Robbins - Word MVP

Use Edit>Replace and enter m2 if the find what control and in the replace
with control, enter m and then hold down the Alt key and type 0178 on the
numeric keypad. That results in ² being inserted. Then click on replace
all. For ³, hold down the Alt key and type 0179 on the numeric keypad.

--
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
 
H

Helmut Weber

Hi Sylvester,

if you want a more general solution,
things will get complicated and the problem
might be impossible to solve without some human interaction,
or an endless list of what to convert to what.

For formatting numbers, better said, IMHO, digits,
in strings like H2SO4 as subscript,
select the formula you want to replace
through the entire document, and then:

Sub Test456()
Dim RngDcm As Range
Dim strTmp As String
Dim chrTmp As Range
Set RngDcm = ActiveDocument.Range
strTmp = Trim(selection.Text)
With RngDcm.Find
.Text = strTmp
While .Execute
RngDcm.Select ' for testing remove laster
For Each chrTmp In RngDcm.Characters
If IsNumeric(chrTmp.Text) Then
chrTmp.Font.Subscript = True
End If
Next
RngDcm.start = RngDcm.End
RngDcm.End = ActiveDocument.Range.End
Wend
End With
End Sub

You may need a second macro, taking care of ft3, m2, c2,
not to speak of a43, whereby 43 should be superscripted.

I'd set up a function "isdigit" instead.

Also note, that ³, e.g., is not numeric,
according to the function "isnumeric".

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

It may be that, digits following small letters
have to be superscripted and digits following
capital letters have to subscripted.

Then another approach would be possible.
 
T

Tim of Calgary

If you have to do this for a bunch of items in the same document, here is the
shell of a macro to do so.

With selection.find
.Text = "m2"
.Replacement.Text = "mx2x"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll

.Text = "m3"
.Replacement.Text = "mx3x"
.Execute Replace:=wdReplaceAll

.Text = "H2SO4"
.Replacement.Text = "Hx2xSOx4x"
.Execute Replace:=wdReplaceAll

.... (and so on for all superscripts) ...

.Replacement.Font.Superscript = True
.Replacement.Font.Subscript = False
.Text = "x([0-9]{1,5})x"
.Replacement.Text = "\1"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

This works if you are replacing a bunch of standard abbreviations (like
chemical names). If an item is not found it is ignored so it is generally
safe to use in multiple documents.

It can also be expanded by assigning say "x2x" for a superscript 2 and "y2y"
for a subscript 2. Then add

.Replacement.Font.Superscript = False
.Replacement.Font.Subscript = True
.Text = "y([0-9]{1,5})y"
.Replacement.Text = "\1"

inside the With statement.

That make sense?

Tim Smith
MS-Word Geek
 

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