Hello everyone,
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 vbtab / Chr(9) before a number, and then determine if any [0-9] following the tab need to be replaced.
I have drafted the following code:
Code:
Sub ReplaceNumbersWithWords2()
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
I also hoped that the replaced numbers should be with track changes or in wdRed color.
May I ask if anyone can kindly assist me in achieving this task? Thank you very much in advance.