View Single Post
  #9   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

For ease of access to the Word (and other Microsoft) forums see
http://www.gmayor.com/MSNews.htm

--

Graham Mayor - Word MVP

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


RPMitchal wrote:
Graham:

At this stage of my VBA learning status I find it somewhat scary that
there are those of you who are "amused" by coming up with applicable
coding, but can understand the challenge of concocting resolutions
for those of us who depend on you all so greatly. However, the
thought does provide me with something towards which I might strive.

You mentioned a VBA forum. Is that synonymous with the "Word
Progamming" category within this forum or something entirely
different?

In the meantime, I will check out the web site for some of those VBA
snippets that you suggested.

Once again, my thanks to you and all of your compadres within this
particular forum for all that you do.

Rod


"Graham Mayor" wrote:

There are lots of web sites that reference snippets of vba code -
including my own, and I keep adding them from time to time, but
creating answers to problems is a mental exercise that keeps us
amused when we have nothing better to do and as others refine our
code we all learn from the experience. You would however be better
posting vba questions in a vba forum.

Thanks for your holiday wishes, but I live on the other side of the
pond. Here the main annual holiday is Easter.

--

Graham Mayor - Word MVP

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



RPMitchal wrote:
Graham:

Thank you so very much for providing both of these macros. As soon
as I am able, I am going to try them out.

You folks out there who contribute to this forum constantly amaze me
with your abilities. I sure wish that one of you (or even a
collaboration) would take it upon yourselves to write a book which
focuses exclusively on VBA for Word using "real world" examples.

I have a series of about five books that I've attempted to use over
the last couple of years in an attempt to get at least a rudimentary
understanding of VBA for Word and have yet to stumble upon one that
brings the learning process "home for me".

Alternatively, if you know of a book that you would recommend, I
would be forever beholden to you or whomever. In the meantime, I
shall continue to rely upon all of you collective gurus out there in
order to have my dilemmas so billiantly addressed.

If by chance you're a "Yankee" enjoy your Thanksgiving holiday.

With much appreciation...

Rod




"Graham Mayor" wrote:

On further reflection, if you know what the numbers are the second
macro would do the lot.

--

Graham Mayor - Word MVP

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


Graham Mayor wrote:
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



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