View Single Post
  #4   Report Post  
Posted to microsoft.public.word.docmanagement
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default Convert Text to Numbers and Vice Versa

This is easier said than done
The following macro will convert numbers 1 to 10 to their text equivalents.
It may be possible to produce neater code, but this will do the job.

Sub NumbersToText()
Dim vFindText As Variant
Dim i As Long

vFindText = Array("1", "2", "3", "4", _
"5", "6", "7", "8", "9", "10")
With Selection
With .Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
While .Execute
With Selection
If vFindText(i) = "10" Then
.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="=10 \*Cardtext", _
PreserveFormatting:=False
Else
.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="=" & _
Mid(vFindText(i), 2, 1) & " \*Cardtext", _
PreserveFormatting:=False
End If
.MoveLeft Unit:=wdCharacter, Count:=1
.Fields.Unlink
.Collapse wdCollapseEnd
End With
Wend
Next i
End With
End With
End Sub

I don't see any simple way of converting numbers written out as text to
numbers unless there are a finite number of numbered texts for which you
could search and replace. That being the case you could create a two column
table in a Word document - here D:\My Documents\Test\changes.doc - with the
numbers written out in the first column with their equivalents in the second
then run the following macro to replace items in the first column with those
in the second. It is going to take forever to run if you have a lot of
different numbers to cope with.

Sub ReplaceFromTableList()

Dim ChangeDoc As Document, RefDoc As Document
Dim ctable As Table
Dim oldpart As Range, newpart As Range
Dim i As Long

Set RefDoc = ActiveDocument
Set ChangeDoc = Documents.Open("D:\My Documents\Test\changes.doc")
Set ctable = ChangeDoc.Tables(1)
RefDoc.Activate
For i = 1 To ctable.Rows.Count
Set oldpart = ctable.Cell(i, 1).Range
oldpart.End = oldpart.End - 1
Set newpart = ctable.Cell(i, 2).Range
newpart.End = newpart.End - 1
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute findText:=oldpart, _
ReplaceWith:=newpart, _
replace:=wdReplaceAll, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue
End With
Next i
ChangeDoc.Close wdDoNotSaveChanges
End Sub


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org




The numbers

RPMitchal wrote:
Summer:

Thank you for your response.

I could see where using *search and replace* would be the quickest
approach if all of the numbers were the same. I.e., change all
instances of the number 10 to *ten*. Unless there's something you
may not yet have told me - like perhaps a method using wild cards,
field codes, switches or something along those lines.

I was actually hoping for a process that might be a bit more automatic
through the use of a macro, whereby the coding actually recognized a
selection as a textual number or vice versa and responded accordingly.

The possibility exists that I may be expecting too much in this
regard. However, I'll wait to see if any of the other resident gurus
*weigh in* on this situation.

Thanks for taking the time to respond.

Rod


"Summer" wrote:

Global Find/Replace would be quickest.
"RPMitchal" wrote in message
...
Word 2003.

I am working on a 500 page document and a working policy has been
put into place whereby all numbers between 1 and 10 should be
written out as text and
all textual numbers greater than 10 should appear as numbers.

For example, the number 5 should be written as *five* and the
textual word twelve should appear as 12.

Additionally, any numbers that appears between parenthases should be
removed
completely.

For example, in an instance that appears as *twelve (12)* the result
should
be merely the number 12.

Would it be possible to accomplish this conversion via a macro; or
even two
separate macros?

Hopefully, I've explained the situation in a way that makes sense.
It sure
would be a big help and save me quite a bit of time and tedium.

As always, thanks very much in advance.

Rod