Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2024, 09:05 PM
syl3786 syl3786 is offline Seeking guidance on using Word macros to find and replace Arabic numbers after indent Windows 10 Seeking guidance on using Word macros to find and replace Arabic numbers after indent Office 2019
Advanced Beginner
Seeking guidance on using Word macros to find and replace Arabic numbers after indent
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default Seeking guidance on using Word macros to find and replace Arabic numbers after indent

Hello everyone,



I hope this message finds you all well. I am currently working on a Word macro that aims to find and replace Arabic numbers specifically after an indent. Despite my efforts in searching through numerous forums and websites dedicated to Word macros, I have been unable to find relevant information to address my specific query.

I would like to share the Word macro I got from this forum thus far, which is intended to find and replace Arabic numbers following the Chr(9) character:

Code:
Sub Replace_Digits_With_Wds()

Dim oRng As range

Application.ScreenUpdating = False
Set oRng = ActiveDocument.range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchWildcards = True
        .text = Chr(9) & "1([!0-9])"
        While .Execute
            oRng.End = oRng.End - 1
            oRng = Chr(9) & "-"
            oRng.Font.ColorIndex = wdBrightGreen
            oRng.Collapse wdCollapseEnd
        Wend
Application.ScreenUpdating = True
Set oRng = Nothing
End With

End Sub
If you have a moment to spare, I would be immensely grateful if you could provide me with some guidance on how to resolve the aforementioned issues. Your expertise and insights would be of great value to me.

Here attached the original document and expected result document for your references:

original document.docx
Expected result document.docx

Thank you kindly for taking the time to read my message and considering my request. I appreciate your assistance.
Reply With Quote
  #2  
Old 01-11-2024, 09:55 PM
Guessed's Avatar
Guessed Guessed is offline Seeking guidance on using Word macros to find and replace Arabic numbers after indent Windows 10 Seeking guidance on using Word macros to find and replace Arabic numbers after indent Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Does it matter how large the indent is?
Is this for single or multiple digits? ie 0 to 9 only or should it include numbers with more than one digit?
What the number be followed by?
Does it matter if the found occurrence is NOT the start of the paragraph?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 01-11-2024, 11:12 PM
syl3786 syl3786 is offline Seeking guidance on using Word macros to find and replace Arabic numbers after indent Windows 10 Seeking guidance on using Word macros to find and replace Arabic numbers after indent Office 2019
Advanced Beginner
Seeking guidance on using Word macros to find and replace Arabic numbers after indent
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Does it matter how large the indent is?
Is this for single or multiple digits? ie 0 to 9 only or should it include numbers with more than one digit?
What the number be followed by?
Does it matter if the found occurrence is NOT the start of the paragraph?
Q: Does it matter how large the indent is?
A: .CharacterUnitFirstLineIndent = 2.03

Q: Is this for single or multiple digits? ie 0 to 9 only or should it include numbers with more than one digit?
A: 0 to 9 only, and should avoid replacing years like "2024", "1987".

Q: What the number be followed by?
A: Mostly text/punctuation marks

Q: Does it matter if the found occurrence is NOT the start of the paragraph?
A: The macro is expected to find and replace all Arabic numbers after the Indentation, which is at the beginning of the paragraphs. Please view the attached documents in this thread.

If you have any questions or concerns, don't hesitate to get in touch with me. Thank you.
Reply With Quote
  #4  
Old 01-12-2024, 12:30 AM
vivka vivka is offline Seeking guidance on using Word macros to find and replace Arabic numbers after indent Windows 7 64bit Seeking guidance on using Word macros to find and replace Arabic numbers after indent Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi! Here is a simple option. I used some ideas from a Guessed's code (https://www.msofficeforums.com/179302-post6.html). You can modifiy the code to meet your needs. Maybe someone can suggest something more elegant.
Code:
Sub Repl_Digits_If()
'In the selected range, replace all single digits that start
'specifically indented paragraphs with asterisks.

Dim oRng As range
Dim oPar As Paragraph
Dim arrText() As String
Dim oDgt As range

Application.ScreenUpdating = False
Set oRng = selection.range
arrText = Split("zero one two three four five six seven eight nine", " ")

    For Each oPar In oRng.Paragraphs
        If oPar.range.ParagraphFormat.FirstLineIndent >= 0.5 And _
            oPar.range.Characters(1) Like "[0-9]*" And _
            oPar.range.Characters(2) Like "[!0-9]*" Then
                Set oDgt = oPar.range.Characters(1)
                oDgt.text = arrText(oDgt.text)
                oDgt.Font.ColorIndex = wdBrightGreen
        End If
    Next oPar
Application.ScreenUpdating = True
Set oRng = Nothing
End Sub
Reply With Quote
  #5  
Old 01-12-2024, 06:42 PM
syl3786 syl3786 is offline Seeking guidance on using Word macros to find and replace Arabic numbers after indent Windows 10 Seeking guidance on using Word macros to find and replace Arabic numbers after indent Office 2019
Advanced Beginner
Seeking guidance on using Word macros to find and replace Arabic numbers after indent
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by vivka View Post
Hi! Here is a simple option. I used some ideas from a Guessed's code (https://www.msofficeforums.com/179302-post6.html). You can modifiy the code to meet your needs. Maybe someone can suggest something more elegant.
Code:
Sub Repl_Digits_If()
'In the selected range, replace all single digits that start
'specifically indented paragraphs with asterisks.

