The following code works for DOCX fil开发者_运维知识库es, but gives "Access Denied" on DOC files:
Public Sub SaveGraded()
oldnamepath = ActiveDocument.FullName
oldname = GetFileName(ActiveDocument.Name)
oldExtension = getextension(ActiveDocument.FullName)
newname = oldname & "_GRADED" & "." & oldExtension
ActiveDocument.SaveAs filename:=newname
Kill oldnamepath
End Sub
Indeed, it's one of those weird things with Word, it'll hold a .doc file open exclusively much longer than a .docx.
You can use a loop to continually test delete until it works - don't use Kill for this since you can't trap it, use VBScript FileSystemObject.DeleteFile (str Name, bool Force), which is more reliable anyway - or you can use ActiveDocument.Close and reopen it. However I've noticed that even this doesn't always work and you sometimes have to follow that by Application.Quit; goodbye script execution unless you're automating it from an external application, which is what I do.
精彩评论