![]() |
|
#1
|
|||
|
|||
![]()
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 May I ask if anyone can kindly assist me in achieving this task? Thank you very much in advance. |
#2
|
|||
|
|||
![]() Quote:
![]() |
#3
|
|||
|
|||
![]()
Hi, syl3786! Or you can use:
Code:
Sub Repl_Digits_W__Wds_3() 'In selected paragraphs, replace the digits that start tabbed paras with wds. Dim para As Paragraph Dim dgt As range Application.ScreenUpdating = False For Each para In selection.range.Paragraphs 'Search for only digits: If para.range.Characters(1) = Chr(9) And _ para.range.Characters(2) Like "[0-9]" And _ para.range.Characters(3) = Chr(32) Then Set dgt = para.range.Characters(2) Select Case dgt Case "0" dgt = "zero" Case "1" dgt = "one" Case "2" dgt = "two" Case "3" dgt = "three" Case "4" dgt = "four" Case "5" dgt = "five" Case "6" dgt = "six" Case "7" dgt = "seven" Case "8" dgt = "eight" Case "9" dgt = "nine" End Select End If Next para Application.ScreenUpdating = True End Sub |
#4
|
|||
|
|||
![]() Quote:
Code:
If para.range.Characters(1) = Chr(9) And _ para.range.Characters(2) Like "[0-9]" And _ para.range.Characters(3) = Chr(32) Then |
#5
|
|||
|
|||
![]()
It's strange, because I didn't have any problem with the code. Anyway, it's nice you have found a solution!
|
#6
|
||||
|
||||
![]()
Here is a shorter option which works for me and applies the red colour as you requested.
Code:
Sub Repl_Digits_W__Wds_3() 'In selected paragraphs, replace the digits that start tabbed paras with wds. Dim para As Paragraph, dgt As Range, arrText() As String arrText = Split("zero one two three four five six seven eight nine", " ") For Each para In Selection.Range.Paragraphs If para.Range.Text Like vbTab & "[0-9] *" Then 'Search for only single digits: Set dgt = para.Range.Characters(2) dgt.Text = arrText(dgt.Text) dgt.Font.ColorIndex = wdRed End If Next para End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
![]()
Guessed, your code is the best solution, I think! And your "[0-9] *" is brilliant! I live and learn.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Can't find and replace (Appendix + any number) | Muse1 | Word | 1 | 12-30-2023 08:36 AM |
![]() |
karkey | Word VBA | 2 | 08-02-2022 03:04 PM |
Find/Replace using format of cell | catflap | Excel | 1 | 09-11-2017 07:28 AM |
![]() |
Allen | Word | 5 | 11-20-2014 10:33 PM |
![]() |
winningson | Word | 3 | 01-19-2013 05:38 AM |