Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003 find every occurance of "Sample" 1

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have a 500 page document with ~1370 photos of Samples
but a few are missing from the doc. Each Photo has a caption under it "SAMPLE No.: PE 01" which is just typed in not using the caption feature.
I would like to make a list of all of those in the doc using VBA ???.
like so
SAMPLE No.: PE 01
SAMPLE No.: PE 02
SAMPLE No.: PE 04
...
...
SAMPLE No.: PE 1370

Thank you

DougP
 
Something like this might help:
Code:
Sub findcaptions(strFT)
    Selection.HomeKey (wdStory)
    Selection.Find.ClearFormatting
    Selection.Find.Text = strFT
    Selection.Find.Execute
    Do While Selection.Find.Found
        a = Selection.MoveEnd(wdWord, 4)
        Debug.Print (Selection)
        Selection.MoveRight
        Selection.Find.Execute
    Loop
End Sub
If you use "Sample" as strFT it will fine "Sample", then select that and 3 more words, and, in this case, print it in the Immediate Window.

_________________
Bob Rashkin
 
I have a 500 page document with ~1370 photos of Samples
but a few are missing from the doc.
A few of what?

Are you saying there are a few photos that do NOT have "SAMPLE No.: PE xxx"? And those are what you are trying to find?

As it stands: "I would like to make a list of all of those in the doc using VBA". OK, a list where?

NB: it is better to use Range.
Code:
Sub Yadda()
Dim ThisDoc As Document
Dim ListDoc As Document
Dim r As Range

Set ThisDoc = ActiveDocument
Set ListDoc = Documents.Add
Set r = ThisDoc.Range
With r.Find
   Do While .Execute(findtext:="SAMPLE No.: ", Forward:=True) = True
      r.MoveEndUntil Cset:=Chr(13)
      ListDoc.Range.InsertAfter Text:=r.Text & vbCrLf
      r.Collapse Direction:=wdCollapseEnd
   Loop
End With
End Sub
This assumes that each "SAMPLE No" terminates with a paragraph mark. In other words,

SAMPLE No.: PE 01
SAMPLE No.: PE 02
SAMPLE No.: PE 04
...
...
SAMPLE No.: PE 1370

each of those is a separate paragraph. If they are NOT, the above code needs to be adjusted.

What it does:

1. makes a Document object of the current (active) document
2. makes a Document object for a new document
3. makes a Range object of the (now) old document - the new document is now the Active document
4. runs through that range for "SAMPLE No.:"
5. when found, extends that Range to just before a paragraph mark using CSet
6. dumps that (as text) into the new document
7. collapses to the end of the current range
8. repeats 4 - 7 until it can not find "SAMPLE No."

Note: there is no need to switch between documents, as the code uses Range and Document objects and does not give a fig what is the active document. There is no need to select and copy anything.

The main issue - as I see it - is that

SAMPLE No.: PE 01
SAMPLE No.: PE 02
SAMPLE No.: PE 04
...
...
SAMPLE No.: PE 1370

has variable lengths after PE. 01 vs 1370

IF - and only IF - each one terminates with a paragraph mark, then the above code takes care of that. The found range is extended to just before the paragraph mark. It is then dumped at the end of the Range of the new document

With a paragraph mark added just to make it neat.

The result is a list of all "SAMPLE No.: PE xxx" in a new document.

faq219-2884

Gerry
My paintings and sculpture
 
fumei, it works perfect.
have a star
and since there are so many adding them to anaother document is better. It created a 25 page document with all the Samples numbers.

Oh happy happy joy joy


DougP
 
Always glad to hear someone has happy happy joy joy.

Just curious. How long did it take? Depending on how fast your machine is, it should not take too too long as it is doing all the processing behind the scene with Range and Document objects. Although....I have to say, a 500 page Word doc with ~1370 photos......

Ai Caramba!!!! I hope your machine is crammed full of RAM.

Frankly, ummmmmm, no offence, but this may be a wee bit abusive of Word. Sounds like more of a database thing than a word processing thing.

faq219-2884

Gerry
My paintings and sculpture
 
you mean to create the items it found from your code? I think it just took seconds if I recall.
And Yes I have 3 gig of RAM since I do a lot of CAD work too.
then I pasted the results from your code into Access and ran an unmatched query to give me the difference so I could see which ones were missing.
Thanks again it saved me tons of time :)



DougP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top