Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

Can't close documents programmatically

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
afswa - 01 Jun 2007 10:25 GMT
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?
Bear - 01 Jun 2007 14:52 GMT
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
Signature

Windows XP, Word 2000

> 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
[quoted text clipped - 24 lines]
>
> Where am I being dense?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.