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