Can't close documents programmatically

A

afswa

I have a routine which assembles various subdocuemtns in to a main container
documet. Each sub-document uses a mailmergs one item deep to update
information in itself before fileds are unlinked, set as a normal document
and then copied and pasted to the container document. It was working OK until
the number of sub documents grew to around the 250 mark. Doug Robbins on the
Mailmerge site suggested a replacement code not using SELECT. But it now
seems that each opened sub-document is not closing and therefore Word falls
over when its maximum document count is reached.

Relevant code section is:

For i = Marks To 1 Step -1
If Left(aMarksArray(i), 2) = "bk" Then
CurName = Right(aMarksArray(i), Len(aMarksArray(i)) - 2)
Set Source = Documents.Open(RawPath + CurName)
ViewType = wdNormalView
Application.Options.Pagination = False
LockFields
ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
Set Target = Documents.Open(ThisContainer)
Target.Bookmarks("bk" + CurName).Range.FormattedText =
Source.Range.FormattedText
Set Source = Documents.Close(RawPath +
CurName)(savechanges:=wdDoNotSaveChanges)
End If
Next


Where am I being dense?
 
B

Bear

AFSWA:

I'm guessing here, but try it out and see if it works.

In the first Set statement, you're linking your working object "Source" to a
given document. To close that document, you don't use another Set statement.
You just close it.

Source.Close (savechanges:=wdDoNotSaveChanges)

At the end of your routine, you'll also want to destroy the working object

Set Source = Nothing

That's just general housekeeping, not related to your 250 limit. Let me know
if that was a good guess -- otherwise I'm sure the list's VBA experts will
chime in.

Bear
 
Top