bkirkman56
08-07-2008, 01:39 PM
I am trying to iterate through selected text in Word 2003 and if it's a number round it to a whole number.
I would love to use Excel's Round() function but I don't see how to apply
it in a Word document.
Below I posted what has been the closest I've come to a solution. Problem I am encountering is that it does not move to the next selected word when I hit my first "number" in the selected text.
VBA Code - This is in a Sub :
...
' Round numbers
MsgBox Selection.Words.Count & " words are selected"
Dim wrd As Range
Dim num As Integer
num = 0
For Each wrd In Selection.Words
If IsNumeric(wrd) And Not IsEmpty(wrd) Then
Dim str As Variant
str = Int(wrd.Text)
wrd.Text = str
num = num + 1
End If
Next wrd
MsgBox num & " numbers were rounded"
...
Sample text that is "selected" in the word document:
<Tstate000:q_mi> Q-MI0032
<Tstate001:q_mi> MICHIGAN
<Tstate003:q_mi> 2,965.0
<Tstate005:q_mi> 18
<Tstate007:q_mi> 90.951
<Tstate009:q_mi> 90.959
<Tstate011:q_mi> PUERTO RICO
<Tstate013:q_mi> 150.0
<Tstate015:q_mi> 1
<Tstate017:q_mi> 4.601
<Tstate019:q_mi> 4.944
<Tstate021:q_mi> USA
<Tstate023:q_mi> 145.0
<Tstate025:q_mi> 1
<Tstate027:q_mi> 4.448
<Tstate029:q_mi> 4.097
I would love to use Excel's Round() function but I don't see how to apply
it in a Word document.
Below I posted what has been the closest I've come to a solution. Problem I am encountering is that it does not move to the next selected word when I hit my first "number" in the selected text.
VBA Code - This is in a Sub :
...
' Round numbers
MsgBox Selection.Words.Count & " words are selected"
Dim wrd As Range
Dim num As Integer
num = 0
For Each wrd In Selection.Words
If IsNumeric(wrd) And Not IsEmpty(wrd) Then
Dim str As Variant
str = Int(wrd.Text)
wrd.Text = str
num = num + 1
End If
Next wrd
MsgBox num & " numbers were rounded"
...
Sample text that is "selected" in the word document:
<Tstate000:q_mi> Q-MI0032
<Tstate001:q_mi> MICHIGAN
<Tstate003:q_mi> 2,965.0
<Tstate005:q_mi> 18
<Tstate007:q_mi> 90.951
<Tstate009:q_mi> 90.959
<Tstate011:q_mi> PUERTO RICO
<Tstate013:q_mi> 150.0
<Tstate015:q_mi> 1
<Tstate017:q_mi> 4.601
<Tstate019:q_mi> 4.944
<Tstate021:q_mi> USA
<Tstate023:q_mi> 145.0
<Tstate025:q_mi> 1
<Tstate027:q_mi> 4.448
<Tstate029:q_mi> 4.097