![]() |
|
#16
|
|||
|
|||
|
Hi Andrew, I've had a think about this and have decided to only add 2 to all page numbers and keep the format of the rollover numbers as they are (number of digits is unchanged). I wasn't asked to fix the index and as you say Word has built-in functionality for indexing. So using your code I have the start of 2 routines I need to run, 1st updates all page numbers that are not rollover trailing page numbers Code:
Sub FindPattern()
Dim aRng As Range, str As String, int1 As Integer, int2 As Integer, arrStr() As String, msg As String, x As String
Set aRng = ActiveDocument.Range
With aRng.Find
.ClearFormatting
.MatchWildcards = True
.Text = " [0-9]{2,3}"
Do While .Execute = True
arrStr = Split(aRng.Text, " ")
For i = LBound(arrStr) To UBound(arrStr)
If IsNumeric((arrStr(i))) Then
int1 = CInt(arrStr(i)) + 2
End If
Next i
' Need help here to update the index fields
' int1 = CInt(arrStr(0)) + 2
' int2 = CInt(arrStr(1)) + 2
' aRng.Text = int1 & "-" & int2
' aRng.Collapse Direction:=wdCollapseEnd
' aRng.End = ActiveDocument.Range.End
Loop
End With
End Sub
Code:
Sub FindToPage()
Dim aRng As Range, str As String, int1 As Integer, int2 As Integer, arrStr() As String, msg As String, x As String
Set aRng = ActiveDocument.Range
With aRng.Find
.ClearFormatting
.MatchWildcards = True
.Text = "–[0-9]{1,3}"
Do While .Execute = True
arrStr = Split(aRng.Text, "–")
For i = LBound(arrStr) To UBound(arrStr)
If IsNumeric(arrStr(i)) Then
If CInt(arrStr(i)) > 7 And CInt(arrStr(i)) < 10 Then
int1 = CInt(arrStr(i)) - 8
Else
int1 = CInt(arrStr(i)) + 2
End If
End If
Next i
Loop
End With
End Sub
This index was created manually so just updating the page numbers is enough. Thanks for your time and all your input, I've learned a great deal |
|
#17
|
|||
|
|||
|
Hi,
Trying to run this code Code:
Sub FindTemplate()
Dim aRng As Range, str As String, int1 As Integer, int2 As Integer, arrStr() As String
Set aRng = ActiveDocument.Range
With aRng.Find
.ClearFormatting
.MatchWildcards = True
.Text = " [0-9]{2,3}"
Do While .Execute = True
arrStr = Split(aRng.Text, " ")
int1 = CInt(arrStr(0)) + 2
aRng.Text = " " & int1
aRng.Collapse Direction:=wdCollapseEnd
aRng.End = ActiveDocument.Range.End
Loop
End With
End Sub
Code:
int1 = CInt(arrStr(0)) + 2 |
|
#18
|
||||
|
||||
|
You don't need to split the found string when it is just one number
Code:
Sub FindTemplate()
Dim aRng As Range, str As String, int1 As Integer, int2 As Integer, arrStr() As String
Set aRng = ActiveDocument.Range
With aRng.Find
.ClearFormatting
.MatchWildcards = True
.Text = " [0-9]{2,3}"
Do While .Execute = True
int1 = CInt(Trim(aRng.Text)) + 2
aRng.Text = " " & int1
aRng.Collapse Direction:=wdCollapseEnd
aRng.End = ActiveDocument.Range.End
Loop
End With
End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#19
|
|||
|
|||
|
Thanks very much, that worked a treat,
now I'm having problems with the second part Code:
Sub FindTemplateToPage()
Dim aRng As Range, str As String, int1 As Integer, int2 As Integer, arrStr() As String
Set aRng = ActiveDocument.Range
With aRng.Find
.ClearFormatting
.MatchWildcards = True
.Text = "-[0-9]{1,3}"
Do While .Execute = True
int1 = CInt(Replace(aRng.Text, "-", ""))
If int1 > 7 And int1 < 10 Then
int1 = int1 - 8
Else
int1 = int1 + 2
End If
aRng.Text = "-" & int1
aRng.Collapse Direction:=wdCollapseEnd
aRng.End = ActiveDocument.Range.End
Loop
End With
End Sub
Code:
.Text = "-[0-9]{1,3}"
Code:
Do While .Execute = True Any ideas? Thanks |
|
#20
|
|||
|
|||
|
This took care of it:
Code:
Sub FindTemplateToPage()
Dim aRng As Range, str As String, int1 As Integer, int2 As Integer, arrStr() As String
Set aRng = ActiveDocument.Range
With aRng.Find
.ClearFormatting
.MatchWildcards = True
.Text = Chr(150) & "[0-9]{1,3}"
Do While .Execute = True
int1 = CInt(Replace(aRng.Text, Chr(150), ""))
If int1 > 7 And int1 < 10 Then
int1 = int1 - 8
Else
int1 = int1 + 2
End If
aRng.Text = Chr(150) & int1
aRng.Collapse Direction:=wdCollapseEnd
aRng.End = ActiveDocument.Range.End
Loop
End With
End Sub
Thanks for all your help |
|
#21
|
||||
|
||||
|
I would guess that your real doc has more than one type of hyphen/n-dash/m-dash showing the spans so perhaps you might consider picking all of them up with a wildcard
.Text = "[-–—]{1}[0-9]{1,3}" In which case you can get int1 more simply by just truncating the initial character int1 = CInt(Mid(aRng.Text, 2)) And If int1 > 7 And int1 < 10 Then would more logically be If int1 = 8 or int1 = 9 Then And you can combine the code from both passes into one macro by copy/paste or create a third macro that calls both the others Code:
Sub RunBoth() FindTemplateToPage FindTemplateToPageDash End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#22
|
|||
|
|||
|
Thanks for this.
How would I find a numeric string not followed by [-–—] |
|
#23
|
||||
|
||||
|
Why do you need to? The macro above that searches for
.Text = " [0-9]{2,3}" will find all 2 or 3 digit numbers preceded by a space (which is all of the numbers other than the ones preceded by a dash. I'm not sure why you specifically excluded the first nine pages from that search though. Is there no index entries on those pages?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#24
|
|||
|
|||
|
Hi Andrew,
Asked about "not followed by" for my personal knowledge and it's an alternative way to go if I wanted to use your original code. Pages 29 - 30 are new so before that there's no change. I haven't actually specified > page 28 'cause there's too many inconstancies in the format of the end (roll-over) page numbers. Seeing the index was created without Word's indexing tools, it'll be better to make these updates manually |
|
#25
|
||||
|
||||
|
For your own knowledge on using Wildcard variations I recommend you do a google search for "jack lyons wildcard cookbook eBook". That resource will show you everything you need to know on wildcard search strings including excluding characters.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#26
|
|||
|
|||
|
OK, great thanks
|
|
| Thread Tools | |
| Display Modes | |
|
|
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 |