简体   繁体   English

Word - 防止 SaveAs2 (VBA) 被覆盖

[英]Word - Prevent SaveAs2 (VBA) from overwriting

Somewhere on the internet I found this code to easily save a .docx file out of a .dotx file into the desired folder:在互联网上的某个地方,我发现此代码可以轻松地将 .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

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? PS 使用“SaveAs2”而不是“SaveAs”或其他方式是否有附加价值?

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.其中 StrPath 和 StrName 分别是路径和名称。

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.如果您需要帮助,请发布一个新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM