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
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