Dim oRng As range
Dim oPar As Paragraph
Dim arrText() As String
Dim oDgt As range

Application.ScreenUpdating = False
Set oRng = selection.range
arrText = Split("zero one two three four five six seven eight nine", " ")

    For Each oPar In oRng.Paragraphs
        If oPar.range.ParagraphFormat.FirstLineIndent >= 0.5 And _
            oPar.range.Characters(1) Like "[0-9]*" And _
            oPar.range.Characters(2) Like "[!0-9]*" Then
                Set oDgt = oPar.range.Characters(1)
                oDgt.text = arrText(oDgt.text)
                oDgt.Font.ColorIndex = wdBrightGreen
        End If
    Next oPar
Application.ScreenUpdating = True
Set oRng = Nothing
End Sub
Thank you very much for your assistance. I have tested it and observed that the program only modifies the numbers "1" to "one" and "9" to "nine" if they appear in a document after identification of all numbers from 1 to 9.

The issue with the provided code is that it attempts to access array elements using the value of oDgt.Text, which is a string representing a single character. However, the Split function used to populate the arrText array expects a full string as input, not just a single character. As a result, the code is not able to correctly replace the digits with their corresponding words.
Reply With Quote
  #6  
Old 01-13-2024, 03:07 AM
vivka vivka is offline Seeking guidance on using Word macros to find and replace Arabic numbers after indent Windows 7 64bit Seeking guidance on using Word macros to find and replace Arabic numbers after indent Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Maybe, you will not believe, but the macro works without problems for me. I've tested it on different combinations of digits, even excluding 1 and 9. Anyway, if you know the cause of malfunction, you can remove it. Or (better) out of curiosity, I'd like to see the problem part of your file, if you don't mind.
Reply With Quote
  #7  
Old 01-13-2024, 05:26 AM
syl3786 syl3786 is offline Seeking guidance on using Word macros to find and replace Arabic numbers after indent Windows 10 Seeking guidance on using Word macros to find and replace Arabic numbers after indent Office 2019
Advanced Beginner
Seeking guidance on using Word macros to find and replace Arabic numbers after indent
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by vivka View Post
Maybe, you will not believe, but the macro works without problems for me. I've tested it on different combinations of digits, even excluding 1 and 9. Anyway, if you know the cause of malfunction, you can remove it. Or (better) out of curiosity, I'd like to see the problem part of your file, if you don't mind.
Thank you for your response. It's important to note that macros can behave differently depending on individual computer settings, so variations in functionality are normal.

Additionally, I have discovered that when the cursor is positioned after an Arabic number, it will only replace that specific number. However, if the cursor is placed elsewhere and not immediately after the Arabic number following the identification, the macro will display a message box stating "Requested Member of the Collection Does Not Exist."
Reply With Quote
  #8  
Old 01-13-2024, 05:55 AM
vivka vivka is offline Seeking guidance on using Word macros to find and replace Arabic numbers after indent Windows 7 64bit Seeking guidance on using Word macros to find and replace Arabic numbers after indent Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, again! If you don't mind here are some comments to your previous post.
1) If the cursor (aka selection or insertion point) is within a word (in our case a digit) that wd is considered selected. 'Within', besides within proper, also inlcudes the positions immediately before and after the word, What is more: 'within' also includes the word's trailing space! In other words, if the cursor is placed immediately before or on, or immediately after the word, or immediately after the word's trailing space, the word is treated as selected. That's why your digit becomes replaced. A rule of a thumb I use: if I want to make sure what the word is, I just double-click on it (use this technique to get surprised). If the word is followed by space, that space is also selected. Other punctuation signs do not belong to the previous word, they are individual words.
2) If you have error 5941 ("Requested Member of the Collection Does Not Exist"), most likely there's a paragraph break/empty paragraph in the selected range, which 'confuses' the code. In this case, I'd suggest adding another condition
Code:
If Len(oPar.range) > 1 Then
right after
Code:
For Each oPar In oRng.Paragraphs
and, of course, adding
Code:
End If
before
Code:
Next oPar
3) I prefer using Selection to ActiveDocument, that's why I used Selection in the code, although you intended to work on the whole document. My reason is simple: ActiveDocument may be too much of a good thing when there's no need to change the whole document. At the same time, selection can be extended to the whole document, if need be, by pressing Ctrl+A with your left hand. As you see, for me Selection is more practical. It's only my opinion.

I hope the above tips/explanations will help and even "system-specific variations" of the code execution will disappear.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Seeking guidance on using Word macros to find and replace Arabic numbers after indent Potential Clashes in Macros - Seeking Assistance yanyan9896 Word VBA 2 10-09-2023 04:59 AM
Find/replace - symbol for all numbers or symbol letters (Word 97) Genericname1111 Word 10 11-10-2019 10:37 PM
Seeking guidance on using Word macros to find and replace Arabic numbers after indent Find and Replace Numbers Macro VBA in Microsoft Word Yotem189 Word VBA 3 09-20-2018 05:55 AM
Numbers format in word to arabic Mzbarca Word 1 02-28-2016 07:56 PM
Find and replace BETWEEN numbers WordUser2015 Word 4 12-19-2014 02:09 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:10 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft