View Single Post
 
Old 03-11-2019, 08:11 AM
totoMSOF totoMSOF is offline Windows 7 64bit Office 2010
Novice
 
Join Date: Mar 2019
Posts: 11
totoMSOF is on a distinguished road
Default

I don't know who tagged this question SOLVED, because from my point of view, it was not. It was answered with another method than the one I was looking for. But I don't want people to give me fishes, I want to be taught to fish !
That's why I kept on digging the problem. Here it is a solution with the regex method.

After some tries, it appeared that the indexes thrown by the Execute method tend to shift by increasing multiple of 45 after some dozens of correct matches. For instance, the 22 first matches are correct, than the 18th next are shifted by 45 characters (before their exact place), than the 12 next are shifted by 90 characters (before...), and so on, to the 21 last which are shifted by 540 characters (= 45 x 12). Sometimes, for only one match, the shift is not exactly a multiple of 45, but one unit less...

I guess there are characters that put this shift from time to time, but I can't see where there are and I don't want to spend much time. If anyone has a clue about that, I will read carefully his idea .

So I changed the algorithm: instead of taking all the matches in one time and looping on the collection obtained, I change the regex in order to get the first match (Global property set to False), and I shift the range used to search that match by starting after the last match found (by the way, that allowed me to understand a little more how range works...). Of course, there is a great loss of time because the regex Execute searches to the end of the document over 700 times, but the code succeeds to perfectly place footnotes accross the whole document in a few dozains of seconds. Moreover, this duration includes a 2 printing 2 lines of debugging (by footnote created). So total time should decrease with some optimisation (may be the DoEvents is useless?). Showing the debug allows to see that the first regex Execute over almost all the document take a few 10th of second, whereas the last take only some 100th of seconds because the range is only a few pages...

Of course, the search method is right and certainly better in this case (I will study it later), but it doesn't answer the initial question about regex functioning.

Here is the code (with French names, sorry). Don't hesitate to comment about the clumsy things you see ...

Code:
Dim debut As Long
Dim fin As Long
Dim indexMatch As Long
Dim refCourte As String
Dim regTrouvees As MatchCollection
Dim rng As Range

Sub Remplacer_ref_2()

Set regex = New RegExp
With regex
    .IgnoreCase = False
    .MultiLine = True
    .Global = False
    .Pattern = " \(\[[A-Z][A-Z0-9-&*]+\](,[^\)]+|)\)"
End With

continuer = True
debut = 0

While continuer
    fin = ActiveDocument.Range.End
    Set rng = ActiveDocument.Range(Start:=debut, End:=fin)
    Debug.Print "DEBUT = " & debut & ", FIN = " & fin
    Set regTrouvees = regex.Execute(rng)
    If regTrouvees.Count > 0 And debut < fin Then
        indexMatch = regTrouvees(0).FirstIndex
        refTexte = regTrouvees(0).Value
        Debug.Print refTexte & " et index = " & debut + indexMatch
        Selection.SetRange Start:=indexMatch + debut, End:=indexMatch + debut
        Selection.Collapse Direction:=wdCollapseStart
        Selection.Footnotes.Add Range:=Selection.Range, Text:=refTexte
        debut = debut + indexMatch + 20
        Set regTrouvees = Nothing
    Else
        continuer = False
    End If
    DoEvents
Wend
MsgBox "FIN !"
End Sub
Reply With Quote