如何使用 VBA 找到当前打开的 Excel 文档的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4044496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:14:34  来源:igfitidea点击:

How can I find the name of the currently open Excel document using VBA?

excel-vbavbaexcel

提问by Jacob Winn

I'm using VBA in Excel to read a spreadsheet and modify its contents. I need to save the results with a name that is similar to the original file, but with some alterations.

我在 Excel 中使用 VBA 来读取电子表格并修改其内容。我需要使用与原始文件类似的名称保存结果,但要进行一些更改。

How can I programmatically find the name of the currently open file using vba?

如何使用 vba 以编程方式查找当前打开的文件的名称?

回答by Michael

ThisWorkbook.Namewill return the file name. ThisWorkbook.FullNamewill return the directory structure and the file name.

ThisWorkbook.Name将返回文件名。 ThisWorkbook.FullName将返回目录结构和文件名。

回答by Fionnuala

It depends, here are two possibilities:

这取决于,这里有两种可能性:

Debug.Print ActiveWorkbook.Name

For Each wk In Workbooks
    Debug.Print wk.Name
Next