字 - 防止saveas2(vba)覆盖

发布于 2025-02-02 12:26:55 字数 1444 浏览 6 评论 0 原文

在互联网上的某个地方,我发现此代码可以轻松地将.docx文件从.dotx文件中保存到所需的文件夹中:

Sub SaveFileInTheCorrectDirectory()
    ActiveDocument.SaveAs2 "[the correct directory, put in manually by me in the VBA code]" & InputBox("Type the desired file name", "Save As")
End Sub

但是,此代码自动覆盖具有相同名称的已经存在的文件(当然是同一目录) 。我已经尝试寻找解决此问题的代码,并找到了一些建议:

,但我不知道如何实现它们...

有人可以这么友善地帮助我吗?

谢谢!

PS使用“ saveas2”而不是“ saveas”或另一种方式,是否有附加值?

Somewhere on the internet I found this code to easily save a .docx file out of a .dotx file into the desired folder:

Sub SaveFileInTheCorrectDirectory()
    ActiveDocument.SaveAs2 "[the correct directory, put in manually by me in the VBA code]" & InputBox("Type the desired file name", "Save As")
End Sub

However, this code automatically overwrites an already existing file with the same name (and in the same directory, of course). I've tried looking for code to fix this, and found a few suggestions:

But I can't figure out how to implement them...

Could someone be so kind to assist me?

Thanks!

PS Is there added value to use "SaveAs2" instead of "SaveAs" or the other way around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

好听的两个字的网名 2025-02-09 12:26:55

这很简单:

Dim StrName as String
StrName = InputBox("Type the desired file name", "Save As")
If Trim(StrName) = "" then Exit Sub
If Dir(StrPath & StrName & ".docx") = "" Then ActiveDocument.SaveAs2 StrPath & StrName & ".docx"

strpath& strname是路径&名称分别。

Note :我还没有添加任何有关文件存在的代码,因为在这种情况下您还没有说过要做的事情。如果您需要帮助,请发布一个新问题。

That's as simple as:

Dim StrName as String
StrName = InputBox("Type the desired file name", "Save As")
If Trim(StrName) = "" then Exit Sub
If Dir(StrPath & StrName & ".docx") = "" Then ActiveDocument.SaveAs2 StrPath & StrName & ".docx"

where StrPath & StrName are the path & name, respectively.

Note: I haven't added any code for what to do if the file exists because you haven't said what you want to do in that case. Post a new question if you need help with that.

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