开发者

Can't KILL file using Word 2007 VBA if it's DOC format

开发者 https://www.devze.com 2023-01-29 03:15 出处:网络
The following code works for DOCX fil开发者_运维知识库es, but gives \"Access Denied\" on DOC files:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消