I hope you're all doing well. I am currently working on a Word macro and I would appreciate your assistance with a specific task. My goal is to automatically identify whether the first character of paragraphs, particularly after a tab, is a number or not.
For instance, in the following example:
1 plus 1 is equal to more than two! As we begin the new year, let's take a moment to reflect on our accomplishments from the previous year and set our goals for the future. The first quarter of the year is crucial for our team as we have several important projects and deadlines to meet. It's vital to maintain open lines of communication among team members to ensure smooth collaboration and timely updates.
If a number [0-9] appears at the beginning of a paragraph, I would like to replace it with the corresponding word:
1 = one
2 = two
3 = three
4 = four
Since there may be other instances of numbers [0-9] in the document, a complete find and replace function is not suitable for this situation. Instead, the word macro should first identify the presence of a tab before a number, and then determine if any [0-9] following the tab need to be replaced.
I have drafted the following code, but unfortunately, it has not been successful:
Code: Select all
Sub ReplaceNumbersWithWords()
Dim para As Paragraph
Dim text As String
Dim i As Long
For Each para In ActiveDocument.Paragraphs
text = para.Range.Text
If Left(text, 1) = Chr(9) Then
i = 2
While Mid(text, i, 1) >= "0" And Mid(text, i, 1) <= "9"
Select Case Mid(text, i, 1)
Case "0"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "zero")
Case "1"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "one")
Case "2"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "two")
Case "3"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "three")
Case "4"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "four")
Case "5"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "five")
Case "6"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "six")
Case "7"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "seven")
Case "8"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "eight")
Case "9"
para.Range.Text = Replace(para.Range.Text, Mid(text, i, 1), "nine")
End Select
i = i + 1
Wend
End If
Next para
End Sub