Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #16  
Old 03-08-2019, 03:20 AM
totoMSOF totoMSOF is offline Windows 7 64bit Office 2010
Novice
 
Join Date: Mar 2019
Posts: 11
totoMSOF is on a distinguished road
Default

Indeed: it works! Thank you for this code.



This is the first part of the job: in fact, I don't want to copy the reference as they appear but I use a dictionnary to put complete reference in the footnotes; this is not complicated, but the thing is that I must format the reference inside the footnote...

I've started to work on that, but before continuing, I am going to keep on looking for the portion of code that doesn't work, in order to understant better VBA, and to study the .Find method.

If anyone can help for that, by providing sources, sites, books reference or answering questions...

In any case, I acknowledge participants for their help!
Reply With Quote
  #17  
Old 03-08-2019, 04:17 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,367
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by totoMSOF View Post
I don't want to copy the reference as they appear but I use a dictionnary to put complete reference in the footnotes; this is not complicated, but the thing is that I must format the reference inside the footnote...
It would have been helpful if you said from the outset what it is you want to achieve. Without knowing where these references comes from or how you want them to be formatted, it's impossible to progress the matter.

Quote:
Originally Posted by totoMSOF View Post
I am going to keep on looking for the portion of code that doesn't work
Given that your opening statements was "Indeed: it works!", I have no idea what you mean by "the portion of code that doesn't work".
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #18  
Old 03-08-2019, 06:26 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 am working on formatting from the dictionnary. If I can't succeed, I will give more detail (maybe in another thread?) but for the moment, I want to search on my own.

The portion of code that doesn't work is mine (with regex) ! I wasn't far from what I was looking for, so I think the best way to improve my knowledge of VBA is to dig the subject...
Reply With Quote
  #19  
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
  #20  
Old 03-11-2019, 01:28 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,367
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by totoMSOF View Post
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.
The thread was marked as solved because it solves the problem. As you yourself said:
Quote:
Originally Posted by totoMSOF View Post
Indeed: it works! Thank you for this code.
The fact it didn't do so using a particular method that you've now shown yourself to be wedded to and is less well suited to the problem is immaterial. You own code is still way less efficient than the answer you were provided.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Tags
regex, replace

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
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
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


All times are GMT -7. The time now is 06:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft