View Single Post
 
Old 03-03-2019, 04:17 AM
totoMSOF totoMSOF is offline Windows 7 64bit Office 2010
Novice
 
Join Date: Mar 2019
Posts: 11
totoMSOF is on a distinguished road
Default Regex over 700 matches in a long doc

Hello,
I have to modify more than 700 biblio references to put them into footnotes in a document of hundred pages. So, I'm looking at VBA Word, but I 'm almost totally newbie. Documentation seems hard to find, especially as regards to basis (object model, etc.).
Here it is what I've tried and the major problem I'm facing with described after. The sketch of the code used is as follows:

Code:
Sub Replace_ref_1()

' Definition of all variables
' ...

Set docRange = ActiveDocument.Range

' Definition of the Regex needed  
With regEx
   ... ' Definition with required options etc. => OK
End With

Options.Overtype = False ' Insert and not delete
ActiveDocument.TrackRevisions = False ' Out of tracking changes mode 
' Regex matchs 
Set regFound = regEx.Execute(docRange)

' Loop over results, going by the last (highest index)
For cpt = regFound.Count To 1 Step -1
    refText = regFound(cpt - 1)
    Selection.SetRange Start:=regFound.Item(cpt - 1).FirstIndex, End:=regFound.Item(cpt - 1).FirstIndex
    Selection.Collapse Direction:=wdCollapseStart
    Selection.Footnotes.Add Range:=Selection.Range, Text:=LTrim(refText)
Next cpt

MsgBox "Number of matchs processed = " & regFound.Count

End Sub
The idea is to use the index of each match (which is, if I understand it well, the number of the begining character of the matched string) to insert a footnote at this place.
I meet the following problem: numbers (mark) of footnotes are not inserted at the correct place. In general, a few to some dozens of characters before expected. I thought it was due to the fact that adding footnotes implied adding characters to the text, and so the index couldn't be OK. That's why I tried processing from the last to the first: cf. "downto" loop. (I checked before this try that matches were in increasing index by writing them in a file.) But this is not better...

I noticed also that for a portion of text with only 3 matches, it's OK. But as soon as the text is too long, it doesn't work, either with the first or the last matches. I also noticed that it is better if I remove the summary (which lies at the begining), but even with that "trick", for a portion of doc with 133 matches, only the 57 first are OK, and all that follow are shifted (without any change in format between the 57th and 58th match...).

I guess there may be a problem in my understanding of index, or, moreover, in the portion of text included in the indexes...

I would be glad if someone could help me about that.

Thanks .
Reply With Quote