Why are Document Properties are corrupted ?

K

Klic

I am running the code below so that I can create a duplicate copy of the
current document.
(My actual macro is significantly more complicated but I've narrowed the
problem down to one line for illustration).

If the current document has been saved already then there is never a
problem, but if the document is new (i.e. never saved) then the macro works
OK the first time and then produces Error 75: Path/File Access Error.

However, if the 'problem line' is commented out the macro works fine EVERY
time even if the current document is new.

I can't figure out why setting a variable to the value of a document
property affects the way in which the temporary document is written to disk,
but it does.

This can be seen by placing a breakpoint on the Kill statement line and
viewing the TempCopy properties through File Manager (Explore). The first
run gives the standard General/Summary/Statistics/Contents and Custom tabs,
but if the problem line is included then only the General tab is shown. If
the problem line is commented out then the properties are always the same
(multiple tabs).

So, my question is, can anyone tell me why this is happening, and what I can
do to fix the problem.
(I could force the user to save any New document before running my macro -
but that's not ideal).

thanks,

Keith
================


Option Explicit

Sub Main()
Dim lLocal As Long
CreateTempDoc

'Line below causes the Kill statement to error with Err 75: Path/File
Access Error
'whenexecuted for SECOND time.
'When commented out the macro runs fine every time

lLocal = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
'<--- problem line

'Place a BreakPoint on the following line and check the properties of
the
'document "C:\Windows\Temp\TempCopy.doc"
'Properties of the TempCopy document contain
General/Summary/Statistics/Contents and Custom
'on the first run, but only General on the second

Kill "C:\Windows\Temp\TempCopy.doc"
End Sub

Private Sub CreateTempDoc()
'Courtesy of Astrid
'http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=etEq0Nk
vBHA.1332%40tkmsftngp03&rnum=7&prev=/groups%3Fas_q%3Dcreating%2520a%2520dupl
icate%2520word%2520document%2520vba%26safe%3Dimages%26ie%3DUTF-8%26oe%3DUTF-
8%26lr%3D%26hl%3Den

Dim oCurDoc As Document
Dim oNewDoc As Document
Dim bSaved As Boolean

Application.ScreenUpdating = False

Set oCurDoc = ActiveDocument
bSaved = oCurDoc.Saved
If Len(oCurDoc.Path) = 0 Then
'Document never saved before,
'save it once with your own filename
'and create a copy for the user to use

oCurDoc.SaveAs FileName:="C:\Windows\Temp\TempCopy.doc"
Set oNewDoc = Documents.Add(Template:=oCurDoc.FullName, Visible:=False)

With oNewDoc
.Saved = bSaved
.ActiveWindow.Visible = True
oCurDoc.Close
End With
Else
Set oNewDoc = Documents.Add(Template:=oCurDoc.FullName, Visible:=False)
With oNewDoc
.SaveAs FileName:="C:\Windows\Temp\TempCopy.doc"
.Close
End With
End If

Application.ScreenRefresh
Application.ScreenUpdating = True

Set oCurDoc = Nothing
Set oNewDoc = Nothing
End Sub
 
J

Jezebel

I tried your code. With a new document I get an error on the kill statement
on the every pass including the first: error 70 Permission Error/Sharing
violation. It seems to me that this is because with a new document you end
up with a second new document that uses the tempfile as its template. I
think the second document is somewhere retaining a reference to the tempfile
(not as the AttachedTemplate, however), which means it's in use and can't be
deleted. On my machine I can't kill the tempfile until the second document
is closed.

Separately, some of the Information() functions don't work properly if the
document is not in the correct display mode. Functions that refer to
repagination and positioning mostly need the document in Print view. You
need a check before, or error-handling around, the Information() statement.
 
K

Klic

Thanks Jezebel,

I ran the macro in PrintView but I'm running Word2000, so maybe thats why
the code gives a different error for you.
For the purpose of my macro I will force the user to save the document.

regards,
Keith
 
K

Klic

PS

Point taken about checking the document view, I hadn't thought to check that
in my orriginal macro...

cheers.
 

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