如果文件是 DOC 格式,则无法使用 Word 2007 VBA 杀死文件

发布于 2024-10-05 23:02:00 字数 381 浏览 0 评论 0原文

以下代码适用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女 2024-10-12 23:02:00

事实上,这是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文