如果文件是 DOC 格式,则无法使用 Word 2007 VBA 杀死文件
以下代码适用于 DOCX 文件,但对 DOC 文件给出“访问被拒绝”:
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
The following code works for DOCX files, 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,这是 Word 的怪异之处之一,它会比 .docx 更长时间地独占打开 .doc 文件。
您可以使用循环不断测试删除,直到它起作用 - 不要为此使用 Kill,因为您无法捕获它,使用 VBScript FileSystemObject.DeleteFile (str Name, bool Force),无论如何,这更可靠 - 或者您可以使用 ActiveDocument.Close 并重新打开它。然而我注意到,即使这样也并不总是有效,有时您必须遵循 Application.Quit;再见脚本执行,除非您从外部应用程序自动执行它,这就是我所做的。
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.