Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete old file after SaveAs

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
NL
How to delete a file after saving the file using ActiveDocument.SaveAs2

Code:
Sub savefile()
     ActiveDocument.SaveAs2 FileName:="c:\test\test.docm"
     Kill ActiveDocument.FullName
End Sub

The code above doesn't work because WORD 2010 doesn't release the file handle of the old document...
 
hi,

You are trying to kill the file you just did a SaveAs, which IS THE ACTIVE DOCUMENT!!!

Before performing the SaveAs, assign the FullName to a variable.

After performing the SaveAs, kill the file in the variable.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi Skip,

Thanks for your response.
I put the wrong code in this thread.
I'm using this option in a more complex code and in that code I'm using a var for the kill command.
Actually I'm using a recycle script instead of the kill command.

I've changed the code to ...
Code:
Sub savefile()
Dim source As String
    source = ActiveDocument.FullName
    ActiveDocument.SaveAs2 FileName:="d:\test\target.docm"
    Kill source
End Sub

...and to my surprise this works on my home PC.
I'm pretty sure I've tested this kind of code on my work PC and it didn't work.
I will test this immediately tomorrow when I arrive at my work place.
I'll report the result here.

CU tomorrow
 
For some inexplicable reason, the same macro that was generating an error yesterday, works today without any changes ??!!
The only thing changed was a system restart...
A strange phenomenon!
 
Please help....!!!
I still can't get the code working.
the code below works fine:
Code:
Sub savefile()
Dim source As String
    source = ActiveDocument.FullName
    ActiveDocument.SaveAs2 FileName:="c:\test\target.docm"
    Kill source
End Sub

But the code below is showing an error "permission denied" probably because the file is open ??

Code:
Option Explicit
'set variables
Public workdir As String
Public AdocFname As String
Public Adocname As String

Sub readParam()
'define global parameters
workdir = "C:\TEST\analyserapporten"
AdocFname = ActiveDocument.FullName
Adocname = ActiveDocument.Name
End Sub

Sub SaveAsDraft()
readParam 'read global parameters
Dim target As String
target = workdir & "\" & Adocname
If MsgBox("The draft version of this document will be saved as :" & vbCr & target, vbInformation + vbOKCancel, "Save File Info") = vbOK Then
    Select Case ActiveDocument.Path
        Case workdir
            ActiveDocument.Save
        Case Else
            ActiveDocument.SaveAs2 FileName:=target
            Kill AdocFname
'            If recycle(AdocFname) = False Then MsgBox AdocFname & vbCr & "Is not send to the Recycle Bin," & vbCr & "delete this file manually after closing WORD.", vbCritical + vbOKOnly, "Removing Error"
    End Select
    Else: MsgBox "File Save cancelled." & vbCr & "Document not saved !", vbCritical + vbOKOnly, "Save File Info"
End If
If MsgBox("Close this document ?", vbInformation + vbYesNo, "Info") = vbYes Then ActiveDocument.Close
End Sub
 
Additionally Info:

If I change the filename to a new name, the procedure works like it should ?!
So changing the line below works, but I don't want to change the name !

Code:
target = workdir & "\" & "temp_" & Adocname
 
Wmbb, The problem is, as SkipVought wrote, you are trying to kill an open file. Your code:
Code:
target = workdir & "\" & "temp_" & Adocname
has the same effect as
Code:
target = workdir & "\" & "subfolder\" & Adocname
Both are saving the active file to a new one. The kill command is not really doing anything. The 'permission denied' error 70 tells you that you can not kill a file from that same file. That is, the file cannot commit suicide.

Once you do the Saveas, your source file is saved as a new file. Then the active file is the new file. What is it that you want Excel to do?
 
Thanks all of you for trying to solve my problem.
First of all I'm using this script in a startup template in WORD 2010.
In the mean time I've worked around by closing the "saveas" document and then I can delete the original file.

My intention was to save a file, opened from anywhere, to a standard work directory and delete the original file.
The problem is that sometimes the code works and sometime it doesn't.
That's why it is difficult to trigger the problem.

In my opinion I'm not trying to kill the active document...
The original document name is saved in the var Adocname and the path is saved in AdocFname.
The file is saved to the work directory using saveas and subsequently I want to delete the original file (AdocFname).

Additional info:
If I pause the code before the deleting, and I deleted the temp file on the original location (~$...docx) manually, and continuing the script, the code works like it should.
After saving the file using saveas, on both locations (the original and the new) a temp file (~$...docx) is present, although just the document on the new location is open (and active).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top