Help: how to release reference to document?

W

ward

Hello,

It seems that after using the clipboard on a document
(using .cut and .paste), that some reference to (an object
from) the document is kept alive.

I can see this when i execute the code below. When i look
at windows explorer, the lock file (~$ileA.doc) is still
there after the code has been executed.

If i jump out of the CopyTocToEndOfDOc procedure before
using the rng.cut method, the file (fileA.doc) is properly
released.

Anybody knows how i can release the document properly? Or
how i can kill the (clipboard?) object that has a
reference to the document?

thanks
Ward



---- CODE -----

Option Explicit
Sub test()

Dim tDoc As Document

Set tDoc = Documents.Open("c:\temp\booktest\fileA.doc")

CopyTocToEndOfDoc tDoc

tDoc.SaveAs "c:\temp\booktest\fileA.doc"
tDoc.Close


End Sub
Private Sub CopyTocToEndOfDoc(mdoc)

Dim TOC As TableOfContents
Dim rng As Range

'Logger.Log "Start copying TOC to end of doc.", lvdebug

'Select toc
Set TOC = mdoc.TablesOfContents.Item(1)

Set rng = TOC.Range

'Cut toc
rng.Cut

'Go to end of document
'Selection.EndKey Unit:=wdStory
Set rng = mdoc.StoryRanges(wdMainTextStory)
rng.Collapse Direction:=wdCollapseEnd

'Paste toc on new page
rng.InsertBreak Type:=wdPageBreak
rng.Paste

'Update toc
Set TOC = mdoc.TablesOfContents.Item(1)
TOC.Update

'Clean up
Set TOC = Nothing
Set rng = Nothing

'Logger.Log "TOC copied.", lvdebug

End Sub
 
J

Jean-Guy Marcil

Hi war,

I tried your code and could not recreate your situation.
(~$ileA.doc) was there during execution, but was gone at the end...

Are you sure you do not happen to have one there from an earlier
crash....When Word crashes, files like (~$ileA.doc) do not get deleted...
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
W

ward

Hello Jean-Guy,

Thanks for trying it out. But i'm absolutely sure it's not
the left-over from a crash. I'm struggling with this a
couple of weeks already. And i've tried different
situations and such, but the file only dissapears when i
close the project (document) that initiated the whole
thing.
Ward
 
P

Peter Hewett

Hi Ward

Instead of cutting and pasting the TOC use range objects to move the TOC.
Try something like this:

Public Sub MoveTOCToEOD()
Dim fldTOC As Word.Field
Dim rngEOD As Word.Range

With ActiveDocument

' Must have found the TOC field to move it
Set fldTOC = LocateTOCField()
If fldTOC Is Nothing Then Exit Sub

' Set destination range to the end of the document
Set rngEOD = .Content
rngEOD.Collapse wdCollapseEnd

' Grab the TOC field
fldTOC.Select

' Move the TOC without copying it
With Selection.Range
.TextRetrievalMode.IncludeFieldCodes = True
rngEOD.FormattedText = .FormattedText
End With

' Delete the original TOC
Selection.Range.Delete

' Now sort out the document and the TOC since
' it's page number will have changed
.Repaginate
Set fldTOC = LocateTOCField()
fldTOC.Update
fldTOC.Update
End With
End Sub

Private Function LocateTOCField() As Word.Field
Dim fldItem As Word.Field

' Locate and return the TOC field if present
For Each fldItem In ActiveDocument.Fields
If fldItem.Type = wdFieldTOC Then
Set LocateTOCField = fldItem
Exit For
End If
Next
End Function

The code works, but you'll need to try it to see if it fixes your problem.

HTH + Cheers - Peter
 
J

Jean-Guy Marcil

Hi Peter,

I am curious as to why using the selection object on Ward's machine did not
delete the temp file as he described, but the exact same code on my machine
did not display the behaviour encountered by Ward.
Also, how is it that not using the copy/paste selection solved his problem?
What is the link between copying and pasting of a selection object and the
temp file generated by Word?

Is it just one of those unexplainable things about Word?
Just curious!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Peter Hewett

Hi JG

Curious, not? You'd think that once the object was on the clipboard that
would be the end of it! These day's I don't try to understand every Word
quirk, especially if I can't reproduce it. I just try to come up with an
alternate method that gets to the same destination.

I think that should be - "It's yet another inexplicable, undocumented,
inconsistently reproducible Word Feature!!! Don'tcha just lurve it!

Cheers - Peter
 
J

Jean-Guy Marcil

Thanks Peter,

I just wanted to make sure it was a quirk and not some technical feature or
programming procedure I was missing out on...
So, I agree with you, it is not worth the effort investigating an
irreproducible quirk!
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
W

ward

Later on I found another procedure using the
same .copy .paste technique (but this time copying between
two different documents)

Same result (problem) there. An again, after implementing
Peter's solution, all was fine :)
 
W

Word Heretic

G'day "ward" <[email protected]>,

You MUST destroy all objects, thus end your sub with

Set tDoc = Nothing

Do the same for anything defined as any object.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


ward reckoned:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top