Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-03-2019, 04:17 AM
totoMSOF totoMSOF is offline Regex over 700 matches in a long doc Windows 7 64bit Regex over 700 matches in a long doc Office 2010
Novice
Regex over 700 matches in a long doc
 
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
 

Tags
regex, replace

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex over 700 matches in a long doc Using VB Regex feature, I tried to replace 'the' and 'this' with 'that' but got screwed abdan Word VBA 3 01-18-2019 09:38 PM
How to compare 2 Excel sheets for 100+ matches? dylansmith Excel 5 05-22-2017 09:09 PM
Macro help regex subspace3 Word VBA 1 10-15-2014 09:53 AM
Regex over 700 matches in a long doc Convert RegEx to Word (Devanagari Font Find/Replace) gasyoun Word VBA 9 04-12-2013 04:15 PM
Regex in Word: Replaced strings are in disorder chgeiselmann Word 0 04-26-2009 11:33 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:24 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft