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