Some hints:
Inside the loop you are using .GetFirstDocument
. Move that outside the loop (outside With vw
) and then use .GetNextDocument(doc)
inside the loop (as you are already doing).
Also, you are calling doc.Remove(True)
and then later trying to fetch the next document by refering to the doc
instance that you just deleted. To fix this, you can add another document instance such as nextDoc
and then use this as a temporary document while you delete the doc
instance:
Before you delete doc:
Set nextDoc = .GetNextDocument(doc)
After you have deleted doc:
Set doc = nextDoc