How to check the number format after Tab?

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

How to check the number format after Tab?

Post by yanlok1345 »

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 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
Could you kindly assist me in achieving this task? Thank you very much in advance.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to check the number format after Tab?

Post by HansV »

If you had a paragraph like this

    1 plus 1 is more than 2!

the result would be

    one plus 1 is more than 2!

Is that really what you want?
Best wishes,
Hans

snb
4StarLounger
Posts: 586
Joined: 14 Nov 2012, 16:06

Re: How to check the number format after Tab?

Post by snb »

Code: Select all

With activedocument
  For j=0 to 9
    .content=replace(.content,vbcr & j & " ", vbcr & array("zero","one","two","three","four","five","six","seven","eight","nine")(j) & " ")
  Next
End With

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to check the number format after Tab?

Post by HansV »

@snb: yanlok also wants to replace a digit after a tab character.
Best wishes,
Hans

snb
4StarLounger
Posts: 586
Joined: 14 Nov 2012, 16:06

Re: How to check the number format after Tab?

Post by snb »

It must be possible for everyone working in VBA to adapt my suggestion to his/her own needs.
The 'how' is shown, the 'what' may be adapted.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to check the number format after Tab?

Post by HansV »

@snb: Two additional remarks to be dismissed:

1) Your code removes all formatting.
2) It adds 10 paragraph breaks at the end of the document.
Best wishes,
Hans

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: How to check the number format after Tab?

Post by yanlok1345 »

HansV wrote:
09 Jan 2024, 11:01
If you had a paragraph like this

    1 plus 1 is more than 2!

the result would be

    one plus 1 is more than 2!

Is that really what you want?
Yes! I just need to replace the first number at the start of every paragraphs.

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: How to check the number format after Tab?

Post by yanlok1345 »

snb wrote:
09 Jan 2024, 12:10
It must be possible for everyone working in VBA to adapt my suggestion to his/her own needs.
The 'how' is shown, the 'what' may be adapted.
Thank you for your valuable input and suggestion. I completely agree with your perspective that it should be feasible for every VBA practitioner to tailor the given suggestion to suit their individual requirements.

I appreciate your contribution to the forum discussion and your emphasis on empowering individuals to adapt solutions to their unique circumstances. This collaborative spirit is what makes forums like this so valuable, as we can learn from each other's experiences and find adaptable solutions that cater to our diverse needs.