![]() |
|
#3
|
|||
|
|||
|
Here's the code:
Code:
Sub perLine()
' Loops thru lines in document passing each line to following Sub ModifyNumbersInText(line)
Static objRegex As Object
Static matches As Object
If objRegex Is Nothing Then Set objRegex = CreateObject("VBScript.RegExp")
Dim strSample As String
Dim para As Paragraph
Dim sentence() As String
For Each para In ActiveDocument.Paragraphs
sentence() = Split(para.Range.Text, Chr(11))
For i = 0 To UBound(sentence)
strSample = sentence(i)
With objRegex
.Pattern = "(\-)(\d)"
.Global = True
.IgnoreCase = True
strSample = .Replace(strSample, " X $2")
End With
ModifyNumbersInText (strSample)
Next i
Next
End Sub
Sub ModifyNumbersInText(line)
'Adds 2 to all numerical values in line
Dim txt As String
Dim NumbersFound As String
Dim NumberArray As Variant
txt = line
'Use Regular Expressions to pull out numbers
Set objRegex = CreateObject("VBScript.RegExp")
objRegex.IgnoreCase = True
objRegex.Global = True
objRegex.Pattern = "\d+" 'grab one or more digits
If objRegex.Test(txt) Then
Set Result = objRegex.Execute(txt)
If Result.Count > 0 Then
For Each match In Result
NumbersFound = NumbersFound & ";" & match.Value
Next match
Else
Exit Sub
End If
End If
'Create Array out of Numbers Found
NumbersFound = Right(NumbersFound, Len(NumbersFound) - 1)
NumberArray = Split(NumbersFound, ";")
'Find/Replace (add 2)
For x = LBound(NumberArray) To UBound(NumberArray)
If NumberArray(x) > 30 Then
txt = Replace(txt, NumberArray(x), NumberArray(x) + 2)
End If
Next x
'Output result
Debug.Print txt
End Sub
Code:
strSample = sentence(i)
With objRegex
.Pattern = "(\-)(\d)"
.Global = True
.IgnoreCase = True
strSample = .Replace(strSample, " X $2")
End With
Running Sub perLine() doesn't do the Regex replace above. Snippet from Word document that I'm trying to update: adaptive-expectations hypothesis 261–2, 305 aggregate demand 232–3, 305 aggregate supply 234–7, 305 aggregate supply curve 231 perfectly elastic 235 perfectly inelastic 235–6 aggregates levy 169 Thanks for any help |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Replace characters in a string
|
Marcia | Excel Programming | 5 | 05-04-2020 05:15 AM |
Wildcard replace any string in context with a specified string
|
wardw | Word | 7 | 05-07-2018 09:13 AM |
Replace multiple strings by one same string at once
|
puff | Word | 2 | 02-28-2018 11:04 AM |
Replace characters in a string
|
Anthon | Excel Programming | 1 | 11-03-2016 12:48 AM |
| How to do multiple find and replace in string. | PRA007 | Word VBA | 2 | 01-06-2016 09:10 PM |