PDA

View Full Version : [SOLVED:] count characters between .Find results



glencoe
02-02-2014, 04:59 PM
This is a general question: Is there a way to know how many characters there are between any two results found by a .Find search?
Since I found through a few other threads in the forum that my code is ridiculously slow and far from optimized (as I am most of the time not using the best functions available), I am rethinking some of my code to improve its performance.
Now, instead of checking manually how many characters there are between two similar strings found by a .Find search, I'd like to know how I can improve that.

westconn1
02-03-2014, 02:07 AM
try like



Dim r As Range, fnd As Range
Set r = olddoc.Content
Set fnd = olddoc.Content
With fnd.Find
.Execute ("Dear")
End With
r.Start = fnd.Start
Set fnd = olddoc.Content
With fnd.Find
.Execute ("Yours Sincerely")
r.End = fnd.End
End With taken from another thread, where odoc is a document object variable
characters between first and second fnd (including find strings) would be r.end - r.start
to exclude find strings change to r.start = fnd.end and r.end = fnd.start

glencoe
02-03-2014, 06:23 AM
Thank you! Looks like a clever and fast way to do it indeed! I will try it shortly! :-)

glencoe
02-04-2014, 06:00 PM
I am now trying to integrate this loop within another one (given by macropod), but I couldn't get the proper range for headers.

From the code below, what change should I bring to get your loop working?



Dim oSection As Section
Dim HdFt As HeaderFooter

With ActiveDocument
For Each oSection In .Sections
For Each HdFt In oSection.Headers
With HdFt
If .LinkToPrevious = False Or oSection.Index = 1 Then
''' your loop would go here
end if
end with
end for
end for
end with

westconn1
02-05-2014, 02:50 AM
afaik while each section has three headers and footers, they are the same for all sections
you can easily test if this is the case

as i am unsure if headers and footers are part of the document content, probably better to use the header range

set r = hdft.range
set fnd = hdft.range (in 2 places)

rest should stay the same, as it all works with the range objects

glencoe
02-05-2014, 07:41 AM
That's perfectly correct indeed!
I still have a small bug to fix in my code, but so far, your loop works fine. Thanks!

glencoe
02-07-2014, 05:43 AM
Thanks westconn1 (http://www.vbaexpress.com/forum/member.php?2465-westconn1) for your help. I fixed my bug now.