Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 11-01-2021, 04:26 PM
davidjm davidjm is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Novice
how do I replace string 123-4 with 123 4
 
Join Date: Jun 2018
Posts: 18
davidjm is on a distinguished road
Default

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
Then I need to run a second routine to update all the rollover trailing page numbers and keep the same number of digits as the original

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
If you could just complete the above routines to update the original file, it would be very much appreciated

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
Reply With Quote
  #17  
Old 11-02-2021, 03:03 AM
davidjm davidjm is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Novice
how do I replace string 123-4 with 123 4
 
Join Date: Jun 2018
Posts: 18
davidjm is on a distinguished road
Default

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
but it's throwing a a Type mismatch error 13 on
Code:
      int1 = CInt(arrStr(0)) + 2
Why is that? What am I doing wrong?
Reply With Quote
  #18  
Old 11-02-2021, 04:16 AM
Guessed's Avatar
Guessed Guessed is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,993
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

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
Reply With Quote
  #19  
Old 11-02-2021, 05:35 AM
davidjm davidjm is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Novice
how do I replace string 123-4 with 123 4
 
Join Date: Jun 2018
Posts: 18
davidjm is on a distinguished road
Default

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
The code runs without error, but I think it's not finding
Code:
    .Text = "-[0-9]{1,3}"
'cause
Code:
Do While .Execute = True
Jumps straight to end.
Any ideas?
Thanks
Reply With Quote
  #20  
Old 11-02-2021, 08:43 AM
davidjm davidjm is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Novice
how do I replace string 123-4 with 123 4
 
Join Date: Jun 2018
Posts: 18
davidjm is on a distinguished road
Default

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
One last question how would I combine these 2 sub()'s into 1, so that I process the index in 1 pass?

Thanks for all your help
Reply With Quote
  #21  
Old 11-02-2021, 05:06 PM
Guessed's Avatar
Guessed Guessed is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,993
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

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
Reply With Quote
  #22  
Old 11-02-2021, 11:13 PM
davidjm davidjm is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Novice
how do I replace string 123-4 with 123 4
 
Join Date: Jun 2018
Posts: 18
davidjm is on a distinguished road
Default

Thanks for this.
How would I find a numeric string not followed by [-–—]
Reply With Quote
  #23  
Old 11-03-2021, 01:00 AM
Guessed's Avatar
Guessed Guessed is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,993
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

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
Reply With Quote
  #24  
Old 11-03-2021, 04:27 AM
davidjm davidjm is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Novice
how do I replace string 123-4 with 123 4
 
Join Date: Jun 2018
Posts: 18
davidjm is on a distinguished road
Default

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
Reply With Quote
  #25  
Old 11-03-2021, 02:33 PM
Guessed's Avatar
Guessed Guessed is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,993
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

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
Reply With Quote
  #26  
Old 11-03-2021, 02:40 PM
davidjm davidjm is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Novice
how do I replace string 123-4 with 123 4
 
Join Date: Jun 2018
Posts: 18
davidjm is on a distinguished road
Default

OK, great thanks
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do I replace string 123-4 with 123 4 Replace characters in a string Marcia Excel Programming 5 05-04-2020 05:15 AM
how do I replace string 123-4 with 123 4 Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
how do I replace string 123-4 with 123 4 Replace multiple strings by one same string at once puff Word 2 02-28-2018 11:04 AM
how do I replace string 123-4 with 123 4 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:55 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